OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
ralph(M10-007): ADR-021: cron is a property of the job; scheduler list derived from the registry; *CronSchedules deleted
ralph2deeb3436b3c
diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 91bf3e790..859fdb0a8 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -164,6 +164,7 @@ export type {AnyJob,EnqueueOptions,QueueDefinition,+ RepeatSchedule,} from './jobs/define';export type { JobEnvelope, JobMeta } from './jobs/envelope';export type { CountableQueue } from './jobs/jobs.metrics';@@ -178,6 +179,7 @@ export type {export {CRON_SCHEDULES,PING_SCHEDULE,+ schedulersFromRegistry,startSchedulers,} from './jobs/schedulers';export type {diff --git a/packages/core/src/jobs.registry.test.ts b/packages/core/src/jobs.registry.test.tsindex 68b765ec0..720720284 100644--- a/packages/core/src/jobs.registry.test.ts+++ b/packages/core/src/jobs.registry.test.ts@@ -1,7 +1,33 @@import { expect, test } from 'bun:test';-import { CRON_SCHEDULES, PING_SCHEDULE } from './jobs/schedulers';+import { PING_SCHEDULE, schedulersFromRegistry } from './jobs/schedulers';import { queues } from './jobs.registry';+// Byte-identity snapshot, mirroring+// `verification/golden/queue-keys/scheduler-ids.json` — see+// `jobs/schedulers.test.ts` for the matching id-level check.+const GOLDEN_SCHEDULER_IDS = [+ 'cohortRefresh',+ 'dataHealth',+ 'delete',+ 'flushEvents',+ 'flushExports',+ 'flushGroups',+ 'flushProfileBackfill',+ 'flushProfiles',+ 'flushReplay',+ 'flushSessions',+ 'gscSync',+ 'insightCleanup',+ 'insightsDaily',+ 'onboarding',+ 'ping',+ 'salt',+ 'sessionReaper',+ 'sessionVacuum',+ 'weeklyDigest',+ 'windDown',+].sort();+// V1's `defaultJobOptions`, verbatim from packages/queue/src/queues.ts.// ADR-005's acceptance note: retry and retention values port as-is, defects// included — five queues that never retry, four with unbounded failed sets.@@ -221,24 +247,18 @@ test('the notification queue carries the notification module job', () => {});});-// M9-001: every scheduler id must land on a cron job that exists, or the-// scheduled run resolves (legacyCompat.cron maps `type` straight onto a job-// name) and then throws `has no job named ...`.-//-// M9-003 closed the last gap: `dataHealth`, `windDown` and `flushExports`-// moved off apps/worker into misc/organization/integration, so this is now an-// exact both-ways match and the exception list is gone. That is the-// machine-checkable half of "every job the old worker ran is served by the-// merged app under ROLE=worker".-test('every cron scheduler id has a handler, and vice versa', () => {- const declared = new Set(Object.keys(queues.cron.jobs));- const scheduled = [...CRON_SCHEDULES, PING_SCHEDULE].map(- (scheduler) => scheduler.id- );+// ADR-021 shrinks M9-001's bidirectional "every scheduler id has a handler,+// and vice versa" test to this single derivation-vs-golden comparison: the+// join between a cron job and its schedule is now enforced by the type+// system (`defineQueue`'s overload for the `cron` queue requires every job on+// it to declare `cron`, a schedule or explicit `null`), so the property that+// test policed can no longer drift — it is true by construction, not by a+// test walking both registries.+test('the cron queue derives the golden 20 scheduler ids', () => {+ const derivedIds = [+ ...schedulersFromRegistry(queues.cron).map((s) => s.id),+ PING_SCHEDULE.id,+ ].sort();- expect(scheduled.filter((id) => !declared.has(id)).sort()).toEqual([]);- // The reverse direction: nothing is registered that nothing ever schedules.- expect(- [...declared].filter((name) => !scheduled.includes(name)).sort()- ).toEqual([]);+ expect(derivedIds).toEqual(GOLDEN_SCHEDULER_IDS);});diff --git a/packages/core/src/jobs/define.ts b/packages/core/src/jobs/define.tsindex 2bb8e5551..8f152a82f 100644--- a/packages/core/src/jobs/define.ts+++ b/packages/core/src/jobs/define.ts@@ -61,6 +61,13 @@ export interface WorkerSettings {limiter?: { max: number; duration: number };}+/**+ * `{ pattern }` is a cron expression, `{ every }` a fixed interval in ms —+ * BullMQ's own job-scheduler repeat options, narrowed to the two forms this+ * codebase uses.+ */+export type RepeatSchedule = { pattern: string } | { every: number };+export interface JobDefinition<TPayload extends z.ZodType> {/*** Must survive a JSON round-trip: BullMQ stores job data as JSON, so a@@ -74,6 +81,14 @@ export interface JobDefinition<TPayload extends z.ZodType> {}) => Promise<void>;/** Defaults for this job alone, over the queue's. */options?: EnqueueOptions;+ /**+ * ADR-021: a property of the job, not a parallel registry. Only meaningful+ * on the `cron` queue, where `defineQueue`'s overload for that name makes+ * this field required — `null` for a job the scheduler never enqueues+ * (e.g. `ping`, gated elsewhere), a schedule otherwise. Leaving it absent+ * is a type error there, on purpose (the three-state rule).+ */+ cron?: RepeatSchedule | null;}/**@@ -85,6 +100,7 @@ export interface AnyJob {payload: z.ZodType;handler: (args: never) => Promise<void>;options?: EnqueueOptions;+ cron?: RepeatSchedule | null;}/** Stamped on by `defineQueue`, so a job value alone is enough to enqueue it. */@@ -141,10 +157,25 @@ export type Producers<TQueues extends QueueMap> = {};};-export function defineJob<TPayload extends z.ZodType>(- definition: JobDefinition<TPayload>-): JobDefinition<TPayload> {- return definition;+/**+ * `TCron` is inferred from the literal `cron` value passed in, not fixed to+ * `JobDefinition`'s optional field — that is what lets a caller's omission of+ * `cron` stay a genuinely absent property (rather than an optional one that+ * happens to be `undefined`), which `defineQueue`'s `cron`-queue overload+ * relies on to reject it at the type level (ADR-021).+ */+export function defineJob<+ TPayload extends z.ZodType,+ TCron extends RepeatSchedule | null | undefined = undefined,+>(definition: {+ payload: TPayload;+ handler: JobDefinition<TPayload>['handler'];+ options?: EnqueueOptions;+ cron?: TCron;+}): JobDefinition<TPayload> &+ (undefined extends TCron ? unknown : { cron: TCron }) {+ return definition as JobDefinition<TPayload> &+ (undefined extends TCron ? unknown : { cron: TCron });}/**@@ -152,9 +183,18 @@ export function defineJob<TPayload extends z.ZodType>(* exists until `createProducers` or `startWorkers` is handed the registry,* which is what keeps importing a module free of side effects and lets one* declaration serve a producer-only process and a worker one.+ *+ * `TJobs`'s bound depends on `TName`: on the `cron` queue every job must carry+ * a `cron` field (a schedule, or `null` for on-demand) — the three-state rule+ * ADR-021 requires enforced at the type level, not by a registry test.*/-export function defineQueue<TJobs extends Record<string, AnyJob>>(- name: string,+export function defineQueue<+ TName extends string,+ TJobs extends TName extends 'cron'+ ? Record<string, AnyJob & { cron: RepeatSchedule | null }>+ : Record<string, AnyJob>,+>(+ name: TName,config: {defaults?: QueueDefaults;worker?: WorkerSettings;@@ -167,7 +207,7 @@ export function defineQueue<TJobs extends Record<string, AnyJob>>(jobName,{ ...job, queue: name, name: jobName },])- ) as QueueDefinition<TJobs>['jobs'];+ ) as unknown as QueueDefinition<TJobs>['jobs'];return {name,diff --git a/packages/core/src/jobs/schedulers.ts b/packages/core/src/jobs/schedulers.tsindex ea2462cc8..c3cf1a1de 100644--- a/packages/core/src/jobs/schedulers.ts+++ b/packages/core/src/jobs/schedulers.ts@@ -3,23 +3,18 @@// what makes it idempotent across replicas: two processes upserting the same// id with the same repeat options at boot converge on one scheduler, with no// locking of our own needed.+//+// ADR-021: the scheduler list is no longer a second, hand-maintained registry+// — it is derived from the `cron` queue's own jobs, so a job and its schedule+// cannot drift apart. This file imports no module; `schedulersFromRegistry`+// takes the registry's `cron` queue as data.+import { queues } from '../jobs.registry';import type { Logger } from '../logger';-import { cohortCronSchedules } from '../modules/cohort/cohort.jobs';-import { eventCronSchedules } from '../modules/event/event.jobs';-import { groupCronSchedules } from '../modules/group/group.jobs';-import { gscCronSchedules } from '../modules/gsc/gsc.jobs';-import { insightCronSchedules } from '../modules/insight/insight.jobs';-import { integrationCronSchedules } from '../modules/integration/integration.jobs';-import { miscCronSchedules } from '../modules/misc/misc.jobs';-import { onboardingCronSchedules } from '../modules/onboarding/onboarding.jobs';-import { organizationCronSchedules } from '../modules/organization/organization.jobs';-import { profileCronSchedules } from '../modules/profile/profile.jobs';-import { saltCronSchedules } from '../modules/salt/salt.jobs';-import { sessionCronSchedules } from '../modules/session/session.jobs';+import type { AnyJob, QueueDefinition, RepeatSchedule } from './define';import { wrap } from './envelope';-export type RepeatSchedule = { pattern: string } | { every: number };+export type { RepeatSchedule } from './define';export interface SchedulerDefinition {/** V1 scheduler id: the job-scheduler key, and — via BullMQ's own default@@ -61,55 +56,43 @@ const CONFLICT_JOB_STATES = [const CONFLICT_ERROR_SUBSTRING = 'job ID already exists';+/**+ * Derives the scheduler list from the `cron` queue's own jobs (ADR-021) — the+ * join is by construction now, not by a second registry kept in sync by a+ * test. A job's `cron` is `RepeatSchedule` (always-on), `null` (on-demand —+ * e.g. `ping`, scheduled conditionally elsewhere), or, on the `cron` queue,+ * never simply absent: `defineQueue`'s overload for that queue name makes+ * omitting it a type error. This throw is the defensive fallback for the one+ * hole the type system can't close — a value forced through with `as any`.+ */+export function schedulersFromRegistry(+ cronQueue: QueueDefinition<Record<string, AnyJob>>+): SchedulerDefinition[] {+ const schedulers: SchedulerDefinition[] = [];++ for (const [jobName, job] of Object.entries(cronQueue.jobs)) {+ if (!('cron' in job)) {+ throw new Error(+ `cron queue job "${jobName}" has no "cron" field — declare a schedule or "cron: null" for on-demand (ADR-021)`+ );+ }+ if (job.cron) {+ schedulers.push({ id: jobName, schedule: job.cron });+ }+ }++ return schedulers.sort((a, b) => a.id.localeCompare(b.id));+}+// V1's exact 19 always-on scheduler ids and cadences// (apps/worker/src/boot-cron.ts). `ping` is the 20th and is conditional —// see `PING_SCHEDULE` and `startSchedulers`.-export const CRON_SCHEDULES: readonly SchedulerDefinition[] = [- // salt — owned by the salt module, declared next to its jobs- // (modules/salt/salt.jobs.ts).- ...saltCronSchedules,- // delete / windDown — owned by the organization module, declared next to its- // jobs (modules/organization/organization.jobs.ts).- ...organizationCronSchedules,- // flushEvents — owned by the event module, declared next to its jobs- // (modules/event/event.jobs.ts).- ...eventCronSchedules,- // flushProfiles / flushProfileBackfill — owned by the profile module,- // declared next to its jobs (modules/profile/profile.jobs.ts).- ...profileCronSchedules,- // flushSessions / flushReplay — owned by the session module, spread in- // below alongside sessionReaper / sessionVacuum.- // flushGroups — owned by the group module, declared next to its jobs- // (modules/group/group.jobs.ts).- ...groupCronSchedules,- // onboarding — owned by the onboarding module, declared next to its jobs- // (modules/onboarding/onboarding.jobs.ts).- ...onboardingCronSchedules,- // gscSync — owned by the gsc module, declared next to its jobs- // (modules/gsc/gsc.jobs.ts).- ...gscCronSchedules,- // cohortRefresh — owned by the cohort module, declared next to its jobs- // (modules/cohort/cohort.jobs.ts).- ...cohortCronSchedules,- // flushSessions / flushReplay / sessionReaper / sessionVacuum — owned by- // the session module, declared next to its jobs- // (modules/session/session.jobs.ts).- ...sessionCronSchedules,- // insightsDaily / insightCleanup / weeklyDigest — owned by the insight- // module, declared next to its jobs (modules/insight/insight.jobs.ts).- ...insightCronSchedules,- // dataHealth — owned by the misc module, declared next to its jobs- // (modules/misc/misc.jobs.ts). ADR-005's acceptance note completed the- // ownership map for this and the two below; M9-003 moved the handlers off- // apps/worker, so each now sits with its owner rather than inline here.- ...miscCronSchedules,- // flushExports — owned by the integration module, declared next to its jobs- // (modules/integration/integration.jobs.ts).- ...integrationCronSchedules,-];+export const CRON_SCHEDULES: readonly SchedulerDefinition[] =+ schedulersFromRegistry(queues.cron);// V1 gated this on `SELF_HOSTED && NODE_ENV === 'production'`-// (apps/worker/src/boot-cron.ts:133).+// (apps/worker/src/boot-cron.ts:133). `misc.jobs.ts`'s `ping` job declares+// `cron: null` for exactly this reason — it is never in `CRON_SCHEDULES`.export const PING_SCHEDULE: SchedulerDefinition = {id: 'ping',schedule: { pattern: '0 0 * * *' },diff --git a/packages/core/src/modules/cohort/cohort.jobs.test.ts b/packages/core/src/modules/cohort/cohort.jobs.test.tsindex c8af1b733..db362e437 100644--- a/packages/core/src/modules/cohort/cohort.jobs.test.ts+++ b/packages/core/src/modules/cohort/cohort.jobs.test.ts@@ -7,11 +7,7 @@ import { createRecordingProducers } from '../../jobs/testing';import { queues } from '../../jobs.registry';import type { Logger } from '../../logger';import type { Services } from '../../services';-import {- cohortCronJobs,- cohortCronSchedules,- cohortQueueJobs,-} from './cohort.jobs';+import { cohortCronJobs, cohortQueueJobs } from './cohort.jobs';import type { CohortService } from './cohort.service';function stubLogger(): Logger {@@ -125,9 +121,10 @@ test('cohortRefresh fans out one enqueueCompute per non-static cohort', async ()});// Byte-identity with the id/cadence schedulers.test.ts's golden snapshot pins-// (apps/worker/src/boot-cron.ts).+// (apps/worker/src/boot-cron.ts). The cadence now lives on the job itself+// (ADR-021), so this reads it straight off the registry.test('the cohort cron fragment carries V1 id and cadence unchanged', () => {- expect(cohortCronSchedules).toEqual([- { id: 'cohortRefresh', schedule: { pattern: '*/30 * * * *' } },- ]);+ expect(queues.cron.jobs.cohortRefresh.cron).toEqual({+ pattern: '*/30 * * * *',+ });});diff --git a/packages/core/src/modules/cohort/cohort.jobs.ts b/packages/core/src/modules/cohort/cohort.jobs.tsindex 460543640..9d001c0fb 100644--- a/packages/core/src/modules/cohort/cohort.jobs.ts+++ b/packages/core/src/modules/cohort/cohort.jobs.ts@@ -1,16 +1,14 @@// Ported from apps/worker/src/jobs/cohort.compute.ts + cron.cohort-refresh.ts-// (M5-003).+// (M5-003); the schedule moved onto the job at ADR-021 (M10-007).//// `cohortCompute` is this module's own queue — registry key `cohortCompute`// EXACTLY (ADR-005's acceptance note: no env rename, so// COHORTCOMPUTE_CONCURRENCY keeps working). `cohortRefresh` is a cron-// fragment: declared here, spread into the ONE `cron` queue by-// jobs.registry.ts and into `CRON_SCHEDULES` by jobs/schedulers.ts. Scheduler+// fragment, spread into the ONE `cron` queue by jobs.registry.ts. Scheduler// id and cadence are V1's, unchanged (apps/worker/src/boot-cron.ts).import { z } from 'zod';import { defineJob } from '../../jobs/define';-import type { SchedulerDefinition } from '../../jobs/schedulers';const cohortComputePayload = z.object({ cohortId: z.string() });@@ -30,6 +28,7 @@ export const cohortCronJobs = {// (apps/worker/src/jobs/cron.cohort-refresh.ts).cohortRefresh: defineJob({payload: z.null(),+ cron: { pattern: '*/30 * * * *' },handler: async ({ ctx }) => {const cohortIds = await ctx.services.cohort.listRefreshableCohortIds();@@ -39,8 +38,3 @@ export const cohortCronJobs = {},}),};--/** This module's fragment of `CRON_SCHEDULES` — id and cadence unchanged. */-export const cohortCronSchedules: readonly SchedulerDefinition[] = [- { id: 'cohortRefresh', schedule: { pattern: '*/30 * * * *' } },-];diff --git a/packages/core/src/modules/event/event.jobs.ts b/packages/core/src/modules/event/event.jobs.tsindex 34d495944..9a7132b3e 100644--- a/packages/core/src/modules/event/event.jobs.ts+++ b/packages/core/src/modules/event/event.jobs.ts@@ -1,11 +1,11 @@// Ported from apps/worker/src/jobs/cron.ts's `flushEvents` case +-// boot-cron.ts's `flush`/`flushEvents` schedule (M8-004). The event buffer-// (Redis → ClickHouse `events`) is this module's domain, so its flush cron-// fragment lives here rather than on the ingest module that merely writes to it.+// boot-cron.ts's `flush`/`flushEvents` schedule (M8-004); the schedule moved+// onto the job at ADR-021 (M10-007). The event buffer (Redis → ClickHouse+// `events`) is this module's domain, so its flush cron fragment lives here+// rather than on the ingest module that merely writes to it.import { z } from 'zod';import { defineJob } from '../../jobs/define';-import type { SchedulerDefinition } from '../../jobs/schedulers';const FLUSH_EVENTS_INTERVAL_MS = 10_000;@@ -13,13 +13,9 @@ const FLUSH_EVENTS_INTERVAL_MS = 10_000;export const eventCronJobs = {flushEvents: defineJob({payload: z.null(),+ cron: { every: FLUSH_EVENTS_INTERVAL_MS },handler: async ({ ctx }) => {await ctx.buffers.event.tryFlush({ trigger: 'cron' });},}),};--/** This module's fragment of `CRON_SCHEDULES` — id and cadence unchanged. */-export const eventCronSchedules: readonly SchedulerDefinition[] = [- { id: 'flushEvents', schedule: { every: FLUSH_EVENTS_INTERVAL_MS } },-];diff --git a/packages/core/src/modules/group/group.jobs.ts b/packages/core/src/modules/group/group.jobs.tsindex 8eae2a5ba..a563b1c30 100644--- a/packages/core/src/modules/group/group.jobs.ts+++ b/packages/core/src/modules/group/group.jobs.ts@@ -1,10 +1,10 @@// Ported from apps/worker/src/jobs/cron.ts's `flushGroups` case +-// boot-cron.ts's `flush`/`flushGroups` schedule (M8-004). The group buffer-// (Redis → ClickHouse `groups`) is this module's domain.+// boot-cron.ts's `flush`/`flushGroups` schedule (M8-004); the schedule moved+// onto the job at ADR-021 (M10-007). The group buffer (Redis → ClickHouse+// `groups`) is this module's domain.import { z } from 'zod';import { defineJob } from '../../jobs/define';-import type { SchedulerDefinition } from '../../jobs/schedulers';const FLUSH_GROUPS_INTERVAL_MS = 10_000;@@ -12,13 +12,9 @@ const FLUSH_GROUPS_INTERVAL_MS = 10_000;export const groupCronJobs = {flushGroups: defineJob({payload: z.null(),+ cron: { every: FLUSH_GROUPS_INTERVAL_MS },handler: async ({ ctx }) => {await ctx.buffers.group.tryFlush({ trigger: 'cron' });},}),};--/** This module's fragment of `CRON_SCHEDULES` — id and cadence unchanged. */-export const groupCronSchedules: readonly SchedulerDefinition[] = [- { id: 'flushGroups', schedule: { every: FLUSH_GROUPS_INTERVAL_MS } },-];diff --git a/packages/core/src/modules/gsc/gsc.jobs.test.ts b/packages/core/src/modules/gsc/gsc.jobs.test.tsindex a865306c1..71ebd1e26 100644--- a/packages/core/src/modules/gsc/gsc.jobs.test.ts+++ b/packages/core/src/modules/gsc/gsc.jobs.test.ts@@ -7,7 +7,7 @@ import { createRecordingProducers } from '../../jobs/testing';import { queues } from '../../jobs.registry';import type { Logger } from '../../logger';import type { Services } from '../../services';-import { gscCronJobs, gscCronSchedules, gscQueueJobs } from './gsc.jobs';+import { gscCronJobs, gscQueueJobs } from './gsc.jobs';import type { GscService } from './gsc.service';function stubLogger(): Logger {@@ -199,9 +199,8 @@ test('gscSync fans out one gscProjectSync enqueue per connected project', async});// Byte-identity with the id/cadence schedulers.test.ts's golden snapshot pins-// (apps/worker/src/boot-cron.ts).+// (apps/worker/src/boot-cron.ts). The cadence now lives on the job itself+// (ADR-021), so this reads it straight off the registry.test('the gsc cron fragment carries V1 id and cadence unchanged', () => {- expect(gscCronSchedules).toEqual([- { id: 'gscSync', schedule: { pattern: '0 3 * * *' } },- ]);+ expect(queues.cron.jobs.gscSync.cron).toEqual({ pattern: '0 3 * * *' });});diff --git a/packages/core/src/modules/gsc/gsc.jobs.ts b/packages/core/src/modules/gsc/gsc.jobs.tsindex bb7ee11cb..ceab165f8 100644--- a/packages/core/src/modules/gsc/gsc.jobs.ts+++ b/packages/core/src/modules/gsc/gsc.jobs.ts@@ -1,15 +1,13 @@// Ported from apps/worker/src/jobs/gsc.ts + boot-cron.ts's gscSync schedule-// (M5-002).+// (M5-002); the schedule moved onto the job at ADR-021 (M10-007).//// `gscProjectSync` / `gscProjectBackfill` are this module's own queue (`gsc`-// in the registry — ADR-005's registry key). `gscSync` is a cron fragment:-// declared here, spread into the ONE `cron` queue by jobs.registry.ts and-// into `CRON_SCHEDULES` by jobs/schedulers.ts. Scheduler id and cadence are-// V1's, unchanged (apps/worker/src/boot-cron.ts).+// in the registry — ADR-005's registry key). `gscSync` is a cron fragment,+// spread into the ONE `cron` queue by jobs.registry.ts. Scheduler id and+// cadence are V1's, unchanged (apps/worker/src/boot-cron.ts).import { z } from 'zod';import { defineJob } from '../../jobs/define';-import type { SchedulerDefinition } from '../../jobs/schedulers';const gscProjectPayload = z.object({ projectId: z.string() });@@ -36,6 +34,7 @@ export const gscCronJobs = {// Matches V1 (apps/worker/src/jobs/gsc.ts's gscSyncAllJob).gscSync: defineJob({payload: z.null(),+ cron: { pattern: '0 3 * * *' },handler: async ({ ctx }) => {const connections = await ctx.services.gsc.listConnectionsForSync();@@ -45,8 +44,3 @@ export const gscCronJobs = {},}),};--/** This module's fragment of `CRON_SCHEDULES` — id and cadence unchanged. */-export const gscCronSchedules: readonly SchedulerDefinition[] = [- { id: 'gscSync', schedule: { pattern: '0 3 * * *' } },-];diff --git a/packages/core/src/modules/insight/insight.jobs.test.ts b/packages/core/src/modules/insight/insight.jobs.test.tsindex 843e0b321..13abcefd4 100644--- a/packages/core/src/modules/insight/insight.jobs.test.ts+++ b/packages/core/src/modules/insight/insight.jobs.test.ts@@ -7,11 +7,7 @@ import { createRecordingProducers } from '../../jobs/testing';import { queues } from '../../jobs.registry';import type { Logger } from '../../logger';import type { Services } from '../../services';-import {- insightCronJobs,- insightCronSchedules,- insightQueueJobs,-} from './insight.jobs';+import { insightCronJobs, insightQueueJobs } from './insight.jobs';import type { InsightService } from './insight.service';function stubLogger(): Logger {@@ -226,11 +222,16 @@ test('weeklyDigest delegates to sendWeeklyDigests', async () => {});// Byte-identity with the ids/cadences schedulers.test.ts's golden snapshot-// pins (apps/worker/src/boot-cron.ts).+// pins (apps/worker/src/boot-cron.ts). The cadences now live on the jobs+// themselves (ADR-021), so this reads them straight off the registry.test('the insight cron fragment carries V1 ids and cadences unchanged', () => {- expect(insightCronSchedules).toEqual([- { id: 'insightsDaily', schedule: { pattern: '0 2 * * *' } },- { id: 'insightCleanup', schedule: { pattern: '30 4 * * *' } },- { id: 'weeklyDigest', schedule: { pattern: '0 8 * * 1' } },- ]);+ expect(queues.cron.jobs.insightsDaily.cron).toEqual({+ pattern: '0 2 * * *',+ });+ expect(queues.cron.jobs.insightCleanup.cron).toEqual({+ pattern: '30 4 * * *',+ });+ expect(queues.cron.jobs.weeklyDigest.cron).toEqual({+ pattern: '0 8 * * 1',+ });});diff --git a/packages/core/src/modules/insight/insight.jobs.ts b/packages/core/src/modules/insight/insight.jobs.tsindex ec145addd..0a68c2e36 100644--- a/packages/core/src/modules/insight/insight.jobs.ts+++ b/packages/core/src/modules/insight/insight.jobs.ts@@ -1,15 +1,14 @@// Ported from apps/worker/src/jobs/insights.ts, insights-enrich.ts,-// cron.insight-cleanup.ts and cron.weekly-digest.ts (M5-001).+// cron.insight-cleanup.ts and cron.weekly-digest.ts (M5-001); the schedules+// moved onto the jobs at ADR-021 (M10-007).//// `insightsProject` is this module's own queue (`insights` in the registry —// ADR-005's registry key). `insightsDaily` / `insightCleanup` / `weeklyDigest`-// are cron fragments: declared here, spread into the ONE `cron` queue by-// jobs.registry.ts and into `CRON_SCHEDULES` by jobs/schedulers.ts. Scheduler-// ids and cadences are V1's, unchanged (apps/worker/src/boot-cron.ts).+// are cron fragments, spread into the ONE `cron` queue by jobs.registry.ts.+// Scheduler ids and cadences are V1's, unchanged (apps/worker/src/boot-cron.ts).import { z } from 'zod';import { defineJob } from '../../jobs/define';-import type { SchedulerDefinition } from '../../jobs/schedulers';const insightsProjectPayload = z.object({projectId: z.string(),@@ -33,6 +32,7 @@ export const insightCronJobs = {// (apps/worker/src/jobs/insights.ts).insightsDaily: defineJob({payload: z.null(),+ cron: { pattern: '0 2 * * *' },handler: async ({ ctx }) => {const date = new Date().toISOString().slice(0, 10);const candidates =@@ -46,26 +46,21 @@ export const insightCronJobs = {},}),+ // Daily 04:30 UTC — prunes stale insights/events.insightCleanup: defineJob({payload: z.null(),+ cron: { pattern: '30 4 * * *' },handler: async ({ ctx }) => {await ctx.services.insight.cleanupStaleInsights();},}),+ // Mondays 08:00 UTC — weekly analytics digest email.weeklyDigest: defineJob({payload: z.null(),+ cron: { pattern: '0 8 * * 1' },handler: async ({ ctx }) => {await ctx.services.insight.sendWeeklyDigests();},}),};--/** This module's fragment of `CRON_SCHEDULES` — ids and cadences unchanged. */-export const insightCronSchedules: readonly SchedulerDefinition[] = [- { id: 'insightsDaily', schedule: { pattern: '0 2 * * *' } },- // Daily 04:30 UTC — prunes stale insights/events.- { id: 'insightCleanup', schedule: { pattern: '30 4 * * *' } },- // Mondays 08:00 UTC — weekly analytics digest email.- { id: 'weeklyDigest', schedule: { pattern: '0 8 * * 1' } },-];diff --git a/packages/core/src/modules/integration/integration.jobs.ts b/packages/core/src/modules/integration/integration.jobs.tsindex 09d74fc9c..b3207e38c 100644--- a/packages/core/src/modules/integration/integration.jobs.ts+++ b/packages/core/src/modules/integration/integration.jobs.ts@@ -1,14 +1,13 @@// Ported from apps/worker/src/jobs/cron.flush-exports.ts + boot-cron.ts's// `flushExports` schedule (M9-003, the wave that deletes apps/worker).-// ADR-005's acceptance note gives `flushExports` to this module.+// ADR-005's acceptance note gives `flushExports` to this module; the+// schedule moved onto the job at ADR-021 (M10-007).//-// `flushExports` is this module's fragment of the ONE `cron` queue's jobs:-// declared here, spread into jobs.registry.ts and into `CRON_SCHEDULES` by-// jobs/schedulers.ts. Scheduler id and cadence are V1's, unchanged.+// `flushExports` is this module's fragment of the ONE `cron` queue's jobs,+// spread into jobs.registry.ts. Scheduler id and cadence are V1's, unchanged.import { z } from 'zod';import { defineJob } from '../../jobs/define';-import type { SchedulerDefinition } from '../../jobs/schedulers';import {type ExportClickhouse,type ExportDb,@@ -22,6 +21,7 @@ const FLUSH_EXPORTS_INTERVAL_MS = 60_000;export const integrationCronJobs = {flushExports: defineJob({payload: z.null(),+ cron: { every: FLUSH_EXPORTS_INTERVAL_MS },handler: async ({ ctx }) => {// Lazy: the registry pulls the S3 and GCS SDKs, ~270ms of module// evaluation (measured, Bun 1.4.0) that a static edge here would add to@@ -40,8 +40,3 @@ export const integrationCronJobs = {},}),};--/** This module's fragment of `CRON_SCHEDULES` — id and cadence unchanged. */-export const integrationCronSchedules: readonly SchedulerDefinition[] = [- { id: 'flushExports', schedule: { every: FLUSH_EXPORTS_INTERVAL_MS } },-];diff --git a/packages/core/src/modules/misc/misc.jobs.ts b/packages/core/src/modules/misc/misc.jobs.tsindex 18ebd6809..a2dd61d49 100644--- a/packages/core/src/modules/misc/misc.jobs.ts+++ b/packages/core/src/modules/misc/misc.jobs.ts@@ -4,14 +4,14 @@//// `ping` is this module's fragment of the ONE `cron` queue's jobs: declared// here and spread into jobs.registry.ts, same shape as-// organization.jobs.ts's `delete` (M6-001). Its schedule is NOT spread into-// `CRON_SCHEDULES` here — `PING_SCHEDULE` in jobs/schedulers.ts already-// declares it, conditionally on `SELF_HOSTED && production`, because it is-// the one scheduler that isn't always-on.+// organization.jobs.ts's `delete` (M6-001). Its `cron` is explicitly `null`+// (ADR-021's on-demand state, not "always scheduled") — `PING_SCHEDULE` in+// jobs/schedulers.ts adds it separately, conditionally on+// `SELF_HOSTED && production`, because it is the one scheduler that isn't+// always-on.import { z } from 'zod';import { defineJob } from '../../jobs/define';-import type { SchedulerDefinition } from '../../jobs/schedulers';import { type DataHealthDb, runDataHealthCron } from './src/data-health';/** Daily 07:30 UTC — V1's cadence (apps/worker/src/boot-cron.ts). */@@ -21,12 +21,14 @@ const DATA_HEALTH_CRON = '30 7 * * *';export const miscCronJobs = {ping: defineJob({payload: z.null(),+ cron: null,handler: async ({ ctx }) => {await ctx.services.misc.runPingCron();},}),dataHealth: defineJob({payload: z.null(),+ cron: { pattern: DATA_HEALTH_CRON },handler: async ({ ctx }) => {await runDataHealthCron({db: ctx.db as unknown as DataHealthDb,@@ -42,8 +44,3 @@ export const miscCronJobs = {},}),};--/** This module's fragment of `CRON_SCHEDULES` — id and cadence unchanged. */-export const miscCronSchedules: readonly SchedulerDefinition[] = [- { id: 'dataHealth', schedule: { pattern: DATA_HEALTH_CRON } },-];diff --git a/packages/core/src/modules/onboarding/onboarding.jobs.test.ts b/packages/core/src/modules/onboarding/onboarding.jobs.test.tsindex c3afd14d8..62dc78cca 100644--- a/packages/core/src/modules/onboarding/onboarding.jobs.test.ts+++ b/packages/core/src/modules/onboarding/onboarding.jobs.test.ts@@ -7,7 +7,7 @@ import { createRecordingProducers } from '../../jobs/testing';import { queues } from '../../jobs.registry';import type { Logger } from '../../logger';import type { Services } from '../../services';-import { onboardingCronJobs, onboardingCronSchedules } from './onboarding.jobs';+import { onboardingCronJobs } from './onboarding.jobs';import type { OnboardingService } from './onboarding.service';function stubLogger(): Logger {@@ -116,9 +116,10 @@ test('onboarding does not log when the cron is a self-hosted no-op', async () =>});// Byte-identity with the id/cadence schedulers.test.ts's golden snapshot pins-// (apps/worker/src/boot-cron.ts).+// (apps/worker/src/boot-cron.ts). The cadence now lives on the job itself+// (ADR-021), so this reads it straight off the registry.test('the onboarding cron fragment carries V1 id and cadence unchanged', () => {- expect(onboardingCronSchedules).toEqual([- { id: 'onboarding', schedule: { pattern: '0 * * * *' } },- ]);+ expect(queues.cron.jobs.onboarding.cron).toEqual({+ pattern: '0 * * * *',+ });});diff --git a/packages/core/src/modules/onboarding/onboarding.jobs.ts b/packages/core/src/modules/onboarding/onboarding.jobs.tsindex 2e6456351..d0df44759 100644--- a/packages/core/src/modules/onboarding/onboarding.jobs.ts+++ b/packages/core/src/modules/onboarding/onboarding.jobs.ts@@ -1,19 +1,19 @@// Ported from apps/worker/src/jobs/cron.onboarding.ts + boot-cron.ts's-// `onboarding` schedule (M6-003).+// `onboarding` schedule (M6-003); the schedule moved onto the job at+// ADR-021 (M10-007).//-// `onboarding` is this module's fragment of the ONE `cron` queue's jobs:-// declared here, spread into jobs.registry.ts and into `CRON_SCHEDULES` by-// jobs/schedulers.ts. Scheduler id and cadence are V1's, unchanged+// `onboarding` is this module's fragment of the ONE `cron` queue's jobs,+// spread into jobs.registry.ts. Scheduler id and cadence are V1's, unchanged// (apps/worker/src/boot-cron.ts) — same shape as organization.jobs.ts (M6-001).import { z } from 'zod';import { defineJob } from '../../jobs/define';-import type { SchedulerDefinition } from '../../jobs/schedulers';/** This module's fragment of the `cron` queue's jobs. */export const onboardingCronJobs = {onboarding: defineJob({payload: z.null(),+ cron: { pattern: '0 * * * *' },handler: async ({ ctx }) => {const result = await ctx.services.onboarding.runOnboardingCron();if (result) {@@ -22,8 +22,3 @@ export const onboardingCronJobs = {},}),};--/** This module's fragment of `CRON_SCHEDULES` — id and cadence unchanged. */-export const onboardingCronSchedules: readonly SchedulerDefinition[] = [- { id: 'onboarding', schedule: { pattern: '0 * * * *' } },-];diff --git a/packages/core/src/modules/organization/organization.jobs.test.ts b/packages/core/src/modules/organization/organization.jobs.test.tsindex ba0cced5e..f1c640087 100644--- a/packages/core/src/modules/organization/organization.jobs.test.ts+++ b/packages/core/src/modules/organization/organization.jobs.test.ts@@ -7,10 +7,7 @@ import { createRecordingProducers } from '../../jobs/testing';import { queues } from '../../jobs.registry';import type { Logger } from '../../logger';import type { Services } from '../../services';-import {- organizationCronJobs,- organizationCronSchedules,-} from './organization.jobs';+import { organizationCronJobs } from './organization.jobs';import type { OrganizationService } from './organization.service';function stubLogger(): Logger {@@ -105,10 +102,9 @@ test('delete delegates to runDeleteCron', async () => {});// Byte-identity with the id/cadence schedulers.test.ts's golden snapshot pins-// (apps/worker/src/boot-cron.ts).+// (apps/worker/src/boot-cron.ts). The cadences now live on the jobs+// themselves (ADR-021), so this reads them straight off the registry.test('the organization cron fragment carries V1 id and cadence unchanged', () => {- expect(organizationCronSchedules).toEqual([- { id: 'delete', schedule: { pattern: '0 * * * *' } },- { id: 'windDown', schedule: { pattern: '0 * * * *' } },- ]);+ expect(queues.cron.jobs.delete.cron).toEqual({ pattern: '0 * * * *' });+ expect(queues.cron.jobs.windDown.cron).toEqual({ pattern: '0 * * * *' });});diff --git a/packages/core/src/modules/organization/organization.jobs.ts b/packages/core/src/modules/organization/organization.jobs.tsindex d51b7b0e5..4167c833a 100644--- a/packages/core/src/modules/organization/organization.jobs.ts+++ b/packages/core/src/modules/organization/organization.jobs.ts@@ -1,16 +1,15 @@// Ported from apps/worker/src/jobs/cron.delete.ts + boot-cron.ts's `delete`// schedule (M6-001); `windDown` joined it at M9-003, the wave that deletes-// apps/worker (ADR-005's acceptance note gives it to this module).+// apps/worker (ADR-005's acceptance note gives it to this module). The+// schedules moved onto the jobs at ADR-021 (M10-007).//-// `delete` is this module's fragment of the ONE `cron` queue's jobs:-// declared here, spread into jobs.registry.ts and into `CRON_SCHEDULES` by-// jobs/schedulers.ts. Scheduler id and cadence are V1's, unchanged+// `delete` is this module's fragment of the ONE `cron` queue's jobs, spread+// into jobs.registry.ts. Scheduler id and cadence are V1's, unchanged// (apps/worker/src/boot-cron.ts).import { z } from 'zod';import type { Ctx } from '../../context';import { defineJob } from '../../jobs/define';-import type { SchedulerDefinition } from '../../jobs/schedulers';import type { Logger } from '../../logger';import {buildWinBackHighlight,@@ -29,6 +28,7 @@ const WIND_DOWN_CRON = '0 * * * *';export const organizationCronJobs = {delete: defineJob({payload: z.null(),+ cron: { pattern: '0 * * * *' },handler: async ({ ctx }) => {const result = await ctx.services.organization.runDeleteCron();ctx.logger.info({ ...result }, 'Delete cron complete');@@ -36,6 +36,7 @@ export const organizationCronJobs = {}),windDown: defineJob({payload: z.null(),+ cron: { pattern: WIND_DOWN_CRON },handler: async ({ ctx }) => {await runWindDownCron(await windDownDeps(ctx, ctx.logger.child({ job: 'wind-down' }))@@ -79,9 +80,3 @@ async function windDownDeps(ctx: Ctx, logger: Logger): Promise<WindDownDeps> {buildHighlight: (input) => buildWinBackHighlight(input, highlight),};}--/** This module's fragment of `CRON_SCHEDULES` — id and cadence unchanged. */-export const organizationCronSchedules: readonly SchedulerDefinition[] = [- { id: 'delete', schedule: { pattern: '0 * * * *' } },- { id: 'windDown', schedule: { pattern: WIND_DOWN_CRON } },-];diff --git a/packages/core/src/modules/profile/profile.jobs.ts b/packages/core/src/modules/profile/profile.jobs.tsindex b111364e6..86d6a2809 100644--- a/packages/core/src/modules/profile/profile.jobs.ts+++ b/packages/core/src/modules/profile/profile.jobs.ts@@ -1,13 +1,12 @@// Ported from apps/worker/src/jobs/cron.ts's `flushProfiles` /-// `flushProfileBackfill` cases + boot-cron.ts's `flush` schedules (M8-004).-// Both buffers are this module's domain: the profile buffer-// (Redis → ClickHouse `profiles`) and the profile-backfill buffer, which-// patches a session's `profile_id` once a device is identified after the-// session row was already written.+// `flushProfileBackfill` cases + boot-cron.ts's `flush` schedules (M8-004);+// the schedules moved onto the jobs at ADR-021 (M10-007). Both buffers are+// this module's domain: the profile buffer (Redis → ClickHouse `profiles`)+// and the profile-backfill buffer, which patches a session's `profile_id`+// once a device is identified after the session row was already written.import { z } from 'zod';import { defineJob } from '../../jobs/define';-import type { SchedulerDefinition } from '../../jobs/schedulers';const FLUSH_PROFILES_INTERVAL_MS = 10_000;const FLUSH_PROFILE_BACKFILL_INTERVAL_MS = 30_000;@@ -16,23 +15,16 @@ const FLUSH_PROFILE_BACKFILL_INTERVAL_MS = 30_000;export const profileCronJobs = {flushProfiles: defineJob({payload: z.null(),+ cron: { every: FLUSH_PROFILES_INTERVAL_MS },handler: async ({ ctx }) => {await ctx.buffers.profile.tryFlush({ trigger: 'cron' });},}),flushProfileBackfill: defineJob({payload: z.null(),+ cron: { every: FLUSH_PROFILE_BACKFILL_INTERVAL_MS },handler: async ({ ctx }) => {await ctx.buffers.profileBackfill.tryFlush({ trigger: 'cron' });},}),};--/** This module's fragment of `CRON_SCHEDULES` — ids and cadences unchanged. */-export const profileCronSchedules: readonly SchedulerDefinition[] = [- { id: 'flushProfiles', schedule: { every: FLUSH_PROFILES_INTERVAL_MS } },- {- id: 'flushProfileBackfill',- schedule: { every: FLUSH_PROFILE_BACKFILL_INTERVAL_MS },- },-];diff --git a/packages/core/src/modules/salt/salt.jobs.ts b/packages/core/src/modules/salt/salt.jobs.tsindex 2ba10c73e..a1702dfd4 100644--- a/packages/core/src/modules/salt/salt.jobs.ts+++ b/packages/core/src/modules/salt/salt.jobs.ts@@ -1,26 +1,20 @@// Ported from apps/worker/src/jobs/cron.salt.ts + boot-cron.ts's `salt`-// schedule (M8-004).+// schedule (M8-004); the schedule moved onto the job at ADR-021 (M10-007).//-// `salt` is this module's fragment of the ONE `cron` queue's jobs: declared-// here, spread into jobs.registry.ts and into `CRON_SCHEDULES` by-// jobs/schedulers.ts. Scheduler id and cadence are V1's, unchanged-// (apps/worker/src/boot-cron.ts).+// `salt` is this module's fragment of the ONE `cron` queue's jobs, spread+// into jobs.registry.ts. Its schedule is now derived straight from `cron`+// below — id and cadence are V1's, unchanged (apps/worker/src/boot-cron.ts).import { z } from 'zod';import { defineJob } from '../../jobs/define';-import type { SchedulerDefinition } from '../../jobs/schedulers';/** This module's fragment of the `cron` queue's jobs. */export const saltCronJobs = {salt: defineJob({payload: z.null(),+ cron: { pattern: '0 0 * * *' },handler: async ({ ctx }) => {await ctx.services.salt.rotateSalt();},}),};--/** This module's fragment of `CRON_SCHEDULES` — id and cadence unchanged. */-export const saltCronSchedules: readonly SchedulerDefinition[] = [- { id: 'salt', schedule: { pattern: '0 0 * * *' } },-];diff --git a/packages/core/src/modules/session/session.jobs.ts b/packages/core/src/modules/session/session.jobs.tsindex 5413feb44..913750b81 100644--- a/packages/core/src/modules/session/session.jobs.ts+++ b/packages/core/src/modules/session/session.jobs.ts@@ -1,15 +1,15 @@// Ported from apps/worker/src/jobs/sessions.ts (+ events.create-session-end.ts),// cron.session-reaper.ts and cron.session-vacuum.ts (M7-001); `flushSessions`// and `flushReplay` (apps/worker/src/jobs/cron.ts) join them here at M8-004.+// Schedules moved onto the jobs at ADR-021 (M10-007).//// `sessions` is this module's own queue — registry key and Redis name// `sessions`, job name `session`, `removeOnComplete: true`, concurrency 1, all// V1's (jobs.registry.ts). Its wire payload is `{ event, snapshot }`, which is// what `legacyCompat.sessions` maps V1's `{type:'createSessionEnd', payload,// snapshot}` onto. `flushSessions` / `flushReplay` / `sessionReaper` /-// `sessionVacuum` are cron fragments: declared here, spread into the ONE-// `cron` queue by jobs.registry.ts and into `CRON_SCHEDULES` by-// jobs/schedulers.ts, ids and cadences unchanged (apps/worker/src/boot-cron.ts).+// `sessionVacuum` are cron fragments, spread into the ONE `cron` queue by+// jobs.registry.ts, ids and cadences unchanged (apps/worker/src/boot-cron.ts).//// Every handler reads its clients off `ctx` — the buffers and the Redis// connection main.ts built once, and the ClickHouse client the job's own@@ -18,7 +18,6 @@import { z } from 'zod';import type { Ctx } from '../../context';import { defineJob } from '../../jobs/define';-import type { SchedulerDefinition } from '../../jobs/schedulers';import type { Logger } from '../../logger';import { chQuery } from '../../shared/ch-query';import {@@ -121,6 +120,7 @@ export const sessionQueueJobs = {export const sessionCronJobs = {flushSessions: defineJob({payload: z.null(),+ cron: { every: FLUSH_SESSIONS_INTERVAL_MS },handler: async ({ ctx }) => {await ctx.buffers.session.tryFlush({ trigger: 'cron' });},@@ -130,12 +130,14 @@ export const sessionCronJobs = {// too, next to the sessions it belongs to.flushReplay: defineJob({payload: z.null(),+ cron: { every: FLUSH_REPLAY_INTERVAL_MS },handler: async ({ ctx }) => {await ctx.buffers.replay.tryFlush({ trigger: 'cron' });},}),sessionReaper: defineJob({payload: z.null(),+ cron: { every: SESSION_REAPER_INTERVAL_MS },handler: async ({ ctx }) => {await reapIdleSessions({...sessionRuntimeFrom(ctx),@@ -147,6 +149,7 @@ export const sessionCronJobs = {}),sessionVacuum: defineJob({payload: z.null(),+ cron: { pattern: SESSION_VACUUM_CRON },handler: async ({ ctx }) => {await vacuumStaleSessions({...sessionRuntimeFrom(ctx),@@ -155,11 +158,3 @@ export const sessionCronJobs = {},}),};--/** This module's fragment of `CRON_SCHEDULES` — ids and cadences unchanged. */-export const sessionCronSchedules: readonly SchedulerDefinition[] = [- { id: 'flushSessions', schedule: { every: FLUSH_SESSIONS_INTERVAL_MS } },- { id: 'flushReplay', schedule: { every: FLUSH_REPLAY_INTERVAL_MS } },- { id: 'sessionReaper', schedule: { every: SESSION_REAPER_INTERVAL_MS } },- { id: 'sessionVacuum', schedule: { pattern: SESSION_VACUUM_CRON } },-];