Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
fix(api): graceful shutdown handlers
Gergő Móricz4d57f68c3e8c
diff --git a/apps/api/src/harness.ts b/apps/api/src/harness.tsindex 12d944c8b..ccfbed79f 100644--- a/apps/api/src/harness.ts+++ b/apps/api/src/harness.ts@@ -858,8 +858,10 @@ async function waitForTermination(services: Services): Promise<void> {const promises: Promise<void>[] = [new Promise<void>(resolve => {- process.on("SIGINT", resolve);- process.on("SIGTERM", resolve);+ if (require.main === module) {+ process.on("SIGINT", resolve);+ process.on("SIGTERM", resolve);+ }}),];@@ -918,8 +920,10 @@ function printUsage() {}async function main() {- process.on("SIGINT", gracefulShutdown);- process.on("SIGTERM", gracefulShutdown);+ if (require.main === module) {+ process.on("SIGINT", gracefulShutdown);+ process.on("SIGTERM", gracefulShutdown);+ }try {if (process.argv.length < 3) {diff --git a/apps/api/src/index.ts b/apps/api/src/index.tsindex 6e4ad28cb..a30ca811c 100644--- a/apps/api/src/index.ts+++ b/apps/api/src/index.ts@@ -149,8 +149,10 @@ async function startServer(port = DEFAULT_PORT) {});};- process.on("SIGTERM", exitHandler);- process.on("SIGINT", exitHandler);+ if (require.main === module) {+ process.on("SIGTERM", exitHandler);+ process.on("SIGINT", exitHandler);+ }return server;}diff --git a/apps/api/src/services/extract-worker.ts b/apps/api/src/services/extract-worker.tsindex 5eed8eb2f..19b2f31fc 100644--- a/apps/api/src/services/extract-worker.ts+++ b/apps/api/src/services/extract-worker.ts@@ -175,15 +175,17 @@ const processExtractJobInternal = async (let isShuttingDown = false;let isWorkerStalled = false;-process.on("SIGINT", () => {- _logger.debug("Received SIGINT. Shutting down gracefully...");- isShuttingDown = true;-});+if (require.main === module) {+ process.on("SIGINT", () => {+ _logger.debug("Received SIGINT. Shutting down gracefully...");+ isShuttingDown = true;+ });-process.on("SIGTERM", () => {- _logger.debug("Received SIGTERM. Shutting down gracefully...");- isShuttingDown = true;-});+ process.on("SIGTERM", () => {+ _logger.debug("Received SIGTERM. Shutting down gracefully...");+ isShuttingDown = true;+ });+}let cantAcceptConnectionCount = 0;diff --git a/apps/api/src/services/indexing/index-worker.ts b/apps/api/src/services/indexing/index-worker.tsindex b39bdeac3..44fbba355 100644--- a/apps/api/src/services/indexing/index-worker.ts+++ b/apps/api/src/services/indexing/index-worker.ts@@ -556,15 +556,17 @@ const processPrecrawlJob = async (token: string, job: Job) => {let isShuttingDown = false;-process.on("SIGINT", () => {- logger.info("Received SIGINT. Shutting down gracefully...");- isShuttingDown = true;-});--process.on("SIGTERM", () => {- logger.info("Received SIGTERM. Shutting down gracefully...");- isShuttingDown = true;-});+if (require.main === module) {+ process.on("SIGINT", () => {+ logger.info("Received SIGINT. Shutting down gracefully...");+ isShuttingDown = true;+ });++ process.on("SIGTERM", () => {+ logger.info("Received SIGTERM. Shutting down gracefully...");+ isShuttingDown = true;+ });+}let cantAcceptConnectionCount = 0;diff --git a/apps/api/src/services/queue-worker.ts b/apps/api/src/services/queue-worker.tsindex 3bda3f5b3..0afc8fce8 100644--- a/apps/api/src/services/queue-worker.ts+++ b/apps/api/src/services/queue-worker.ts@@ -231,15 +231,17 @@ async function processFinishCrawlJobInternal(_job: NuQJob) {let isShuttingDown = false;let isWorkerStalled = false;-process.on("SIGINT", () => {- _logger.debug("Received SIGINT. Shutting down gracefully...");- isShuttingDown = true;-});+if (require.main === module) {+ process.on("SIGINT", () => {+ _logger.debug("Received SIGINT. Shutting down gracefully...");+ isShuttingDown = true;+ });-process.on("SIGTERM", () => {- _logger.debug("Received SIGTERM. Shutting down gracefully...");- isShuttingDown = true;-});+ process.on("SIGTERM", () => {+ _logger.debug("Received SIGTERM. Shutting down gracefully...");+ isShuttingDown = true;+ });+}let cantAcceptConnectionCount = 0;diff --git a/apps/api/src/services/worker/nuq-prefetch-worker.ts b/apps/api/src/services/worker/nuq-prefetch-worker.tsindex 909bd10f4..2a590e2f2 100644--- a/apps/api/src/services/worker/nuq-prefetch-worker.ts+++ b/apps/api/src/services/worker/nuq-prefetch-worker.ts@@ -38,8 +38,10 @@ import { logger } from "../../lib/logger";process.exit(0);}- process.on("SIGINT", shutdown);- process.on("SIGTERM", shutdown);+ if (require.main === module) {+ process.on("SIGINT", shutdown);+ process.on("SIGTERM", shutdown);+ }(async () => {while (true) {diff --git a/apps/api/src/services/worker/nuq-worker.ts b/apps/api/src/services/worker/nuq-worker.tsindex cb4aaf0ec..5e7318082 100644--- a/apps/api/src/services/worker/nuq-worker.ts+++ b/apps/api/src/services/worker/nuq-worker.ts@@ -46,8 +46,10 @@ import { initializeEngineForcing } from "../../scraper/WebScraper/utils/engine-fisShuttingDown = true;}- process.on("SIGINT", shutdown);- process.on("SIGTERM", shutdown);+ if (require.main === module) {+ process.on("SIGINT", shutdown);+ process.on("SIGTERM", shutdown);+ }let noJobTimeout = 1500;diff --git a/apps/api/src/services/worker/scrape-worker.ts b/apps/api/src/services/worker/scrape-worker.tsindex 78f628b4a..5b977ac57 100644--- a/apps/api/src/services/worker/scrape-worker.ts+++ b/apps/api/src/services/worker/scrape-worker.ts@@ -1218,6 +1218,8 @@ const exitHandler = () => {process.exit(0);};-process.on("SIGINT", exitHandler);-process.on("SIGTERM", exitHandler);-process.on("exit", exitHandler);+if (require.main === module) {+ process.on("SIGINT", exitHandler);+ process.on("SIGTERM", exitHandler);+ process.on("exit", exitHandler);+}diff --git a/apps/playwright-service-ts/api.ts b/apps/playwright-service-ts/api.tsindex 21f117687..1492c0c21 100644--- a/apps/playwright-service-ts/api.ts+++ b/apps/playwright-service-ts/api.ts@@ -292,9 +292,11 @@ app.listen(port, () => {});});-process.on('SIGINT', () => {- shutdownBrowser().then(() => {- console.log('Browser closed');- process.exit(0);+if (require.main === module) {+ process.on('SIGINT', () => {+ shutdownBrowser().then(() => {+ console.log('Browser closed');+ process.exit(0);+ });});-});+}