@lunora/cli 1.0.0-alpha.40 → 1.0.0-alpha.400

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/LICENSE.md +33 -0
  2. package/README.md +3 -1
  3. package/dist/bin.mjs +2 -10
  4. package/dist/index.d.mts +1034 -374
  5. package/dist/index.d.ts +1034 -374
  6. package/dist/index.mjs +1 -19
  7. package/dist/packem_chunks/dispatch.mjs +1 -0
  8. package/dist/packem_chunks/handler.mjs +1 -160
  9. package/dist/packem_chunks/handler10.mjs +1 -16
  10. package/dist/packem_chunks/handler11.mjs +1 -22
  11. package/dist/packem_chunks/handler12.mjs +1 -192
  12. package/dist/packem_chunks/handler13.mjs +3 -131
  13. package/dist/packem_chunks/handler14.mjs +1 -65
  14. package/dist/packem_chunks/handler15.mjs +1 -58
  15. package/dist/packem_chunks/handler16.mjs +1 -80
  16. package/dist/packem_chunks/handler17.mjs +7 -43
  17. package/dist/packem_chunks/handler18.mjs +1 -105
  18. package/dist/packem_chunks/handler19.mjs +1 -170
  19. package/dist/packem_chunks/handler2.mjs +1 -114
  20. package/dist/packem_chunks/handler20.mjs +3 -94
  21. package/dist/packem_chunks/handler21.mjs +3 -94
  22. package/dist/packem_chunks/handler22.mjs +1 -0
  23. package/dist/packem_chunks/handler23.mjs +2 -0
  24. package/dist/packem_chunks/handler24.mjs +99 -0
  25. package/dist/packem_chunks/handler25.mjs +2 -0
  26. package/dist/packem_chunks/handler26.mjs +9 -0
  27. package/dist/packem_chunks/handler27.mjs +1 -0
  28. package/dist/packem_chunks/handler3.mjs +1 -204
  29. package/dist/packem_chunks/handler4.mjs +3 -33
  30. package/dist/packem_chunks/handler5.mjs +1 -49
  31. package/dist/packem_chunks/handler6.mjs +1 -91
  32. package/dist/packem_chunks/handler7.mjs +1 -42
  33. package/dist/packem_chunks/handler8.mjs +2 -174
  34. package/dist/packem_chunks/handler9.mjs +1 -315
  35. package/dist/packem_chunks/runCodegenCommand.mjs +4 -52
  36. package/dist/packem_chunks/runDeployCommand.mjs +11 -594
  37. package/dist/packem_chunks/runDevCommand.mjs +2 -0
  38. package/dist/packem_chunks/runInitCommand.mjs +165 -1432
  39. package/dist/packem_chunks/runResetCommand.mjs +1 -41
  40. package/dist/packem_chunks/runRpcCommand.mjs +1 -68
  41. package/dist/packem_shared/COMMANDS-aSTSMwZS.mjs +1 -0
  42. package/dist/packem_shared/DEFAULT_IMPORT_BATCH_SIZE-CyIsI3S1.mjs +1 -0
  43. package/dist/packem_shared/EXIT_CODE-D_XCixv8.mjs +1 -0
  44. package/dist/packem_shared/admin-token-VdUnvnKW.mjs +1 -0
  45. package/dist/packem_shared/admin-url-DJB8P6Jh.mjs +1 -0
  46. package/dist/packem_shared/api-spec-lYyq4vL6.mjs +1 -0
  47. package/dist/packem_shared/binding-manifest-file-CQ1eQYy1.mjs +2 -0
  48. package/dist/packem_shared/buildRegistryIndex-Dc8D8AR6.mjs +1 -0
  49. package/dist/packem_shared/cap-error-body-YBKO32BF.mjs +1 -0
  50. package/dist/packem_shared/catalog-CahzmDLV.mjs +1 -0
  51. package/dist/packem_shared/cli-CFsmCYWp.mjs +2 -0
  52. package/dist/packem_shared/cli-manifest-DoQ_3bzs.mjs +1 -0
  53. package/dist/packem_shared/cloudflare-credentials-BVmZnDZk.mjs +1 -0
  54. package/dist/packem_shared/codegen-error-AmH54ofi.mjs +3 -0
  55. package/dist/packem_shared/command-BgjsvUFg.mjs +1 -0
  56. package/dist/packem_shared/commands-DvSfLvyt.mjs +13 -0
  57. package/dist/packem_shared/createLogger-4TujevyB.mjs +2 -0
  58. package/dist/packem_shared/createRecordingSpawner-Ch7zuQQE.mjs +1 -0
  59. package/dist/packem_shared/deploy-target-resolve-BTCW1Iv4.mjs +1 -0
  60. package/dist/packem_shared/diffSnapshots-htqNZT8Q.mjs +7 -0
  61. package/dist/packem_shared/docker-AqLAkOyY.mjs +1 -0
  62. package/dist/packem_shared/health-probe-Bw9JFfIy.mjs +1 -0
  63. package/dist/packem_shared/import-BRM1hs7j.mjs +12 -0
  64. package/dist/packem_shared/index-oHZBGBwB.mjs +1 -0
  65. package/dist/packem_shared/insertSchemaExtension-DuiV6cba.mjs +8 -0
  66. package/dist/packem_shared/lint-ignore-report-DKZagpqk.mjs +2 -0
  67. package/dist/packem_shared/mcp-config-file-CcDTeXIq.mjs +2 -0
  68. package/dist/packem_shared/open-url-EnKy--w-.mjs +1 -0
  69. package/dist/packem_shared/output-format-DWe_tkTR.mjs +2 -0
  70. package/dist/packem_shared/parseManifest-CwPTKdtS.mjs +1 -0
  71. package/dist/packem_shared/path-containment-CgxYZggb.mjs +1 -0
  72. package/dist/packem_shared/plan-DVfi3TrN.mjs +5 -0
  73. package/dist/packem_shared/planDevCommand-IglbXy5A.mjs +1 -0
  74. package/dist/packem_shared/platform-diagnostics-Cn2g6Jh-.mjs +4 -0
  75. package/dist/packem_shared/prompt-cancelled-BvsNxg_Q.mjs +1 -0
  76. package/dist/packem_shared/render-lunora-error-C3OJY5wk.mjs +2 -0
  77. package/dist/packem_shared/resolve-Bdc9KEUv.mjs +5 -0
  78. package/dist/packem_shared/resolve-target-C_ZloTvd.mjs +1 -0
  79. package/dist/packem_shared/runAddCommand-BChdJQ5L.mjs +1 -0
  80. package/dist/packem_shared/runExportCommand-slAq5BCF.mjs +5 -0
  81. package/dist/packem_shared/runMigrateGenerateCommand-BwemPIOz.mjs +11 -0
  82. package/dist/packem_shared/schemaIrToSnapshot-BHuNOfgs.mjs +1 -0
  83. package/dist/packem_shared/secret-key-8DPOH98z.mjs +1 -0
  84. package/dist/packem_shared/shared-D-zCOmgY.mjs +1 -0
  85. package/dist/packem_shared/storage-DNzYhXR-.mjs +1 -0
  86. package/dist/packem_shared/tui-prompts-B3YwUhGw.mjs +4 -0
  87. package/dist/packem_shared/variables-CPYyJtsL.mjs +1 -0
  88. package/dist/packem_shared/wrangler-name-Dsk5K1f-.mjs +1 -0
  89. package/dist/packem_shared/wrangler-secrets-CTQPpalP.mjs +1 -0
  90. package/package.json +39 -19
  91. package/skills/README.md +35 -17
  92. package/skills/lunora/SKILL.md +154 -64
  93. package/skills/lunora-create-package/SKILL.md +97 -84
  94. package/skills/lunora-deploy/SKILL.md +175 -105
  95. package/skills/lunora-functions/SKILL.md +362 -56
  96. package/skills/lunora-migration-helper/SKILL.md +162 -137
  97. package/skills/lunora-performance-audit/SKILL.md +137 -95
  98. package/skills/lunora-quickstart/SKILL.md +137 -136
  99. package/skills/lunora-realtime/SKILL.md +140 -99
  100. package/skills/lunora-setup-auth/SKILL.md +162 -102
  101. package/skills/lunora-setup-hyperdrive/SKILL.md +130 -109
  102. package/skills/lunora-setup-hyperdrive-global/SKILL.md +93 -90
  103. package/skills/lunora-setup-mail/SKILL.md +85 -113
  104. package/skills/lunora-setup-scheduler/SKILL.md +132 -96
  105. package/skills/lunora-setup-storage/SKILL.md +189 -95
  106. package/dist/packem_chunks/planDevCommand.mjs +0 -541
  107. package/dist/packem_chunks/runMigrateGenerateCommand.mjs +0 -397
  108. package/dist/packem_shared/COMMANDS-B0ftFD_3.mjs +0 -948
  109. package/dist/packem_shared/DEFAULT_IMPORT_BATCH_SIZE-Ck-2bU08.mjs +0 -244
  110. package/dist/packem_shared/admin-url-4UzT-CI4.mjs +0 -19
  111. package/dist/packem_shared/api-spec-CtA6ilu4.mjs +0 -13
  112. package/dist/packem_shared/buildRegistryIndex-BcYe607_.mjs +0 -38
  113. package/dist/packem_shared/codegen-error-DJG-ghs_.mjs +0 -31
  114. package/dist/packem_shared/command-D3lB_4Az.mjs +0 -19
  115. package/dist/packem_shared/commands-B-gR09Z_.mjs +0 -845
  116. package/dist/packem_shared/createLogger-B40gPzQo.mjs +0 -78
  117. package/dist/packem_shared/createRecordingSpawner-DxI3mebw.mjs +0 -43
  118. package/dist/packem_shared/detect-package-manager-DYp7n3mJ.mjs +0 -61
  119. package/dist/packem_shared/diffSnapshots-BeDvvNiF.mjs +0 -161
  120. package/dist/packem_shared/docker-hMQ97KSQ.mjs +0 -21
  121. package/dist/packem_shared/insertSchemaExtension-DAqbfr9Z.mjs +0 -64
  122. package/dist/packem_shared/open-url-Dfq6fAyT.mjs +0 -41
  123. package/dist/packem_shared/output-format-wUvAN6AL.mjs +0 -17
  124. package/dist/packem_shared/parseArgs-YXFuKdEk.mjs +0 -56
  125. package/dist/packem_shared/parseManifest--vZf2FY1.mjs +0 -94
  126. package/dist/packem_shared/prompt-cancelled-APzX1Im-.mjs +0 -9
  127. package/dist/packem_shared/resolve-target-qbsJ_5sF.mjs +0 -16
  128. package/dist/packem_shared/runAddCommand-bnY6-HKb.mjs +0 -4
  129. package/dist/packem_shared/schema-drift-gate-BtBt0as0.mjs +0 -79
  130. package/dist/packem_shared/schemaIrToSnapshot-DdsljJT-.mjs +0 -43
  131. package/dist/packem_shared/storage-BIsph-Vk.mjs +0 -84
  132. package/dist/packem_shared/tui-prompts-BjEN8XgP.mjs +0 -658
  133. package/dist/packem_shared/wrangler-name-cy4yhm9j.mjs +0 -12
  134. package/dist/packem_shared/wrangler-secrets-P2_ZUR-k.mjs +0 -47
