From 923a51521c54e1a1ede5d2076acdd7cf1ab4ad69 Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Thu, 16 Nov 2023 10:53:36 +0700 Subject: [PATCH] hotfix: hackernews fork --- src/app.js | 30 ++++++++++++++++-------------- src/hackernews.js | 4 ++-- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/app.js b/src/app.js index d589803..9e6e99b 100644 --- a/src/app.js +++ b/src/app.js @@ -34,6 +34,22 @@ const mongo = mongoose.createConnection(String(process.env.MONGO_URL), { useNewUrlParser: true }); +// Fork processes +const hackernewsFork = fork(pathTo(import.meta.url, "./hackernews.js"), { detached: true }); + +function terminate(caller) { + const t = Date.now(); + mongo.close(); + bot.stop(caller); + hackernewsFork.kill(); + terminal.info(`${caller}: ${Date.now() - t}ms`); +} + +// Enable graceful stop, register to process; +process.once("SIGINT", () => terminate("SIGINT")); +process.once("SIGTERM", () => terminate("SIGTERM")); + + async function main() { mongo.on("connected", () => terminal.info("MongoDB connected")); @@ -113,7 +129,6 @@ async function main() { } }); - fork(pathTo("./hackernews.js"), { detached: false }); // For more information about what this is, please refer to: // https://nodejs.org/api/process.html#process_process_memoryusage @@ -128,16 +143,3 @@ async function main() { } main(); - -function terminate(caller) { - const t = Date.now(); - mongo.close((err) => { - err && terminal.error(err); - }); - bot.stop(caller); - terminal.info(`${caller}: ${Date.now() - t}ms`); -} - -// Enable graceful stop -process.once("SIGINT", () => terminate("SIGINT")); -process.once("SIGTERM", () => terminate("SIGTERM")); diff --git a/src/hackernews.js b/src/hackernews.js index 5e33d48..54c1fb0 100644 --- a/src/hackernews.js +++ b/src/hackernews.js @@ -1,8 +1,8 @@ import { Telegraf } from "telegraf"; import dotenv from "dotenv"; import { pathTo } from "#utils/path.js"; -import { run } from "#services/hackernews.js"; -import { sentry } from "#utils/logger.js"; +import { run } from "#services/hackernews/index.js"; +import { sentry } from "#utils/logger/index.js"; dotenv.config({ path: pathTo(import.meta.url, "../.env") });