mirror your GitHub repos to tangled.org automatically
1import type { H3Event } from 'h3'
2
3/**
4 * Nudge the job worker to run now, instead of waiting for the next cron tick.
5 *
6 * Fire-and-forget: registered via `event.waitUntil` so the serverless function
7 * stays alive to complete the kick after the response flushes, but the webhook
8 * never blocks on it. Any failure is swallowed; the per-minute cron is the
9 * safety net, so a missed kick only costs latency, not correctness. The worker
10 * itself is safe to run concurrently with cron (claims are `FOR UPDATE SKIP
11 * LOCKED` with a lease), so a kick racing a tick can't double-process a job.
12 *
13 * Auth uses the same `CRON_SECRET` bearer the worker route already requires.
14 */
15export function kickWorker(event: H3Event): void {
16 const cronSecret = process.env.CRON_SECRET
17 const base = useRuntimeConfig().public.url?.replace(/\/$/, '')
18 if (!cronSecret || !base) return
19
20 const run = globalThis.fetch(`${base}/api/jobs/run`, {
21 method: 'GET',
22 headers: { authorization: `Bearer ${cronSecret}` },
23 })
24 .then(() => undefined)
25 .catch(() => undefined)
26
27 event.waitUntil(run)
28}