@@ -1,157 +1,193 @@
1
1
  ---
2
2
  name: lunora-setup-scheduler
3
- description: Schedules deferred and recurring work in a Lunora app. Use for `ctx.scheduler.runAfter` / `runAt` (delayed function dispatch), cron jobs via `lunora registry add crons` (the `cronJobs()` builder), the `SchedulerDO` / `SCHEDULER` binding, retries, and the workpool for bounded concurrency.
3
+ description: Schedules deferred and recurring work in a Lunora app. Covers `ctx.scheduler.runAfter` / `runAt` (delayed dispatch of a function or workflow), cron jobs in `lunora/crons.ts` via `lunora registry add crons` and the `cronJobs()` builder, the `SchedulerDO` / `SCHEDULER` binding and `LUNORA_ORIGIN_URL`, retries and dead-lettering, and `createWorkpool` for bounded concurrency. Use when the user wants to "run this later", "send a reminder in 3 days", "run every night / every 5 minutes", add a cron, cancel a scheduled job, or sees `ORIGIN_NOT_CONFIGURED` or a missing `SchedulerDO` export.
4
4
  ---
5
5
 
6
6
  # Lunora Setup Scheduler
7
7
 
8
- Schedule work in Lunora two ways, both backed by `@lunora/scheduler` (re-exported
9
- from `@lunora/server`):
8
+ Lunora schedules work in two ways, both implemented in `@lunora/scheduler`:
10
9
 
