oidc-provider 9.13.3 with PKCE, ES256, crosscode.identity/1 claims and back-channel logout; clients are the linked products (Connect). Sign-in steps (password, sign-up, email check, 2FA and its setup, privacy notice, news question, password reset) survive to the product. Account page and admin API, church profile, export and deletion fan-out, Hub v2 client, jobs. End-to-end tests with a test product on openid-client. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
55 lines
2.7 KiB
TypeScript
55 lines
2.7 KiB
TypeScript
/**
|
|
* Background jobs on pg-boss (as Cross Events): one run per schedule however many processes run,
|
|
* and timers in this process if the queue can't start. JOBS_ENABLED=false turns them all off.
|
|
*/
|
|
import PgBoss from 'pg-boss';
|
|
import type { Ctx } from './context.js';
|
|
import { heartbeat } from './hub/client.js';
|
|
import { runDue } from './links/deliveries.js';
|
|
import { deleteExpired } from './oidc/adapter.js';
|
|
import { runDeletionJob } from './privacy/deletion.js';
|
|
import { runExportsJob } from './privacy/export.js';
|
|
|
|
type Job = { name: string; cron: string; everyMs: number; run: (ctx: Ctx) => Promise<unknown> };
|
|
|
|
export const JOBS: Job[] = [
|
|
{ name: 'deliveries', cron: '* * * * *', everyMs: 60e3, run: (ctx) => runDue(ctx) },
|
|
{ name: 'exports', cron: '*/5 * * * *', everyMs: 5 * 60e3, run: (ctx) => runExportsJob(ctx) },
|
|
{ name: 'deletions', cron: '*/15 * * * *', everyMs: 15 * 60e3, run: (ctx) => runDeletionJob(ctx) },
|
|
{ name: 'hub-heartbeat', cron: '*/15 * * * *', everyMs: 15 * 60e3, run: (ctx) => heartbeat(ctx) },
|
|
{
|
|
name: 'cleanup', cron: '17 * * * *', everyMs: 3600e3,
|
|
run: async (ctx) => {
|
|
const now = ctx.now();
|
|
const [oidc, pending, tokens, nonces] = await Promise.all([
|
|
deleteExpired(ctx),
|
|
ctx.db.pendingSignIn.deleteMany({ where: { expiresAt: { lt: now } } }),
|
|
ctx.db.emailToken.deleteMany({ where: { expiresAt: { lt: new Date(now.getTime() - 7 * 24 * 3600e3) } } }),
|
|
ctx.db.linkNonce.deleteMany({ where: { seenAt: { lt: new Date(now.getTime() - 3600e3) } } }),
|
|
]);
|
|
return { oidc: oidc.count, pending: pending.count, tokens: tokens.count, nonces: nonces.count };
|
|
},
|
|
},
|
|
];
|
|
|
|
export async function startJobs(ctx: Ctx, connectionString: string): Promise<() => Promise<void>> {
|
|
if (!ctx.config.jobsEnabled) return async () => {};
|
|
const tz = ctx.config.timezone;
|
|
try {
|
|
const boss = new PgBoss({ connectionString, schema: 'pgboss_accounts', application_name: 'crosscode-accounts-jobs' });
|
|
boss.on('error', (e: Error) => console.warn('[jobs]', e.message));
|
|
await boss.start();
|
|
for (const job of JOBS) {
|
|
await boss.createQueue(job.name, { policy: 'stately' }).catch(() => {});
|
|
await boss.schedule(job.name, job.cron, undefined, { tz });
|
|
await boss.work(job.name, async () => job.run(ctx));
|
|
}
|
|
console.log(`[jobs] Job queue started: ${JOBS.map((j) => j.name).join(', ')}.`);
|
|
return async () => { await boss.stop({ graceful: true }); };
|
|
} catch (e) {
|
|
console.warn('[jobs] Job queue unavailable, using timers:', (e as Error).message);
|
|
const timers = JOBS.map((job) => setInterval(() => { job.run(ctx).catch((err) => console.warn(`[jobs] ${job.name}:`, (err as Error).message)); }, job.everyMs));
|
|
return async () => { timers.forEach(clearInterval); };
|
|
}
|
|
}
|