import { logger } from '@/logger'; import { ApplyPriceChanges } from './apply-price-changes'; import { ReconcileSubscriptions } from './reconcile-subscriptions'; const RECONCILE_CRON = '*/30 * * * *'; const PRICE_CHANGES_CRON = '*/5 * * * *'; /** * Registers the billing background jobs. Called once from `server.ts`, with the * same guards as `startPaymentJobs`: a rejected promise inside Bun's cron would * crash the process, and `unref` keeps the job from holding it alive. */ export function startBillingJobs(): void { Bun.cron( RECONCILE_CRON, async () => { try { const totals = await new ReconcileSubscriptions().execute(); logger.info(totals, 'Pro subscriptions reconciliation finished'); } catch (error) { logger.error({ err: error }, 'Pro subscriptions reconciliation failed'); } }, { tz: 'UTC' }, ).unref(); Bun.cron( PRICE_CHANGES_CRON, async () => { await runPriceChanges(); }, { tz: 'UTC' }, ).unref(); } /** * Applies the Pro price changes queued by the backoffice. Also called right * after a save, so the change does not wait for the next tick. Never throws. */ export async function runPriceChanges(): Promise { try { const totals = await new ApplyPriceChanges().execute(); if (totals.applied + totals.failed + totals.dropped > 0) { logger.info(totals, 'Pro price changes applied'); } } catch (error) { logger.error({ err: error }, 'Applying Pro price changes failed'); } }