/** * JOBS OUT 4, the dead-man check (NEXT-JOBS-OUT-4 item 4, 28 Nov 2026). The owner's account object's alarm stood set in * the past from 03:53 to 09:55 IST and never fired: for six hours no mail was fetched, nothing scheduled was sent, no * calendar pulled. Every job hung off that one alarm, and nothing outside the object looked at it. * * This lives outside the object: the Worker's cron (wrangler.jsonc `triggers.crons`, every 5 minutes) pages through the * directory's accounts and asks each object how its alarm stands (`Business.alarmWatch`). An alarm missing, or standing * in the past by more than ALARM_STALE_MS, is set again in five seconds and logged with when the last pass began and * ended. It never runs a job itself and reads no table of an account. * * Every live personal account always holds an alarm (each pass sets the next, at most an hour on, idle or not), so a * missing one is a fault, not rest. The exceptions keep their own alarm and may leave it unset on purpose, so the * watch leaves them alone: a deletion under way (pending, held, erasing, done), an owner's restore, a frozen account. */import { directory as directoryOf } from"./directory.ts";importtype { Env } from"./env.ts";/** An alarm this far in the past never fired: the platform fires a due alarm within seconds. */exportconst ALARM_STALE_MS = 10 * 60_000;/** Accounts asked at once, and per directory page. */const AT_ONCE = 10;const PAGE = 200;exportinterfaceAlarmWatch { slug: string; alarm_at: string | null; last_began: string | null; last_ended: string | null; running: string | null; rearmed: boolean;/** Why it was left as it stands: its own code keeps its alarm (a deletion, an owner's restore, frozen). */ left?: "deleting" | "restoring" | "frozen" | "restore_unknown";}/** Whether an alarm standing at `at` (null: none) is dead at `now`. */exportfunction alarmStale(at: number | null, now: number): boolean {return at === null || at < now - ALARM_STALE_MS;}exportinterfaceWatchRun { asked: number; rearmed: string[]; failed: string[];}/** One cron run: every account in the directory asked, AT_ONCE at a time; one that fails is said and the rest go on. */exportasyncfunction watchAlarms(env: Env, now: number = Date.now()): Promise<WatchRun> {const directory = directoryOf(env);const run: WatchRun = { asked: 0, rearmed: [], failed: [] };let after = "";for (;;) {const slugs = await directory.alarmWatchPage(after, PAGE);if (!slugs.length) break;for (let i = 0; i < slugs.length; i += AT_ONCE) {awaitPromise.all( slugs.slice(i, i + AT_ONCE).map(async (slug) => { run.asked++;try {const seen = await env.BUSINESS.get(env.BUSINESS.idFromName(slug)).alarmWatch(now);if (seen.rearmed) run.rearmed.push(slug); } catch (error) { run.failed.push(slug); console.error(`cottondb: alarm watch ${slug} failed: ${error instanceof Error ? error.message : String(error)}`); } }), ); } after = slugs[slugs.length - 1]!;if (slugs.length < PAGE) break; }if (run.rearmed.length || run.failed.length) console.log(`cottondb: alarm watch: ${run.asked} asked, re-armed ${run.rearmed.join(", ") || "none"}, failed ${run.failed.join(", ") || "none"}`);return run;}