mirror of
https://github.com/tiennm99/miti99bot.git
synced 2026-04-17 15:20:58 +00:00
3.3 KiB
3.3 KiB
Phase 02 — Cron Wiring
Priority: P0 Status: Complete Depends on: Phase 01
Overview
Add Cloudflare Cron Triggers support to the module framework. Modules can declare crons: [{ schedule, handler }] alongside commands.
Requirements
Functional
- Module contract extended with optional
crons[]. - Each cron entry:
{ schedule: string, name: string, handler: async (event, ctx) => void }.scheduleis a cron expression (e.g."0 1 * * *").namerequired for logging + conflict detection (unique within module).handlerreceives(event, { db, sql, env }).
src/index.jsexportsscheduled(event, env, ctx)in addition tofetch.scheduled()dispatches to all modules whoseschedulematchesevent.cron.- Multiple modules can share the same schedule — all their handlers fire.
wrangler.tomlrequires[triggers] crons = [...]— populated by a build step OR manually (decision below).
Non-functional
- Errors in one cron handler do not block others (
Promise.allSettled). - Handler timeouts bounded by Workers cron execution limits (15min max).
Architecture
Cron Trigger fires
│
▼
src/index.js → scheduled(event, env, ctx)
│
▼
getRegistry(env) ◄── reuses existing memoized registry
│
▼
for each module.crons[] where entry.schedule === event.cron:
ctx.waitUntil(entry.handler(event, {
db: createStore(module.name, env),
sql: createSqlStore(module.name, env),
env,
}))
Related Code Files
Create
src/modules/cron-dispatcher.js— dispatchesevent.cronto matching handlerstests/modules/cron-dispatcher.test.js
Modify
src/index.js— addscheduledexportsrc/modules/registry.js— collect + validatecrons[]per module; conflict check on(module, cronName)duplicatessrc/modules/validate-command.js→ addvalidate-cron.jssiblingwrangler.toml— add[triggers] crons = ["0 1 * * *", ...](union of all schedules)scripts/register.js— no change (cron triggers are set bywrangler deployfrom toml)- Docs for module contract
Open Decisions
wrangler.tomlcrons population:- (a) manual — module author adds schedule to toml when adding cron.
- (b) generated — prebuild script scans modules, writes toml triggers.
- Lean (a) for simplicity — YAGNI. Document in
adding-a-module.md.
Todo List
cron-dispatcher.jsvalidate-cron.js- Extend
registry.jsto surfacecrons[] - Add
scheduledexport insrc/index.js - Update module contract JSDoc typedef
- Unit tests for dispatcher (schedule match, fan-out, error isolation)
- Document in
docs/using-cron.md(done in Phase 06)
Success Criteria
- A module declaring
crons: [{ schedule: "*/5 * * * *", name: "tick", handler }]hashandlerinvoked every 5 min locally viawrangler dev --test-scheduledand in prod. - Error in one handler doesn't prevent others.
npm testgreen.
Risks
wrangler dev --test-scheduledintegration — document thecurl "http://localhost:8787/__scheduled?cron=..."pattern.- Cold start on cron: registry memoization across fetch+scheduled invocations — ensure single shared cache.
Next Steps
- Phase 04 (retention cron) consumes this.