import { Logger } from "@nestjs/common"; import type { INestApplication } from "@nestjs/common"; import { bootstrap } from "./app/bootstrap.js"; const logger = new Logger("Main"); let app: INestApplication | null = null; const signals: NodeJS.Signals[] = ["SIGINT", "SIGTERM"]; for (const signal of signals) { process.once(signal, () => { void (async () => { logger.log(`Received ${signal}. Closing Nest application...`); // #region agent log (hypothesis S1) fetch("http://127.0.0.1:7242/ingest/a683e422-cfe7-4556-a583-809fbfbeeb4a", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ sessionId: "debug-session", runId: "run1", hypothesisId: "S1", location: "apps/bff/src/main.ts:signalHandler(entry)", message: "Process signal handler invoked", data: { signal, pid: process.pid, appInitialized: !!app }, timestamp: Date.now(), }), }).catch(() => {}); // #endregion if (!app) { logger.warn("Nest application not initialized. Exiting immediately."); process.exit(0); return; } try { // #region agent log (hypothesis S1) fetch("http://127.0.0.1:7242/ingest/a683e422-cfe7-4556-a583-809fbfbeeb4a", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ sessionId: "debug-session", runId: "run1", hypothesisId: "S1", location: "apps/bff/src/main.ts:signalHandler(beforeClose)", message: "Calling app.close()", data: { signal, pid: process.pid }, timestamp: Date.now(), }), }).catch(() => {}); // #endregion await app.close(); logger.log("Nest application closed gracefully."); } catch (error) { const resolvedError = error as Error; logger.error( `Error during Nest application shutdown: ${resolvedError.message}`, resolvedError.stack ); } finally { // #region agent log (hypothesis S1) fetch("http://127.0.0.1:7242/ingest/a683e422-cfe7-4556-a583-809fbfbeeb4a", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ sessionId: "debug-session", runId: "run1", hypothesisId: "S1", location: "apps/bff/src/main.ts:signalHandler(exit)", message: "Exiting process after shutdown handler", data: { signal, pid: process.pid }, timestamp: Date.now(), }), }).catch(() => {}); // #endregion process.exit(0); } })(); }); } void bootstrap() .then(startedApp => { app = startedApp; }) .catch(error => { const resolvedError = error as Error; logger.error( `Failed to bootstrap the Nest application: ${resolvedError.message}`, resolvedError.stack ); process.exit(1); });