nuxt-cf-jobs 0.2.0 → 0.4.0

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.
package/README.md CHANGED
@@ -73,13 +73,13 @@ jobsIgnore: ['**/_*.ts', '**/*.d.ts', '**/*.test.ts', '**/*.spec.ts']
73
73
  The generated registry is available as `#cf-jobs/app` by default.
74
74
 
75
75
  ```ts
76
+ import type { JobName, JobPayload } from '#cf-jobs/app'
76
77
  import {
77
78
  buildJobPayload,
78
79
  getJobDefinition,
79
80
  getQueue,
80
- prepareJob,
81
- type JobName,
82
- type JobPayload,
81
+
82
+ prepareJob
83
83
  } from '#cf-jobs/app'
84
84
 
85
85
  const name: JobName = 'sync/table'
@@ -201,8 +201,7 @@ export default defineEventHandler(async (event) => {
201
201
  await repository.migrate()
202
202
 
203
203
  const publisher = createQueuePublisher(env, queue =>
204
- queue === 'default' ? 'QUEUE_DEFAULT' : undefined,
205
- )
204
+ queue === 'default' ? 'QUEUE_DEFAULT' : undefined,)
206
205
 
207
206
  const record = await prepareJob({
208
207
  name: 'sync/table',
@@ -220,8 +219,8 @@ export default defineEventHandler(async (event) => {
220
219
  Consume durable queue messages with `runDurableJobMessage()` and the D1 repository:
221
220
 
222
221
  ```ts
223
- import { runDurableJobMessage } from 'nuxt-cf-jobs/durable'
224
222
  import { createD1DurableJobRepository } from 'nuxt-cf-jobs/d1'
223
+ import { runDurableJobMessage } from 'nuxt-cf-jobs/durable'
225
224
  import { jobRegistry } from '#cf-jobs/app'
226
225
 
227
226
  export default defineNitroPlugin((nitroApp) => {
@@ -276,6 +275,71 @@ export default defineNitroPlugin((nitroApp) => {
276
275
  - `releaseStaleReservedJobs()`
277
276
  - `toDispatchableJob()`
278
277
 
278
+ ## Scheduled Tasks (cron)
279
+
280
+ Queue jobs handle per-request deferred work; **scheduled tasks** handle cron work. `defineScheduledTask` co-locates the cron schedule with its handler, and the module derives `nitro.tasks`, `nitro.scheduledTasks`, and the Cloudflare `triggers.crons` from it — so there is no central list to keep in sync (and no way for the three to silently drift apart).
281
+
282
+ ```ts
283
+ // server/tasks/cleanup.ts
284
+ export default defineScheduledTask({
285
+ name: 'db:cleanup', // nitro task name (also the runTask id)
286
+ cron: '0 3 * * *', // or cron: ['0 3 * * *', '0 */6 * * *']
287
+ description: 'Nightly cleanup',
288
+ run() {
289
+ // ...same shape as nitro defineTask's run
290
+ return { result: 'ok' }
291
+ },
292
+ })
293
+ ```
294
+
295
+ Enable scanning via `cfJobs.tasksDir`:
296
+
297
+ ```ts
298
+ export default defineNuxtConfig({
299
+ cfJobs: {
300
+ // `true` → auto-discover `server/tasks` in the app AND every extended layer
301
+ // (nuxt.options._layers), so a new layer with cron work needs no host config.
302
+ tasksDir: true,
303
+ // …or be explicit: tasksDir: ['server/tasks', '../some-layer/server/tasks']
304
+ },
305
+ })
306
+ ```
307
+
308
+ Notes:
309
+
310
+ - `name` and `cron` must be **string literals** — the module reads them statically at build time (without executing the file, which typically imports a DB/server utils that won't load outside nitro). Computed values are skipped with a warning.
311
+ - Plain nitro `defineTask` files in the same dirs are still registered (so they're runnable via `runTask`), just not scheduled.
312
+ - `nitro.scheduledTasks` is populated only outside dev by default (so crons don't fire locally); override with `cfJobs.scheduledTasks: true | false`. The deploy-only `triggers.crons` is always written.
313
+ - Opt-in: nothing is scanned or registered unless `tasksDir` is set.
314
+
315
+ ## CLI
316
+
317
+ The package ships a `cf-jobs` binary, an `artisan queue:*`-style tool for inspecting and managing the durable D1 job tables. It queries D1 through `wrangler d1 execute`, so it works against both the local miniflare database (default) and production (`--remote`), auto-detecting the D1 binding from your wrangler config.
318
+
319
+ ```bash
320
+ # backpressure overview: per-queue ready/reserved/delayed, ready-lag, failures, stuck reservations
321
+ pnpm cf-jobs # alias for `cf-jobs status`
322
+ pnpm cf-jobs status --remote
323
+
324
+ # inspect jobs
325
+ pnpm cf-jobs jobs --queue billing --state ready --limit 20
326
+ pnpm cf-jobs failed # artisan queue:failed
327
+ pnpm cf-jobs schedule # artisan schedule:list (cron + next run)
328
+ pnpm cf-jobs tasks # every discovered task
329
+
330
+ # manage (prompt for confirmation; pass --yes to skip, required when non-interactive)
331
+ pnpm cf-jobs retry <id> # artisan queue:retry — re-queue a failed job
332
+ pnpm cf-jobs retry --queue billing # re-queue a whole queue's failures
333
+ pnpm cf-jobs forget <id> # artisan queue:forget
334
+ pnpm cf-jobs flush # artisan queue:flush — delete all failed jobs
335
+ pnpm cf-jobs clear --state reserved # artisan queue:clear — drop active jobs (e.g. stuck reservations)
336
+ pnpm cf-jobs migrate # create the job tables/indexes in D1
337
+ ```
338
+
339
+ Every command accepts `--config <wrangler path>`, `--db <binding>`, `--remote`, `--json`, and `--jobs-table` / `--failed-table` overrides. `status` flags queues whose oldest ready job is lagging and reservations stuck for more than five minutes (a crashed or timed-out consumer). Run `cf-jobs <command> --help` for the full argument list.
340
+
341
+ `cf-jobs` shells out to `wrangler`; it resolves the binary from `node_modules/.bin`, falling back to `wrangler` on `PATH` (override with `CF_JOBS_WRANGLER_BIN`).
342
+
279
343
  ## Runtime Validation
280
344
 
281
345
  The generated registry validates jobs at startup. It fails loudly for:
@@ -298,9 +362,9 @@ assertQueueBindings()
298
362
  Use the narrow subpaths when you can:
299
363
 
300
364
  ```ts
301
- import { defineJob } from 'nuxt-cf-jobs/server'
302
- import { runDurableJobMessage } from 'nuxt-cf-jobs/durable'
303
365
  import { createD1DurableJobRepository } from 'nuxt-cf-jobs/d1'
366
+ import { runDurableJobMessage } from 'nuxt-cf-jobs/durable'
367
+ import { defineJob } from 'nuxt-cf-jobs/server'
304
368
  import { createFakeQueue } from 'nuxt-cf-jobs/testing'
305
369
  ```
306
370
 
@@ -0,0 +1 @@
1
+