From 5fef947b50316b280404de6f53b39c33b73cbdff Mon Sep 17 00:00:00 2001 From: Samuel Bodin <1637651+bodinsamuel@users.noreply.github.com> Date: Thu, 28 Oct 2021 11:27:35 +0200 Subject: [PATCH] fix: mini fixes --- src/index.ts | 35 ++++++++++++++++++++++++++++++++--- src/npm/Prefetcher.ts | 25 +++++++++++++++---------- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index 527d320aa..b256d5771 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -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 @@ -27,7 +30,6 @@ async function main(): Promise { // - maybe retrigger bootstrap setTimeout(() => { log.info('👋 Scheduled process cleaning'); - // eslint-disable-next-line no-process-exit process.exit(0); }, KILL_PROCESS_EVERY_MS); @@ -65,5 +67,32 @@ async function main(): Promise { 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 { + 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' }); }); diff --git a/src/npm/Prefetcher.ts b/src/npm/Prefetcher.ts index 55fd2da47..296026280 100644 --- a/src/npm/Prefetcher.ts +++ b/src/npm/Prefetcher.ts @@ -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'; @@ -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; } }