refactor(hooks): move server bootstrap into ServerInit hook
Module-level top-level await for db/scheduler init and the cache warmup IIFE move into the canonical export const init hook. Same ordering and non-blocking semantics; makes the lifecycle explicit and works on environments without top-level await.
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "homepage",
|
"name": "homepage",
|
||||||
"version": "1.52.1",
|
"version": "1.52.2",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
+16
-17
@@ -1,4 +1,4 @@
|
|||||||
import type { Handle, HandleServerError } from "@sveltejs/kit"
|
import type { Handle, HandleServerError, ServerInit } from "@sveltejs/kit"
|
||||||
import { redirect } from "@sveltejs/kit"
|
import { redirect } from "@sveltejs/kit"
|
||||||
import { sequence } from "@sveltejs/kit/hooks"
|
import { sequence } from "@sveltejs/kit/hooks"
|
||||||
import * as auth from "./auth"
|
import * as auth from "./auth"
|
||||||
@@ -32,27 +32,26 @@ async function timing({ event, resolve }: Parameters<Handle>[0]) {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize database connection on server startup
|
export const init: ServerInit = async () => {
|
||||||
console.log('🚀 Server starting - initializing database connection...');
|
console.log('🚀 Server starting - initializing database connection...');
|
||||||
await dbConnect().then(() => {
|
try {
|
||||||
console.log('✅ Database connected successfully');
|
await dbConnect();
|
||||||
// Initialize the recurring payment scheduler after DB is ready
|
console.log('✅ Database connected successfully');
|
||||||
initializeScheduler();
|
initializeScheduler();
|
||||||
console.log('✅ Recurring payment scheduler initialized');
|
console.log('✅ Recurring payment scheduler initialized');
|
||||||
}).catch((error) => {
|
} catch (error) {
|
||||||
console.error('❌ Failed to connect to database on startup:', error);
|
console.error('❌ Failed to connect to database on startup:', error);
|
||||||
// Don't crash the server - API routes will attempt reconnection
|
// Don't crash the server - API routes will attempt reconnection
|
||||||
});
|
}
|
||||||
|
|
||||||
// Warm liturgical calendar cache in the background — non-blocking so the
|
// Warm liturgical calendar cache in the background — non-blocking so the
|
||||||
// server starts accepting requests immediately; any request arriving before
|
// server starts accepting requests immediately; any request arriving before
|
||||||
// warmup completes falls back to lazy computation (still correct, just cold).
|
// warmup completes falls back to lazy computation (still correct, just cold).
|
||||||
{
|
|
||||||
const t0 = performance.now();
|
const t0 = performance.now();
|
||||||
warmLiturgicalCache()
|
warmLiturgicalCache()
|
||||||
.then(() => console.log(`✅ Liturgical calendar cache warmed in ${Math.round(performance.now() - t0)}ms`))
|
.then(() => console.log(`✅ Liturgical calendar cache warmed in ${Math.round(performance.now() - t0)}ms`))
|
||||||
.catch((error) => console.error('⚠️ Liturgical calendar warmup failed:', error));
|
.catch((error) => console.error('⚠️ Liturgical calendar warmup failed:', error));
|
||||||
}
|
};
|
||||||
|
|
||||||
async function authorization({ event, resolve }: Parameters<Handle>[0]) {
|
async function authorization({ event, resolve }: Parameters<Handle>[0]) {
|
||||||
const session = await event.locals.timing.measure('auth', () => event.locals.auth());
|
const session = await event.locals.timing.measure('auth', () => event.locals.auth());
|
||||||
|
|||||||
Reference in New Issue
Block a user