11
- - **Deferred dispatch** — `ctx.scheduler.runAfter` / `runAt` from any function,
12
- to run a function later. Built into the runtime; **no registry install**.
13
- - **Recurring jobs (crons)** — declare them in `lunora/crons.ts` with
14
- `cronJobs()`. Add the starter with `lunora registry add crons`.
10
+ - **Deferred dispatch**: `ctx.scheduler.runAfter` / `runAt` run a function (or
11
+ start a workflow) later. Jobs are stored in the `SchedulerDO` Durable Object
12
+ (binding `SCHEDULER`), which owns the alarm, the retries and the dead-letter list.
13
+ - **Recurring jobs (crons)**: declared in `lunora/crons.ts` with `cronJobs()`.
14
+ Codegen compiles them into Cloudflare Cron Triggers, and the Worker's
15
+ `scheduled()` handler dispatches them directly. They don't go through the
16
+ `SchedulerDO`.
15
17
 
16
- Both run jobs through the `SchedulerDO` Durable Object (binding `SCHEDULER`),
17
- which owns the alarm and durable storage.
18
+ ## When Not to Use
18
19
 
19
- ## When to Use
20
+ - The project has no Lunora backend yet: use `lunora-quickstart` first.
21
+ - You only need to react to data changes: use a reactive `query` /
22
+ subscription (`lunora-realtime`).
23
+ - Multi-step orchestration with sleeps and waits belongs in a durable workflow.
24
+ A scheduler or cron can start one, but don't build step logic on top of jobs.
20
25
 
