|
4 | 4 | * On startup runs one catch-up sweep, then sweeps every 15 minutes. |
5 | 5 | * |
6 | 6 | * Sweep algorithm: |
7 | | - * 1. Load all `active` runs. |
8 | | - * 2. For each, compute today's "night date" in the run's earliest |
| 7 | + * 1. Reconcile run lifecycle state (`scheduled -> active`, |
| 8 | + * `active|paused -> completed`) based on each run's local date. |
| 9 | + * 2. Load all `active` runs. |
| 10 | + * 3. For each, compute today's "night date" in the run's earliest |
9 | 11 | * participant timezone, and the 21:00 anchor in UTC. |
10 | | - * 3. If the anchor has passed (or we're catching up after downtime), |
| 12 | + * 4. If the anchor has passed (or we're catching up after downtime), |
11 | 13 | * generate assignments for tonight via the assignments service — |
12 | 14 | * this is idempotent at the DB level. |
13 | | - * 4. Dispatch notifications for each freshly-inserted assignment. |
| 15 | + * 5. Dispatch notifications for each freshly-inserted assignment. |
14 | 16 | * |
15 | 17 | * Failures are logged but never thrown — the timer keeps ticking. |
16 | 18 | */ |
17 | 19 |
|
18 | 20 | import { ourmojiSchedulerLogger as logger } from "~/server/services/ourmoji/logger"; |
19 | | -import { listExperimentRunsByStatus, getParticipantsForRun } from "~/server/services/ourmoji/repository"; |
| 21 | +import { |
| 22 | + listExperimentRunsByStatus, |
| 23 | + getParticipantsForRun, |
| 24 | + updateExperimentRunStatus, |
| 25 | +} from "~/server/services/ourmoji/repository"; |
20 | 26 | import { generateAssignmentsForNight } from "~/server/services/ourmoji/assignments"; |
21 | 27 | import { dispatchAssignmentNotification } from "~/server/services/ourmoji/notifications"; |
22 | 28 | import { |
23 | 29 | computeNextAnchorUtc, |
| 30 | + localDateInTimezone, |
24 | 31 | nightDateForAnchor, |
25 | 32 | } from "~/server/services/ourmoji/schedule"; |
26 | 33 |
|
27 | 34 | const SWEEP_INTERVAL_MS = 15 * 60 * 1000; // 15 minutes |
28 | 35 |
|
| 36 | +async function reconcileRunStatuses(now: Date): Promise<void> { |
| 37 | + const scheduledRuns = await listExperimentRunsByStatus(["scheduled"]); |
| 38 | + for (const run of scheduledRuns) { |
| 39 | + const localToday = localDateInTimezone( |
| 40 | + now, |
| 41 | + run.earliestParticipantTimezone || "UTC", |
| 42 | + ); |
| 43 | + |
| 44 | + if (run.endDate < localToday) { |
| 45 | + await updateExperimentRunStatus(run.id, "completed"); |
| 46 | + logger.info("Completed expired scheduled run", { |
| 47 | + runId: run.id, |
| 48 | + localToday, |
| 49 | + }); |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + if (run.startDate <= localToday) { |
| 54 | + await updateExperimentRunStatus(run.id, "active"); |
| 55 | + logger.info("Activated scheduled run", { |
| 56 | + runId: run.id, |
| 57 | + localToday, |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + const liveRuns = await listExperimentRunsByStatus(["active", "paused"]); |
| 63 | + for (const run of liveRuns) { |
| 64 | + const localToday = localDateInTimezone( |
| 65 | + now, |
| 66 | + run.earliestParticipantTimezone || "UTC", |
| 67 | + ); |
| 68 | + if (run.endDate < localToday) { |
| 69 | + await updateExperimentRunStatus(run.id, "completed"); |
| 70 | + logger.info("Completed finished run", { |
| 71 | + runId: run.id, |
| 72 | + previousStatus: run.status, |
| 73 | + localToday, |
| 74 | + }); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
29 | 79 | async function sweep(): Promise<void> { |
30 | 80 | const now = new Date(); |
| 81 | + await reconcileRunStatuses(now); |
| 82 | + |
31 | 83 | const runs = await listExperimentRunsByStatus(["active"]); |
32 | 84 | if (runs.length === 0) { |
33 | 85 | logger.debug("Sweep tick — no active runs"); |
|
0 commit comments