55 行
1.3 KiB
JavaScript
55 行
1.3 KiB
JavaScript
const { getConfig } = require("./config");
|
|
const { initTracing } = require("./observability/tracing");
|
|
|
|
const config = getConfig();
|
|
initTracing(config);
|
|
|
|
const { buildApp } = require("./app");
|
|
|
|
async function main() {
|
|
const app = await buildApp();
|
|
app.log.info(
|
|
{
|
|
jobDriver: app.config.jobDriver,
|
|
jobsEnabled: app.config.jobsEnabled,
|
|
},
|
|
"API process started",
|
|
);
|
|
|
|
const { host, port } = app.config;
|
|
await app.listen({ host, port });
|
|
|
|
let shuttingDown = false;
|
|
const shutdown = async (signal) => {
|
|
if (shuttingDown) return;
|
|
shuttingDown = true;
|
|
|
|
app.log.info({ signal }, "Graceful shutdown start");
|
|
|
|
const timeoutMs = 10_000;
|
|
const forceTimer = setTimeout(() => {
|
|
app.log.error({ timeoutMs }, "Graceful shutdown timeout, force exit");
|
|
process.exit(1);
|
|
}, timeoutMs);
|
|
forceTimer.unref?.();
|
|
|
|
try {
|
|
await app.close();
|
|
app.log.info("Graceful shutdown completed");
|
|
process.exit(0);
|
|
} catch (error) {
|
|
app.log.error({ err: error }, "Graceful shutdown failed");
|
|
process.exit(1);
|
|
}
|
|
};
|
|
|
|
process.on("SIGTERM", () => shutdown("SIGTERM"));
|
|
process.on("SIGINT", () => shutdown("SIGINT"));
|
|
}
|
|
|
|
main().catch((err) => {
|
|
// eslint-disable-next-line no-console
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|