Compare commits

..

1 Commits

+38 -26
View File
@@ -21,7 +21,44 @@ import cuid from "cuid";
// Setup global event debugger in non-production environments
if (Bun.env.NODE_ENV == "development") setupEventDebugger();
// Helper to run a startup sync safely — failures are logged but never crash the process.
const safeStartup = async (label: string, fn: () => Promise<void>) => {
try {
await fn();
} catch (err) {
console.error(
`[startup] ${label} failed — will retry on next interval`,
err,
);
}
};
// ---------------------------------------------------------------------------
// Start the HTTP server FIRST so the pod is reachable immediately.
// All data-sync tasks run afterwards and are non-blocking.
// ---------------------------------------------------------------------------
Bun.serve({
port: PORT,
websocket: engine.handler().websocket,
fetch: (req, server) => {
const url = new URL(req.url);
if (url.pathname.startsWith("/socket.io/")) {
return engine.handleRequest(req, server as any);
}
return app.fetch(req, server);
},
});
console.log(`[startup] Server listening on port ${PORT}`);
// ---------------------------------------------------------------------------
// Background initialisation — none of this blocks the server.
// ---------------------------------------------------------------------------
// Ensure administrator role exists
await safeStartup("ensureAdminRole", async () => {
const existingAdmin = await prisma.role.findFirst({
where: { moniker: "administrator" },
include: { users: { include: { roles: true } } },
@@ -44,18 +81,7 @@ if (!existingAdmin) {
});
events.emit("role:created", new RoleController(created));
}
// Helper to run a startup sync safely — failures are logged but never crash the process.
const safeStartup = async (label: string, fn: () => Promise<void>) => {
try {
await fn();
} catch (err) {
console.error(
`[startup] ${label} failed — will retry on next interval`,
err,
);
}
};
});
// Refresh the internal list of companies every minute
await safeStartup("refreshCompanies", refreshCompanies);
@@ -98,17 +124,3 @@ setInterval(() => {
.syncSites()
.catch((err) => console.error("[interval] syncSites failed", err));
}, 60 * 1000);
Bun.serve({
port: PORT,
websocket: engine.handler().websocket,
fetch: (req, server) => {
const url = new URL(req.url);
if (url.pathname.startsWith("/socket.io/")) {
return engine.handleRequest(req, server as any);
}
return app.fetch(req, server);
},
});