diff --git a/apps/docs/content/docs/platform/enterprise/data-drains.mdx b/apps/docs/content/docs/platform/enterprise/data-drains.mdx index 8bf882fe94a..953290aedeb 100644 --- a/apps/docs/content/docs/platform/enterprise/data-drains.mdx +++ b/apps/docs/content/docs/platform/enterprise/data-drains.mdx @@ -21,11 +21,11 @@ The detail page shows the last ten runs with status, trigger, time, row count, b ## Cadence and latency -The scheduler checks for due drains every 15 minutes. Each drain runs only when its own cadence has elapsed since its last claimed run, minus a small buffer for scheduler drift: +The scheduler checks for due drains every 15 minutes. Each drain runs only when its own cadence has elapsed since its last claimed run, minus a buffer for scheduler drift and queueing delay: | Cadence | Runs when the last run started at least | | --- | --- | -| **Every 15 minutes** | 13 minutes ago | +| **Every 15 minutes** | 8 minutes ago | | **Every hour** | 55 minutes ago | | **Every day** | 23 hours 55 minutes ago | diff --git a/apps/sim/lib/api/contracts/schedules.ts b/apps/sim/lib/api/contracts/schedules.ts index 20804d87248..8488f842611 100644 --- a/apps/sim/lib/api/contracts/schedules.ts +++ b/apps/sim/lib/api/contracts/schedules.ts @@ -67,9 +67,10 @@ export const workflowScheduleRowSchema = z.object({ status: scheduleStatusSchema, /** * Why Sim turned the schedule off. Null while it is active or completed, and - * when a person disabled it. + * when a person disabled it. Defaulted so a new client tolerates an old + * server's response during rollout. */ - disabledReason: scheduleDisableReasonSchema.nullable(), + disabledReason: scheduleDisableReasonSchema.nullable().default(null), lastFailedAt: z.string().nullable(), /** * Legacy rows pre-dating the `sourceType` column can still surface in diff --git a/apps/sim/lib/data-drains/dispatcher.ts b/apps/sim/lib/data-drains/dispatcher.ts index 084b26d8f7b..034c6a0656c 100644 --- a/apps/sim/lib/data-drains/dispatcher.ts +++ b/apps/sim/lib/data-drains/dispatcher.ts @@ -16,14 +16,14 @@ const DAY_MS = 24 * HOUR_MS const DISPATCH_PAGE_SIZE = 200 /** - * Cron start times drift, so each cadence's buffer lets the next tick claim a - * drain whose preceding tick started late. The buffer stays well under the - * 15-minute cron tick so a drain is never claimed twice within one interval. * Cadence is measured from the worker's claim timestamp, independently of - * delivery duration. + * delivery duration. That claim lands after any cron drift and queueing delay, + * so each cadence's buffer lets the next tick still claim a drain whose last + * run started late. The buffer stays well under the 15-minute cron tick so a + * drain is never claimed twice within one interval. */ const CADENCE_SCHEDULE: Record = { - every_15_minutes: { intervalMs: 15 * MINUTE_MS, bufferMs: 2 * MINUTE_MS }, + every_15_minutes: { intervalMs: 15 * MINUTE_MS, bufferMs: 7 * MINUTE_MS }, hourly: { intervalMs: HOUR_MS, bufferMs: 5 * MINUTE_MS }, daily: { intervalMs: DAY_MS, bufferMs: 5 * MINUTE_MS }, } diff --git a/apps/sim/lib/data-drains/service.integration.ts b/apps/sim/lib/data-drains/service.integration.ts index 42bb6ea731a..3988b7f2341 100644 --- a/apps/sim/lib/data-drains/service.integration.ts +++ b/apps/sim/lib/data-drains/service.integration.ts @@ -452,8 +452,8 @@ describe('data drain durable delivery', () => { }) it.each([ - { cadence: 'every_15_minutes', ageMs: 13 * 60_000, due: false }, - { cadence: 'every_15_minutes', ageMs: 13 * 60_000 + 1000, due: true }, + { cadence: 'every_15_minutes', ageMs: 8 * 60_000, due: false }, + { cadence: 'every_15_minutes', ageMs: 8 * 60_000 + 1000, due: true }, { cadence: 'hourly', ageMs: 55 * 60_000, due: false }, { cadence: 'hourly', ageMs: 55 * 60_000 + 1000, due: true }, { cadence: 'daily', ageMs: (23 * 60 + 55) * 60_000, due: false }, @@ -480,6 +480,25 @@ describe('data drain durable delivery', () => { } ) + it('claims a 15-minute drain on the next tick when its run started minutes late', async () => { + const claimedAt = new Date(Date.now() - 5 * 60_000) + await db + .update(dataDrains) + .set({ scheduleCadence: 'every_15_minutes' }) + .where(eq(dataDrains.id, drainId)) + expect((await dispatchDueDrains(claimedAt)).dispatched).toBe(1) + await vi.waitFor(async () => { + const [run] = await db.select().from(dataDrainRuns).where(eq(dataDrainRuns.drainId, drainId)) + expect(run.status).toBe('success') + }) + const nextTick = new Date(claimedAt.getTime() + 15 * 60_000) + expect((await dispatchDueDrains(nextTick)).dispatched).toBe(1) + await vi.waitFor(async () => { + const runs = await db.select().from(dataDrainRuns).where(eq(dataDrainRuns.drainId, drainId)) + expect(runs.map((run) => run.status)).toEqual(['success', 'success']) + }) + }) + it('claims a due drain once when dispatchers race', async () => { await seedRows(1) const now = new Date()