Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mini fixes #771

Merged
merged 2 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* eslint-disable no-process-exit */
import { nextTick } from 'async';

import { StateManager } from './StateManager';
import * as algolia from './algolia/index';
import { createAPI } from './api';
Expand All @@ -12,7 +15,7 @@ import * as watch from './watch';

log.info('🗿 npm ↔️ Algolia replication starts ⛷ 🐌 🛰');

const KILL_PROCESS_EVERY_MS = 12 * 60 * 60 * 1000; // every 12 hours
const KILL_PROCESS_EVERY_MS = 1 * 60 * 60 * 1000; // every 1 hours

/**
* Main process
Expand All @@ -27,7 +30,6 @@ async function main(): Promise<void> {
// - maybe retrigger bootstrap
setTimeout(() => {
log.info('👋 Scheduled process cleaning');
// eslint-disable-next-line no-process-exit
process.exit(0);
}, KILL_PROCESS_EVERY_MS);

Expand Down Expand Up @@ -65,5 +67,32 @@ async function main(): Promise<void> {
main().catch(async (err) => {
sentry.report(err);
await sentry.drain();
process.exit(1); // eslint-disable-line no-process-exit
process.exit(1);
});

async function close(): Promise<void> {
log.info('Close was requested');
await sentry.drain();

nextTick(() => {
process.exit(1);
});
}

process.once('SIGINT', async () => {
await close();
});

process.once('SIGTERM', async () => {
await close();
});

process.on('unhandledRejection', async (reason) => {
sentry.report(reason);
await close();
});

// Report any uncaught exception, without letting the process crash
process.on('uncaughtException', (err) => {
sentry.report(err, { context: 'uncaughtException' });
});
25 changes: 15 additions & 10 deletions src/npm/Prefetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { DocumentListParams, DocumentResponseRow } from 'nano';

import { config } from '../config';
import { log } from '../utils/log';
import * as sentry from '../utils/sentry';
import { wait } from '../utils/wait';

import type { GetPackage } from './types';
Expand Down Expand Up @@ -68,18 +69,22 @@ export class Prefetcher {
options.startkey = this.#nextKey;
options.skip = 1;
}
const { rows: packages, offset } = await npm.findAll(options);
try {
const { rows: packages, offset } = await npm.findAll(options);

if (packages.length <= 0) {
this.#finished = true;
this.#running = false;
this.#offset = offset;
log.info('[pf] done');
return;
}

if (packages.length <= 0) {
this.#finished = true;
this.#running = false;
this.#ready.push(...packages);
this.#offset = offset;
log.info('[pf] done');
return;
this.#nextKey = packages[packages.length - 1].id;
} catch (err) {
sentry.report(err);
}

this.#ready.push(...packages);
this.#offset = offset;
this.#nextKey = packages[packages.length - 1].id;
}
}