21
- - Running a function after a delay or at a specific time
22
- (`runAfter` / `runAt`).
23
- - Declaring recurring jobs (sweep, digest, report) on a cron schedule.
24
- - Bounding concurrency for many enqueued jobs (a workpool).
26
+ ## Deferred dispatch: `runAfter` / `runAt`
25
27
 
26
- ## When Not to Use
28
+ ### Wire it once
27
29
 
28
- - The project has no Lunora backend yet — use `lunora-quickstart` first.
29
- - You just need to react to live data changes — use a reactive `query` /
30
- subscription (`lunora-realtime`), not a scheduled job.
30
+ ```bash
31
+ pnpm add @lunora/scheduler
32
+ lunora codegen
33
+ ```
31
34
 
32
- ## Deferred dispatch — `runAfter` / `runAt`
35
+ Codegen needs `@lunora/scheduler` installed as soon as `ctx.scheduler` is used,
36
+ because the generated `_generated/app.ts` imports `createScheduler`. Once the
37
+ dependency is present, codegen emits `_generated/scheduler.ts` (re-exporting
38
+ `SchedulerDO`) and a `.scheduler()` method on the app builder.
33
39
 
34
- Available on `ctx.scheduler` in any function. Target functions are passed by
35
- reference from the generated `api` / `internal` proxy:
40
+ - **Vite-first apps** (generated worker entry): the plugin forwards
41
+ `SchedulerDO` and calls `.scheduler()` for you.
42
+ - **Hand-written worker entry** (`src/server.ts` with `defineApp`): wire it yourself.
43
+ `wrangler.jsonc`'s `SCHEDULER` binding is reconciled from this export:
36
44
 
37
45
  ```ts
38
- import { mutation, v } from "@lunora/server";
46
+ const app = defineApp<Env>()
47
+ .shard((env) => env.SHARD)
48
+ .scheduler({ namespace: (env) => env.SCHEDULER })
49
+ .build();
50
+
51
+ export const ShardDO = app.ShardDO;
52
+ export { SchedulerDO } from "../lunora/_generated/scheduler.js";
53
+ ```
54
+
55
+ The `SchedulerDO` calls back into the Worker at `LUNORA_ORIGIN_URL`. It reads
56
+ that value from its own env and never from the request, so a caller can't point
57
+ dispatch at an arbitrary URL. Without it, every `runAfter`/`runAt` fails with
58
+ `ORIGIN_NOT_CONFIGURED`, and nothing provisions it for you:
39
59
 
