@coji/durably 0.11.0 → 0.13.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.
@@ -1,3 +1,3 @@
1
- export { N as withLogPersistence } from '../index-fppJjkF-.js';
1
+ export { V as withLogPersistence } from '../index-DWsJlgyh.js';
2
2
  import 'kysely';
3
3
  import 'zod';
package/docs/llms.md CHANGED
@@ -32,9 +32,11 @@ const dialect = new LibsqlDialect({ client })
32
32
  // Option 1: With jobs (1-step initialization, returns typed instance)
33
33
  const durably = createDurably({
34
34
  dialect,
35
- pollingInterval: 1000, // Job polling interval (ms)
36
- heartbeatInterval: 5000, // Heartbeat update interval (ms)
37
- staleThreshold: 30000, // When to consider a job abandoned (ms)
35
+ pollingIntervalMs: 1000, // Job polling interval (ms)
36
+ leaseRenewIntervalMs: 5000, // Lease renewal interval (ms)
37
+ leaseMs: 30000, // Lease duration (ms); expired leases are reclaimed
38
+ preserveSteps: false, // Set to true to keep step output data after terminal state (default: false = cleanup)
39
+ retainRuns: '30d', // Auto-delete terminal runs older than 30 days (runs during worker polling; supports 'd', 'h', 'm' units)
38
40
  // Optional: type-safe labels with Zod schema
39
41
  // labels: z.object({ organizationId: z.string(), env: z.string() }),
40
42
  jobs: {
@@ -206,10 +208,14 @@ type MyRun = Run & {
206
208
  const typedRuns = await durably.getRuns<MyRun>({ jobName: 'my-job' })
207
209
  ```
208
210
 
209
- ### Retry Failed Runs
211
+ ### Retrigger Failed Runs
210
212
 
211
213
  ```ts
212
- await durably.retry(runId)
214
+ // Creates a fresh run (new ID) with the same input and labels
215
+ // Input is validated against the current job schema — throws if incompatible
216
+ // Note: idempotencyKey is not carried forward
217
+ const newRun = await durably.retrigger(runId)
218
+ console.log(newRun.id) // new run ID
213
219
  ```
214
220
 
215
221
  ### Cancel Runs
@@ -224,6 +230,21 @@ await durably.cancel(runId)
224
230
  await durably.deleteRun(runId)
225
231
  ```
226
232
 
233
+ ### Purge Old Runs
234
+
235
+ Batch-delete terminal runs (completed, failed, cancelled) older than a cutoff date.
236
+ Pending and leased runs are never deleted.
237
+
238
+ ```ts
239
+ // Delete terminal runs older than 30 days
240
+ const deleted = await durably.purgeRuns({
241
+ olderThan: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000),
242
+ limit: 500, // optional batch size (default: 500)
243
+ })
244
+ ```
245
+
246
+ For automatic cleanup, use the `retainRuns` option (see Core Concepts). Cleanup runs during idle worker polling cycles, at most once per minute, in batches of 100.
247
+
227
248
  ## Events
228
249
 
229
250
  Subscribe to job execution events:
@@ -231,12 +252,11 @@ Subscribe to job execution events:
231
252
  ```ts
232
253
  // Run lifecycle events
233
254
  durably.on('run:trigger', (e) => console.log('Triggered:', e.runId))
234
- durably.on('run:start', (e) => console.log('Started:', e.runId))
255
+ durably.on('run:leased', (e) => console.log('Leased:', e.runId))
235
256
  durably.on('run:complete', (e) => console.log('Done:', e.output))
236
257
  durably.on('run:fail', (e) => console.error('Failed:', e.error))
237
258
  durably.on('run:cancel', (e) => console.log('Cancelled:', e.runId))
238
259
  durably.on('run:delete', (e) => console.log('Deleted:', e.runId))
239
- durably.on('run:retry', (e) => console.log('Retried:', e.runId))
240
260
  durably.on('run:progress', (e) =>
241
261
  console.log('Progress:', e.progress.current, '/', e.progress.total),
242
262
  )
@@ -277,8 +297,8 @@ while (true) {
277
297
  if (done) break
278
298
 
279
299
  switch (value.type) {
280
- case 'run:start':
281
- console.log('Started')
300
+ case 'run:leased':
301
+ console.log('Leased')
282
302
  break
283
303
  case 'run:complete':
284
304
  console.log('Completed:', value.output)
@@ -336,7 +356,7 @@ const handler = createDurablyHandler(durably, {
336
356
  }
337
357
  },
338
358
 
339
- // Guard before run-level operations (read, subscribe, steps, retry, cancel, delete)
359
+ // Guard before run-level operations (read, subscribe, steps, retrigger, cancel, delete)
340
360
  onRunAccess: async (ctx, run, { operation }) => {
341
361
  if (run.labels.organizationId !== ctx.orgId) {
342
362
  throw new Response('Forbidden', { status: 403 })
@@ -365,7 +385,7 @@ GET /runs?label.organizationId=org_123
365
385
  GET /runs/subscribe?label.organizationId=org_123&label.env=prod
366
386
  ```
367
387
 
368
- **Response Shape:** The `/runs` and `/run` endpoints return `ClientRun` objects (internal fields like `heartbeatAt`, `idempotencyKey`, `concurrencyKey`, `updatedAt` are stripped). Use `toClientRun()` to apply the same projection in custom code:
388
+ **Response Shape:** The `/runs` and `/run` endpoints return `ClientRun` objects (internal fields like `leaseOwner`, `leaseExpiresAt`, `idempotencyKey`, `concurrencyKey`, `updatedAt` are stripped). Use `toClientRun()` to apply the same projection in custom code:
369
389
 
370
390
  ```ts
371
391
  import { toClientRun } from '@coji/durably'
@@ -418,7 +438,7 @@ type RunOperation =
418
438
  | 'read'
419
439
  | 'subscribe'
420
440
  | 'steps'
421
- | 'retry'
441
+ | 'retrigger'
422
442
  | 'cancel'
423
443
  | 'delete'
424
444
 
@@ -458,9 +478,9 @@ const { dialect } = new SQLocalKysely('app.sqlite3')
458
478
 
459
479
  const durably = createDurably({
460
480
  dialect,
461
- pollingInterval: 100,
462
- heartbeatInterval: 500,
463
- staleThreshold: 3000,
481
+ pollingIntervalMs: 100,
482
+ leaseRenewIntervalMs: 500,
483
+ leaseMs: 3000,
464
484
  jobs: {
465
485
  myJob: defineJob({
466
486
  name: 'my-job',
@@ -479,13 +499,13 @@ await durably.init()
479
499
  ## Run Lifecycle
480
500
 
481
501
  ```text
482
- trigger() → pending → running → completed
483
-
502
+ trigger() → pending → leased → completed
503
+
484
504
  → failed
485
505
  ```
486
506
 
487
507
  - **pending**: Waiting for worker to pick up
488
- - **running**: Worker is executing steps
508
+ - **leased**: Worker has acquired a lease and is executing steps
489
509
  - **completed**: All steps finished successfully
490
510
  - **failed**: A step threw an error
491
511
  - **cancelled**: Manually cancelled via `cancel()`
@@ -526,7 +546,7 @@ interface StepContext {
526
546
  interface Run<TLabels extends Record<string, string> = Record<string, string>> {
527
547
  id: string
528
548
  jobName: string
529
- status: 'pending' | 'running' | 'completed' | 'failed' | 'cancelled'
549
+ status: 'pending' | 'leased' | 'completed' | 'failed' | 'cancelled'
530
550
  input: unknown
531
551
  labels: TLabels
532
552
  output: unknown | null
@@ -601,7 +621,7 @@ interface LogData {
601
621
  interface RunFilter<
602
622
  TLabels extends Record<string, string> = Record<string, string>,
603
623
  > {
604
- status?: 'pending' | 'running' | 'completed' | 'failed' | 'cancelled'
624
+ status?: 'pending' | 'leased' | 'completed' | 'failed' | 'cancelled'
605
625
  jobName?: string | string[]
606
626
  labels?: Partial<TLabels>
607
627
  limit?: number
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coji/durably",
3
- "version": "0.11.0",
3
+ "version": "0.13.0",
4
4
  "description": "Step-oriented resumable batch execution for Node.js and browsers using SQLite",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -20,6 +20,20 @@
20
20
  "docs",
21
21
  "README.md"
22
22
  ],
23
+ "scripts": {
24
+ "build": "tsup",
25
+ "test": "pnpm test:node && pnpm test:react && pnpm test:browser",
26
+ "test:node": "vitest run --config vitest.config.ts --exclude 'tests/node/**/*.postgres.test.ts'",
27
+ "test:node:postgres": "vitest run --config vitest.config.ts postgres",
28
+ "test:node:all": "vitest run --config vitest.config.ts",
29
+ "test:react": "vitest run --config vitest.react.config.ts",
30
+ "test:browser": "vitest run --config vitest.browser.config.ts",
31
+ "typecheck": "tsc --noEmit",
32
+ "lint": "biome lint .",
33
+ "lint:fix": "biome lint --write .",
34
+ "format": "prettier --experimental-cli --check .",
35
+ "format:fix": "prettier --experimental-cli --write ."
36
+ },
23
37
  "keywords": [
24
38
  "batch",
25
39
  "job",
@@ -45,20 +59,24 @@
45
59
  "zod": "^4.0.0"
46
60
  },
47
61
  "dependencies": {
62
+ "better-sqlite3": "12.6.2",
48
63
  "ulidx": "^2.4.1"
49
64
  },
50
65
  "devDependencies": {
51
- "@biomejs/biome": "^2.4.6",
66
+ "@biomejs/biome": "^2.4.7",
52
67
  "@libsql/client": "^0.17.0",
53
68
  "@libsql/kysely-libsql": "^0.4.1",
54
69
  "@testing-library/react": "^16.3.2",
70
+ "@types/better-sqlite3": "^7.6.13",
71
+ "@types/pg": "^8.15.6",
55
72
  "@types/react": "^19.2.14",
56
73
  "@types/react-dom": "^19.2.3",
57
- "@vitejs/plugin-react": "^5.1.4",
58
- "@vitest/browser": "^4.0.18",
59
- "@vitest/browser-playwright": "4.0.18",
60
- "jsdom": "^28.1.0",
61
- "kysely": "^0.28.11",
74
+ "@vitejs/plugin-react": "^6.0.1",
75
+ "@vitest/browser": "^4.1.0",
76
+ "@vitest/browser-playwright": "4.1.0",
77
+ "jsdom": "^29.0.0",
78
+ "kysely": "^0.28.12",
79
+ "pg": "^8.16.3",
62
80
  "playwright": "^1.58.2",
63
81
  "prettier": "^3.8.1",
64
82
  "prettier-plugin-organize-imports": "^4.3.0",
@@ -67,19 +85,7 @@
67
85
  "sqlocal": "^0.17.0",
68
86
  "tsup": "^8.5.1",
69
87
  "typescript": "^5.9.3",
70
- "vitest": "^4.0.18",
88
+ "vitest": "^4.1.0",
71
89
  "zod": "^4.3.6"
72
- },
73
- "scripts": {
74
- "build": "tsup",
75
- "test": "pnpm test:node && pnpm test:react && pnpm test:browser",
76
- "test:node": "vitest run --config vitest.config.ts",
77
- "test:react": "vitest run --config vitest.react.config.ts",
78
- "test:browser": "vitest run --config vitest.browser.config.ts",
79
- "typecheck": "tsc --noEmit",
80
- "lint": "biome lint .",
81
- "lint:fix": "biome lint --write .",
82
- "format": "prettier --experimental-cli --check .",
83
- "format:fix": "prettier --experimental-cli --write ."
84
90
  }
85
- }
91
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 coji
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.