Skip to content

feat: implement cron window spread backend - #4566

Open
carderne wants to merge 15 commits into
mainfrom
feat/cron-windows
Open

feat: implement cron window spread backend#4566
carderne wants to merge 15 commits into
mainfrom
feat/cron-windows

Conversation

@carderne

@carderne carderne commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Summary

Add the backend plumbing for cron schedule spreading/delaying.

Changes

  • New DB fields on Schedule and ScheduleInstance
  • Use queueTimestamp for the "effectiveAt" delayed start time, propagate it to Clickhouse TaskRun table
  • Disable fastpath for delayed jobs
  • Add schedule timing logic, API endpoints with windows, persistence
  • Calculate phase for every schedule, only persist when window is non-null
  • Additional o11y for phased rollout

Flag

  • default off, window will still be accepted by API inert
  • phase will still be calculated and logged but no effect

When switched on:

  • ALL schedules will get phased within a 60s delay
  • Schedules with a non-null window will get phased within that window

TODO:

@changeset-bot

changeset-bot Bot commented Aug 11, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 0044bbf

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

The change adds schedule windows with duration and percentage formats, validation, persistence, and API presentation. The schedule engine now calculates nominal and effective schedule times, derives deterministic phases, and propagates both timestamps through scheduling and triggering. Task runs now store nullable queue timestamps in ClickHouse. Queue handling records queue wait time and prevents fast-path delivery for future messages. Tests cover schedule timing, recovery, queue timestamps, and future queue eligibility.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the implementation and rollout flag, but it omits the required issue, checklist, testing, changelog, and screenshots sections. Add the template sections, complete the checklist, document testing steps, provide a changelog entry, and include an issue reference or state why none applies.
Docstring Coverage ⚠️ Warning Docstring coverage is 19.05% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: backend support for cron window spreading.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/cron-windows

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Aug 11, 2026

Copy link
Copy Markdown

Open in StackBlitz

@trigger.dev/build

npm i https://pkg.pr.new/@trigger.dev/build@beb6074

trigger.dev

npm i https://pkg.pr.new/trigger.dev@beb6074

@trigger.dev/core

npm i https://pkg.pr.new/@trigger.dev/core@beb6074

@trigger.dev/python

npm i https://pkg.pr.new/@trigger.dev/python@beb6074

@trigger.dev/react-hooks

npm i https://pkg.pr.new/@trigger.dev/react-hooks@beb6074

@trigger.dev/redis-worker

npm i https://pkg.pr.new/@trigger.dev/redis-worker@beb6074

@trigger.dev/rsc

npm i https://pkg.pr.new/@trigger.dev/rsc@beb6074

@trigger.dev/schema-to-json

npm i https://pkg.pr.new/@trigger.dev/schema-to-json@beb6074

@trigger.dev/sdk

npm i https://pkg.pr.new/@trigger.dev/sdk@beb6074

commit: beb6074

coderabbitai[bot]

This comment was marked as resolved.

