Files
mrp/server/src/server.ts

70 lines
2.2 KiB
TypeScript
Raw Normal View History

2026-03-14 14:44:40 -05:00
import { createApp } from "./app.js";
import { env } from "./config/env.js";
2026-03-15 19:40:35 -05:00
import { pruneOldAuthSessions } from "./lib/auth-sessions.js";
2026-03-14 14:44:40 -05:00
import { bootstrapAppData } from "./lib/bootstrap.js";
import { prisma } from "./lib/prisma.js";
2026-03-15 14:57:41 -05:00
import { setLatestStartupReport } from "./lib/startup-state.js";
import { assertStartupReadiness } from "./lib/startup-validation.js";
2026-03-15 15:21:27 -05:00
import { recordSupportLog } from "./lib/support-log.js";
2026-03-14 14:44:40 -05:00
async function start() {
await bootstrapAppData();
2026-03-15 19:40:35 -05:00
const prunedSessionCount = await pruneOldAuthSessions();
2026-03-15 14:57:41 -05:00
const startupReport = await assertStartupReadiness();
setLatestStartupReport(startupReport);
2026-03-15 15:21:27 -05:00
recordSupportLog({
level: startupReport.status === "PASS" ? "INFO" : startupReport.status === "WARN" ? "WARN" : "ERROR",
source: "startup-validation",
message: `Startup validation completed with status ${startupReport.status}.`,
context: {
generatedAt: startupReport.generatedAt,
durationMs: startupReport.durationMs,
passCount: startupReport.passCount,
warnCount: startupReport.warnCount,
failCount: startupReport.failCount,
2026-03-15 19:40:35 -05:00
prunedSessionCount,
2026-03-15 15:21:27 -05:00
},
});
2026-03-15 14:57:41 -05:00
for (const check of startupReport.checks.filter((entry) => entry.status !== "PASS")) {
console.warn(`[startup:${check.status.toLowerCase()}] ${check.label}: ${check.message}`);
2026-03-15 15:21:27 -05:00
recordSupportLog({
level: check.status === "WARN" ? "WARN" : "ERROR",
source: "startup-check",
message: `${check.label}: ${check.message}`,
context: {
checkId: check.id,
status: check.status,
},
});
2026-03-15 14:57:41 -05:00
}
2026-03-14 14:44:40 -05:00
const app = createApp();
const server = app.listen(env.PORT, () => {
console.log(`MRP server listening on port ${env.PORT}`);
});
const shutdown = async () => {
server.close();
await prisma.$disconnect();
};
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
}
start().catch(async (error) => {
console.error(error);
2026-03-15 15:21:27 -05:00
recordSupportLog({
level: "ERROR",
source: "server-startup",
message: error instanceof Error ? error.message : "Server startup failed.",
context: {
stack: error instanceof Error ? error.stack ?? null : null,
},
});
2026-03-14 14:44:40 -05:00
await prisma.$disconnect();
process.exit(1);
});