2025-12-10 16:08:34 +09:00
|
|
|
import { Logger } from "@nestjs/common";
|
|
|
|
|
import type { INestApplication } from "@nestjs/common";
|
2025-12-02 10:05:11 +09:00
|
|
|
|
2025-12-10 16:08:34 +09:00
|
|
|
import { bootstrap } from "./app/bootstrap.js";
|
2025-08-20 18:02:50 +09:00
|
|
|
|
2025-09-18 15:18:12 +09:00
|
|
|
const logger = new Logger("Main");
|
|
|
|
|
let app: INestApplication | null = null;
|
2025-08-22 17:02:49 +09:00
|
|
|
|
2025-09-18 15:18:12 +09:00
|
|
|
const signals: NodeJS.Signals[] = ["SIGINT", "SIGTERM"];
|
|
|
|
|
for (const signal of signals) {
|
2025-09-25 13:21:11 +09:00
|
|
|
process.once(signal, () => {
|
|
|
|
|
void (async () => {
|
|
|
|
|
logger.log(`Received ${signal}. Closing Nest application...`);
|
2025-12-12 18:44:26 +09:00
|
|
|
// #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
|
2025-08-28 16:57:57 +09:00
|
|
|
|
2025-09-25 13:21:11 +09:00
|
|
|
if (!app) {
|
|
|
|
|
logger.warn("Nest application not initialized. Exiting immediately.");
|
|
|
|
|
process.exit(0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-22 17:02:49 +09:00
|
|
|
|
2025-09-25 13:21:11 +09:00
|
|
|
try {
|
2025-12-12 18:44:26 +09:00
|
|
|
// #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
|
2025-09-25 13:21:11 +09:00
|
|
|
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 {
|
2025-12-12 18:44:26 +09:00
|
|
|
// #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
|
2025-09-25 13:21:11 +09:00
|
|
|
process.exit(0);
|
|
|
|
|
}
|
|
|
|
|
})();
|
2025-08-20 18:02:50 +09:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-18 15:18:12 +09:00
|
|
|
void bootstrap()
|
2025-09-25 11:44:10 +09:00
|
|
|
.then(startedApp => {
|
2025-09-18 15:18:12 +09:00
|
|
|
app = startedApp;
|
|
|
|
|
})
|
2025-09-25 11:44:10 +09:00
|
|
|
.catch(error => {
|
2025-09-18 15:18:12 +09:00
|
|
|
const resolvedError = error as Error;
|
|
|
|
|
logger.error(
|
|
|
|
|
`Failed to bootstrap the Nest application: ${resolvedError.message}`,
|
|
|
|
|
resolvedError.stack
|
|
|
|
|
);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
});
|