@carderne
carderne marked this pull request as ready for review August 11, 2026 14:17
coderabbitai[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Observability map

As of 0044bbf.

19/100 over 417 measured of 433 entry points (base 18, up 1)

What this PR changed

route base head now failing
/api/v1/dashboard-agent/eval-policy (suppressed: request-context) new 0
/_app/orgs/new 0 50

FIX FIRST

  • /api/v1/projects/:projectRef/envvars (sensitive) - auth-boundary, request-context
  • /auth/sso (sensitive) - auth-boundary, request-context
  • /_app/orgs/:organizationSlug/settings/team (sensitive) - error-classification, auth-scope, request-context

AUDIT 3 of 50 sensitive mutations record an actor. 47 without one.
CONTEXT 14 of 417 entry points name a tenant on a failure path. 325 appear only here, 39 of them sensitive, in the JSON rather than the fix list.

What the score is made of
CHECKS
  error-classification  171 applicable,  96 pass,   0 sole, global without it 10
  auth-boundary          62 applicable,  57 pass,   0 sole, global without it 15
  auth-scope             19 applicable,  17 pass,   0 sole, global without it 18
  request-context       417 applicable,  14 pass, 224 sole, global without it 64
  audit-trail            50 applicable,   3 pass,   0 sole, not in the score

The score and findings here are report-only and never gate the merge. Separately, a required test suite keeps this tool's symbol and route lists in sync with the code they name, and can fail a pull request that renames or removes a symbol they reference, or that adds the first route with a segment they anticipate. Each failure names the list to edit. The rules and their reasons: internal-packages/observability-map/README.md.

devin-ai-integration[bot]

This comment was marked as resolved.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 4 new potential issues.

Open in Devin Review

Comment on lines +94 to +99
const firstNominalAt = calculateNextNominalTimestamp(schedule, timezone, afterNominal);
const firstOccurrence = occurrenceAt(firstNominalAt);

if (firstOccurrence.effectiveAt.getTime() >= now.getTime()) {
return { ...firstOccurrence, skippedExpiredOccurrences: false };
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 A deploy while a spread schedule is waiting to run silently cancels that run

A scheduled occurrence that is already waiting inside its spread delay is thrown away and replaced by the following occurrence (calculateNextSchedulableOccurrence early-return at internal-packages/schedule-engine/src/engine/scheduleCalculation.ts:94-99) whenever the schedule is re-registered during the delay, so that occurrence never runs.

Impact: With cron spreading enabled, deploying an app while a spread schedule is in its delay window silently skips that scheduled run.

Why re-registration during the delay window drops the pending occurrence

With spreading enabled, occurrence N is enqueued to fire at effectiveAt_N = nominalAt_N + offset, which can be up to a full interval (or the configured window, e.g. 30 minutes) after nominalAt_N.

syncDeclarativeSchedules (apps/webapp/app/v3/services/createBackgroundWorker.server.ts:723,756) calls registerNextTaskScheduleInstance({ instanceId }) on every deploy, with no fromTimestamp. registerNextTaskScheduleInstance then uses fromTimestamp = registrationTime (internal-packages/schedule-engine/src/engine/index.ts:216-238).

If now falls in (nominalAt_N, effectiveAt_N):

  • firstNominalAt = next(now) = nominalAt_{N+1}
  • firstOccurrence.effectiveAt >= now, so the function returns immediately at scheduleCalculation.ts:97-99 and never consults the latestNominalAt branch (scheduleCalculation.ts:103-115) that would have found occurrence N still eligible.

enqueueScheduledTask then re-enqueues under the same job id scheduled-task-instance:${instanceId}, and SimpleQueue.enqueue (packages/redis-worker/src/queue.ts:107-131) overwrites the existing entry rather than skipping it. The pending occurrence N job is destroyed and never fires.

Before this PR the delay window was effectively zero (the job was always available before its nominal time), so next(now) resolved to the same pending occurrence and nothing was lost.

The latestNominalAt check appears to already encode the right rule; it just needs to be evaluated before accepting firstOccurrence when firstNominalAt skipped past a still-eligible earlier tick.

Prompt for agents
In calculateNextSchedulableOccurrence (internal-packages/schedule-engine/src/engine/scheduleCalculation.ts), the fast path returns the first nominal occurrence after `afterNominal` as soon as its effective time is in the future. When `afterNominal` defaults to `now` (every external caller: deploy sync via syncDeclarativeSchedules, schedule upsert, manual registration), and the previously-registered occurrence is currently sitting inside its spread delay window (now is after its nominal tick but before its effective time), `next(now)` resolves to the FOLLOWING nominal tick. registerNextTaskScheduleInstance then re-enqueues under the same worker job id, and redis-worker's SimpleQueue.enqueue overwrites the pending job — so the in-delay occurrence is silently dropped and never fires. With a 30m window on an hourly cron, roughly half of all deploys would drop a scheduled run.

The existing `latestNominalAt` branch (using previousScheduledTimestamp(now + 1ms)) already computes exactly the occurrence that should win in this situation, but it is only reached when the first occurrence has already expired. Consider evaluating the latest nominal tick whenever it is strictly after `afterNominal`, is not after `now`, and its effective time is still upcoming — preferring it over the later `firstNominalAt` — so that re-registration during a delay window preserves the pending occurrence. Add coverage for: afterNominal == now, now strictly between nominalAt and effectiveAt, spreading enabled.
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

generatorDescription: cronstrue.toString(options.cron),
timezone: options.timezone ?? "UTC",
externalId: options.externalId ? options.externalId : null,
...normalizeScheduleWindow(options.window),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Editing a schedule in the dashboard silently removes its configured delay window

A schedule's saved delay window is wiped out (normalizeScheduleWindow(options.window) at apps/webapp/app/v3/services/upsertTaskSchedule.server.ts:166) whenever the schedule is saved from the dashboard, because the dashboard form never carries the window value.

Impact: A window configured through the API is silently lost the first time someone edits that schedule in the dashboard.

Form submission path

The dashboard create/edit routes parse the form body with the UpsertSchedule zod schema (apps/webapp/app/routes/resources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.schedules.new/route.tsx:73) and pass the result straight to UpsertTaskScheduleService. The form has no window field, so options.window is always undefined, and normalizeScheduleWindow(undefined) returns { windowDurationSeconds: null, windowPercentage: null }, which the update writes over the existing values.

This also flips scheduleHasChanged (apps/webapp/app/v3/services/upsertTaskSchedule.server.ts:172-174) and re-registers all instances, so timing changes immediately.

A fix could either keep the existing window when the caller omits the field on the dashboard path (e.g. an explicit window: undefined vs. "not provided" distinction), or surface the window in the form so it round-trips.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines 150 to 152
for (let i = 0; i < count; i++) {
nextScheduledTimestamp = calculateNextScheduledTimestamp(
cron,
timezone,
nextScheduledTimestamp
);
nextScheduledTimestamp = calculateNextNominalTimestamp(cron, timezone, nextScheduledTimestamp);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 upcoming timestamps can now include past dates on a late fire

nextScheduledTimestamps now chains purely off calculateNextNominalTimestamp instead of calculateNextScheduledTimestamp, dropping the old "if the computed slot is in the past, recompute from now" fallback. For the dashboard/API callers that seed from new Date() this is identical. Inside the engine, though, upcoming is generated from exactScheduleTime (internal-packages/schedule-engine/src/engine/index.ts:538-543), which with spreading enabled is the nominal tick and can be well in the past by the time the job actually fires. ScheduledTaskPayload.upcoming may therefore contain timestamps that have already elapsed, which previously could not happen. Arguably more consistent (strict nominal chaining), but it is a user-visible payload change worth confirming.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +159 to +162
const queueWaitMs =
typeof message.message.eligibleAtMs === "number"
? Math.max(0, Date.now() - message.message.eligibleAtMs)
: undefined;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Queue-wait metrics will absorb the intentional spread delay

eligibleAtMs is stamped at enqueue time (internal-packages/run-engine/src/engine/systems/enqueueSystem.ts:155 uses Date.now() unless anchorEligibilityAtQueuePosition). For a spread schedule the run is enqueued at trigger time (up to a full window before its effective time), so the new queue_wait_ms span attribute and the existing wait field in run-queue/index.ts:1018-1019 will report the deliberate spread delay as queue backlog. If these feed latency dashboards/alerts, enabling the flag will look like a queue regression.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant