Skip to content

Cron jobs#21

Open
digitalmio wants to merge 4 commits into
mainfrom
feat/cron
Open

Cron jobs#21
digitalmio wants to merge 4 commits into
mainfrom
feat/cron

Conversation

@digitalmio
Copy link
Copy Markdown
Owner

No description provided.

Avoids potential undefined access by extracting the first element
and checking for existence before calling `.getTime()`.
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 26, 2026

Greptile Summary

This PR introduces cron job support for EdgePod Durable Objects, backed by the croner library and Cloudflare's alarm API. Cron schedules are defined via createSchedule, registered through a cronFunctions override on BaseEdgePodEngine, and executed inside handleCronAlarm using a 2-minute lookback window to tolerate alarm delivery jitter.

  • packages/server/src/server/cron.ts — New module with scheduleNextAlarm, executeCron, and handleCronAlarm; handles schedule parsing, context construction, DB tracking, and invalidation broadcasting for cron runs.
  • packages/server/src/server/do.ts — Adds cronFunctions property, boot-sequence alarm seeding inside blockConcurrencyWhile, and an alarm() override that delegates to handleCronAlarm.
  • CLI templates + playground — Scaffolds cron/index.ts and updates the generated server.ts and types.ts so new projects get the cronFunctions override and CronCtx type out of the box.

Confidence Score: 5/5

Safe to merge; the cron scheduling logic is correct for all practical schedule frequencies and the changes are well-isolated.

The core alarm loop (scheduleNextAlarm → alarm() → handleCronAlarm → scheduleNextAlarm) is sound for schedules at or above 2-minute intervals, which covers all common use cases. The two findings are quality improvements: early validation of cron expressions in createSchedule, and trimming do.ts by one line to stay within the stated file-length limit. Neither affects runtime correctness.

packages/server/src/tools/createSchedule.ts (expression validation) and packages/server/src/server/do.ts (file length).

Important Files Changed

Filename Overview
packages/server/src/server/cron.ts New file implementing cron scheduling via croner + Cloudflare alarm API; correct for typical schedules (>= 5 min), inherits the scheduledTime precision issues flagged in existing review threads.
packages/server/src/server/do.ts Adds cronFunctions property, boot-sequence alarm seeding, and alarm() override; file is now 251 lines, one line over the 250-line hard limit defined in AGENTS.md.
packages/server/src/tools/createSchedule.ts Thin factory for CronDefinition; schedule string is not validated at definition time — invalid expressions fail silently at runtime.
packages/server/src/types/index.ts Adds CronContext and CronDefinition types; handler return type is correctly typed as Promise, fixing the previous JsonValue concern.
packages/cli/src/templates/cron.ts New template scaffolding the cron/index.ts file for generated projects; content is all comments plus export {}.
packages/cli/src/templates/server.ts Adds cron import and cronFunctions override to the generated server template; straightforward and consistent with the function/schema pattern.
playground/edgepod/cron/index.ts Scaffold placeholder with commented usage example; correctly exports nothing so cronFunctions defaults to an empty object.

Sequence Diagram

sequenceDiagram
    participant CF as Cloudflare Runtime
    participant DO as BaseEdgePodEngine
    participant Cron as cron.ts
    participant Croner as croner (Cron)
    participant Handler as User CronHandler

    CF->>DO: new (cold start / wake)
    DO->>DO: blockConcurrencyWhile()
    DO->>Cron: scheduleNextAlarm(cronFunctions, setAlarm)
    Cron->>Croner: nextRuns(1, now) for each schedule
    Croner-->>Cron: next fire Date
    Cron->>CF: storage.setAlarm(soonestMs)

    CF->>DO: alarm() fires
    DO->>Cron: handleCronAlarm(cronFunctions, setAlarm, ...)
    Cron->>Croner: nextRuns(1, now-2min) for each schedule
    Croner-->>Cron: nextRun Date
    alt "nextRun <= now"
        Cron->>Cron: executeCron(name, def, nextRun, ...)
        Cron->>Handler: def.handler(ctx)
        Handler-->>Cron: void
        Cron->>DO: broadcastInvalidations(tables)
    end
    Cron->>CF: storage.setAlarm(nextSoonestMs)
Loading

Reviews (2): Last reviewed commit: "Add explanatory comment for cold-start a..." | Re-trigger Greptile

Comment thread packages/server/src/server/do.ts
Comment thread packages/server/src/server/cron.ts Outdated
Comment thread packages/server/src/server/cron.ts
Comment thread packages/server/src/types/index.ts Outdated
Cron handlers now receive the scheduled execution time as a parameter
rather than capturing the current time. This ensures consistent tracing
and context across async operations.
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