40
- import { internal } from "./_generated/api";
60
+ - dev: `LUNORA_ORIGIN_URL="http://localhost:5173"` in `.dev.vars` (your dev
61
+ server's URL)
62
+ - prod: `vars.LUNORA_ORIGIN_URL` in `wrangler.jsonc`, or
63
+ `wrangler secret put LUNORA_ORIGIN_URL` (per `--env`)
41
64
 
42
- export const startTrial = mutation.input({ userId: v.string() }).mutation(async ({ ctx, args: { userId } }) => {
43
- // run an internal action 14 days from now
44
- const { id } = await ctx.scheduler.runAfter(14 * 24 * 60 * 60 * 1000, internal.billing.endTrial, { userId });
65
+ `lunora doctor` warns when the scheduler binding exists and the variable is unset.
45
66
 
46
- return { jobId: id };
67
+ ### Schedule from a mutation or action
68
+
69
+ ```ts
70
+ import { LunoraError } from "lunorash/server";
71
+
72
+ import { internal } from "#lunora/_generated/internal.js";
73
+ import { mutation } from "#lunora/_generated/server.js";
74
+
75
+ export const startTrial = mutation.mutation(async ({ ctx }) => {
76
+ // the caller's own id, never one the client passes in
77
+ const { userId } = ctx.auth;
78
+ if (!userId) {
79
+ throw new LunoraError("UNAUTHORIZED", "not signed in");
80
+ }
81
+ // run an internal action 14 days from now; the id is a plain string
82
+ const jobId = await ctx.scheduler.runAfter(14 * 24 * 60 * 60 * 1000, internal.billing.endTrial, { userId });
83
+
84
+ return { jobId };
47
85
  });
48
86
  ```
49
87
 
50
- - `runAfter(delayMs, fnRef, args, options?)` — run after a delay (`delayMs` must
51
- be a non-negative finite number). `runAt(date, fnRef, args, options?)` — run at
52
- a `Date` or epoch-ms timestamp.
53
- - Both return `{ id, scheduledFor }`. Cancel with `ctx.scheduler.cancel(id)`;
54
- inspect with `ctx.scheduler.get(id)` / `ctx.scheduler.list()`.
55
- - `options` accepts a `retry` policy (`{ maxAttempts, backoff, baseMs, maxMs }`;
56
- DO defaults: `maxAttempts: 5`, `backoff: "exponential"`, `baseMs: 30_000`) and
57
- a `shardKey` routing hint. On retry exhaustion the job is dead-lettered, never
58
- silently dropped.
59
- - The `SchedulerDO` binding (`SCHEDULER`) is **auto-inferred and reconciled**
60
- into `wrangler.jsonc` by `@lunora/config` once `@lunora/scheduler` is in use —
61
- run `lunora codegen` / `lunora doctor` to confirm. Scheduled jobs run with no
62
- end-user identity, so target **internal** functions (`internal.*`).
63
-
64
- ## Recurring jobs — crons
88
+ - The full `ctx.scheduler` surface is `runAfter(delayMs, target, args?)`,
89
+ `runAt(timestampMs, target, args?)` (epoch ms), `cancel(id)`, `get(id)`, and `list()`.
90
+ It is not available in a `query`, because queries are deterministic and re-run.
91
+ - `target` is a generated `internal.*` / `api.*` mutation or action reference, a
92
+ `"file:fn"` string, or a `workflows.<name>` / `agents.<name>` reference.
93
+ A workflow or agent reference starts a fresh instance when the job fires, with
94
+ `args` as its params. Scheduled jobs run with no end-user identity, so target
95
+ `internal.*` functions.
96
+ - **Inside a mutation, scheduling is transactional.** The call is buffered and
97
+ dispatched only after the mutation commits. If the mutation rolls back (OCC
98
+ conflict, validation error), the job is dropped too. The returned id is final
99
+ immediately, so you can store it on the row you're writing and `cancel` it later.
100
+ - Retries use the DO defaults: 5 retries, exponential backoff from 30 s. A job
101
+ that exhausts them is dead-lettered (visible in the studio), not dropped. To set
102
+ a per-job `retry` policy (`{ maxAttempts, backoff: "exponential" | "linear",
103
+ baseMs, maxMs }`) or a `shardKey`, build your own client with
104
+ `createScheduler({ namespace })` from `@lunora/scheduler`. Its `runAfter`/`runAt`
105
+ take a fourth `RunOptions` argument and add `dead()` / `deadRetry(id)`.
106
+
107
+ ## Recurring jobs: crons
65
108
 
66
109
  ### Step 1: Add the starter
67
110
 
68
111
  ```bash
69
112
  lunora registry add crons
113
+ pnpm install
70
114
  ```
71
115
 
72
- This adds `@lunora/server` to `package.json` (run `pnpm install`) and copies
73
- `lunora/crons.ts` (a `cronJobs()` registry with one illustrative job) and
74
- `lunora/crons/jobs.ts` (the example `run` internal mutation it fires) into your
75
- project — both **yours** to edit. No extra DO binding is required.
116
+ This adds `@lunora/server` + `@lunora/scheduler` to `package.json` and copies two
117
+ files you own: `lunora/crons.ts` (a `cronJobs()` registry) and
118
+ `lunora/crons/jobs.ts` (an example `internalMutation`, `internal.crons.jobs.run`).
76
119
 
77
120
  ### Step 2: Declare jobs
78
121
 
79
122
  ```ts
80
123
  import { cronJobs } from "@lunora/server";
81
124
 
82
- import { internal } from "./_generated/api";
125
+ import { internal } from "#lunora/_generated/internal.js";
83
126
 
84
127
  const crons = cronJobs();
85
128
 
86
129
  crons.interval("sweep presence", { minutes: 5 }, internal.presence.sweep, { roomId: "lobby" });
87
- crons.daily("digest", { hourUTC: 9, minuteUTC: 0 }, internal.email.digest, {});
88
- crons.weekly("report", { dayOfWeek: "monday", hourUTC: 8, minuteUTC: 0 }, internal.reports.weekly, {});
89
- crons.monthly("invoice", { day: 1, hourUTC: 0, minuteUTC: 0 }, internal.billing.invoice, {});
90
- crons.cron("custom", "0 */6 * * *", internal.foo.bar, {}); // raw cron escape hatch
130
+ crons.hourly("rollup", { minuteUTC: 17 }, internal.stats.rollup);
131
+ crons.daily("digest", { hourUTC: 9, minuteUTC: 0 }, internal.email.digest);
132
+ crons.weekly("report", { dayOfWeek: "monday", hourUTC: 8, minuteUTC: 0 }, internal.reports.weekly);
133
+ crons.monthly("invoice", { day: 1, hourUTC: 0, minuteUTC: 0 }, internal.billing.invoice);
134
+ crons.cron("custom", "0 */6 * * *", internal.foo.bar); // raw cron escape hatch
91
135
 
92
136
  export default crons;
93
137
  ```
94
138
 
95
- - `name` must be a non-empty **string literal**, unique across the project.
96
- - `fnRef` must be a static two-segment access on the proxy
97
- (`internal.<file>.<fn>` or `api.<file>.<fn>`) so codegen can discover it.
98
- Cron targets must be **internal** functions — a client can never invoke them.
99
- - All schedules are UTC and validated at definition time
100
- (`hourUTC: 25` throws immediately).
139
+ - Names must be non-empty string literals, unique across the project.
140
+ - Targets must be static property accesses (`internal.<…path>.<fn>`, one segment
141
+ per folder and file, e.g. `internal.crons.jobs.run`) or a `workflows.<name>` /
142
+ `agents.<name>` reference. Codegen resolves them from the AST, so a computed
143
+ reference fails codegen. `args` is optional and must be a literal.
144
+ - Schedules are UTC and validated when the job is defined. `interval` takes exactly one of
145
+ `{ minutes }` (must divide 60) or `{ hours }` (must divide 24). `{ seconds }`
146
+ is rejected because Cron Triggers have a one-minute floor. For sub-minute work,
147
+ use `runAfter`.
148
+ - Use `hourly` with an offset minute to spread jobs away from `:00`.
101
149
 
102
- ### Step 3: Regenerate types and the schedule
150
+ ### Step 3: Regenerate
103
151
 
104
152
  ```bash
105
153
  lunora codegen
106
154
  ```
107
155
 
108
- Codegen discovers each registration by AST, compiles the schedule to a cron
109
- expression, and emits `lunora/_generated/crons.ts` (the dispatcher the Worker's
110
- `scheduled()` handler consumes) plus the matching `triggers.crons` entry in
111
- `wrangler.jsonc`. You never hand-edit the wrangler schedule array.
156
+ Codegen emits `lunora/_generated/crons.ts` (`LUNORA_CRON_TRIGGERS` plus the
157
+ `LUNORA_CRONS` dispatcher map, which `defineApp` wires into `scheduled()`) and
158
+ syncs `triggers.crons` in `wrangler.jsonc`. Don't hand-edit that array.
159
+
160
+ **Verify:** the studio's scheduled-jobs view lists each cron and has a "Run now"
161
+ action, which calls the same dispatch path as a real tick.
112
162
 
113
- ## Bounded concurrency — workpool (optional)
163
+ ## Bounded concurrency: workpool (optional)
114
164
 
115
- For many enqueued jobs that must not all run at once, use a workpool — a named
116
- logical pool inside the same `SchedulerDO` (no extra binding):
165
+ A workpool is a named pool inside the same `SchedulerDO`. It needs no extra
166
+ binding. Build it in Worker code that has the raw `env`:
117
167
 
118
168
  ```ts
119
169
  import { createWorkpool } from "@lunora/scheduler";
120
170
 
121
- const pool = createWorkpool({
122
- namespace: env.SCHEDULER,
123
- originUrl: "https://my-app.example.com",
124
- name: "imports",
125
- maxConcurrency: 3,
126
- });
171
+ const pool = createWorkpool({ namespace: env.SCHEDULER, name: "imports", maxConcurrency: 3 });
127
172
 
128
- await pool.enqueue(internal.imports.processRow, { rowId });
173
+ await pool.enqueue(internal.imports.processRow, { rowId }, { retry: { maxAttempts: 3 } });
174
+ // pool.cancel(id), pool.status() → { inFlight, queued, maxConcurrency }
129
175
  ```
130
176
 
131
- The DO caps simultaneous dispatch at `maxConcurrency` and queues the rest
132
- durably. A Cloudflare-Queues-backed variant (`createQueueWorkpool`) leans on
133
- queue config for concurrency/retries instead — reach for the DO workpool when you
134
- need per-job cancel / status.
177
+ The DO dispatches at most `maxConcurrency` jobs at once and queues the rest
178
+ durably. One alarm drain keeps at most six dispatches in flight (the DO connection limit),
179
+ so a long-running job holds a slot for its whole duration. For individually long jobs
180
+ (LLM calls, exports), prefer `createQueueWorkpool`, which is backed by Cloudflare Queues.
181
+ You give up the hard cap, per-job cancel and per-job status in exchange.
135
182
 
136
183
  ## Common Pitfalls
137
184
 
138
- 1. **Targeting a non-internal function from a job/cron.** Scheduled dispatch has
139
- no end-user identity — target `internal.*` functions.
140
- 2. **Non-static `fnRef` or `name` in `cronJobs()`.** Codegen discovers them by
141
- AST; a dynamic reference or computed name can't be found.
142
- 3. **Forgetting `lunora codegen` after editing crons.** The `triggers.crons`
143
- array and the dispatcher map are codegen output — re-run it.
144
- 4. **Non-idempotent job handlers.** A missed tick may be retried and a slow tick
145
- can overlap the next — make handlers idempotent.
146
-
147
- ## Checklist
148
-
149
- - [ ] Deferred work uses `ctx.scheduler.runAfter` / `runAt` against
150
- `internal.*` functions.
151
- - [ ] `SCHEDULER` (SchedulerDO) binding present in `wrangler.jsonc`
152
- (`lunora doctor` clean) when using the scheduler.
153
- - [ ] Recurring jobs declared in `lunora/crons.ts` via `cronJobs()` (after
154
- `lunora registry add crons`).
155
- - [ ] `lunora codegen` run so `_generated/crons.ts` + `triggers.crons` are
156
- synced.
157
- - [ ] Job handlers are idempotent.
185
+ 1. **`ORIGIN_NOT_CONFIGURED`**: `LUNORA_ORIGIN_URL` is unset for the `SchedulerDO`
186
+ (see above).
187
+ 2. **`SchedulerDO` not exported** from a hand-written worker entry: wrangler
188
+ only binds classes the entry exports, so `SCHEDULER` is never provisioned.
189
+ 3. **Dynamic `name` or target in `cronJobs()`**: codegen cannot discover it.
190
+ 4. **Editing crons without re-running `lunora codegen`**: `triggers.crons` and the
191
+ dispatcher map are generated output and go stale.
192
+ 5. **Non-idempotent handlers**: a failed job is retried, and a slow cron tick
193
+ can overlap the next one.