As Accounts and Cross Events: a gzipped pg_dump every night (02:20) into backend/backups/, the same website-*.sql.gz files crosscode makes before updates, with Admin -> Backups (back up now, download, how many to keep). The heartbeat, event feed and YouTube move from plain timers onto pg-boss with the backup, and Admin -> Background jobs switches each on or off, changes its schedule or runs it now. The heartbeat now reports the newest backup (before: always none, so the Hub warned) and the real job state. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
50 lines
2.1 KiB
TypeScript
50 lines
2.1 KiB
TypeScript
/**
|
|
* Starts the Church Website's backend: database, HTTP server, and (on a Hub install) registering
|
|
* with the Hub from HUB_LICENCE_KEY, then the background jobs (src/jobs.ts, 1.4.0): heartbeats,
|
|
* which also link the site to the church's Accounts once the Hub lists it, the event feed, the
|
|
* hourly YouTube feed (§7) and the nightly database backup.
|
|
*/
|
|
import 'dotenv/config';
|
|
import { PrismaPg } from '@prisma/adapter-pg';
|
|
import { PrismaClient } from './generated/prisma/client.js';
|
|
import { createApp } from './app.js';
|
|
import { createCtx } from './context.js';
|
|
import { connectFromEnv, heartbeat } from './hub/client.js';
|
|
import { startJobs } from './jobs.js';
|
|
import { loadConfig } from './lib/config.js';
|
|
import { ensureStarterPages } from './site/starter.js';
|
|
|
|
async function main() {
|
|
const config = loadConfig();
|
|
const url = process.env.DATABASE_URL;
|
|
if (!url) throw new Error('DATABASE_URL isn\'t set.');
|
|
const db = new PrismaClient({ adapter: new PrismaPg({ connectionString: url, connectionTimeoutMillis: 5000 }) });
|
|
const ctx = createCtx(config, db);
|
|
// A new install's fixed pages (home, Plan your visit, …), before anything is served.
|
|
await ensureStarterPages(ctx)
|
|
.then((kinds) => { if (kinds.length) console.log(`[website] Added the starter pages: ${kinds.join(', ')}.`); })
|
|
.catch((e) => console.error('[website] Couldn\'t add the starter pages:', (e as Error).message));
|
|
const app = createApp(ctx);
|
|
const server = app.listen(config.port, () => console.log(`[website] Listening on ${config.port} (${config.publicUrl}).`));
|
|
// 1.4.0: the heartbeat, the event feed, YouTube and the nightly backup, on pg-boss.
|
|
const stopJobs = await startJobs(ctx, url);
|
|
|
|
connectFromEnv(ctx).then((r) => {
|
|
if (r.connected || r.reason === 'already') heartbeat(ctx).catch(() => {});
|
|
}).catch(() => {});
|
|
|
|
const stop = async () => {
|
|
await stopJobs();
|
|
server.close();
|
|
await db.$disconnect();
|
|
process.exit(0);
|
|
};
|
|
process.on('SIGTERM', stop);
|
|
process.on('SIGINT', stop);
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error('[website] Failed to start:', e);
|
|
process.exit(1);
|
|
});
|