durability 5.1.0 → 5.2.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 +145 -12
- package/dist/{durability-alarms.d.ts → alarms/index.d.ts} +13 -5
- package/dist/{durability-alarms.js → alarms/index.js} +69 -55
- package/dist/alarms/migrations.d.ts +9 -0
- package/dist/alarms/migrations.js +133 -0
- package/dist/experimental.d.ts +10 -10
- package/dist/experimental.js +7 -7
- package/dist/{fanout.d.ts → fanout/index.d.ts} +13 -6
- package/dist/{fanout.js → fanout/index.js} +54 -46
- package/dist/fanout/migrations.d.ts +9 -0
- package/dist/fanout/migrations.js +181 -0
- package/dist/{stored-message.d.ts → fanout/stored-message.d.ts} +1 -1
- package/dist/index.d.ts +14 -9
- package/dist/index.js +13 -8
- package/dist/{log.d.ts → log/index.d.ts} +15 -3
- package/dist/{log.js → log/index.js} +152 -124
- package/dist/log/migrations.d.ts +18 -0
- package/dist/log/migrations.js +297 -0
- package/dist/{memoize.d.ts → memoize/index.d.ts} +1 -1
- package/dist/{memoize.js → memoize/index.js} +14 -11
- package/dist/memoize/migrations.d.ts +10 -0
- package/dist/memoize/migrations.js +21 -0
- package/dist/migrations.d.ts +0 -111
- package/dist/migrations.js +0 -612
- package/dist/{durability.d.ts → operations/index.d.ts} +13 -5
- package/dist/{durability.js → operations/index.js} +59 -47
- package/dist/operations/migrations.d.ts +10 -0
- package/dist/operations/migrations.js +150 -0
- package/dist/{outbox.d.ts → outbox/index.d.ts} +4 -4
- package/dist/{outbox.js → outbox/index.js} +16 -60
- package/dist/outbox/migrations.d.ts +9 -0
- package/dist/outbox/migrations.js +43 -0
- package/dist/{routing-client.js → routing/client.js} +1 -1
- package/dist/{routing.d.ts → routing/index.d.ts} +3 -3
- package/dist/{routing.js → routing/index.js} +3 -3
- package/dist/{load.d.ts → routing/load.d.ts} +1 -1
- package/dist/{schedule.d.ts → schedule/index.d.ts} +16 -8
- package/dist/{schedule.js → schedule/index.js} +65 -49
- package/dist/schedule/migrations.d.ts +9 -0
- package/dist/schedule/migrations.js +118 -0
- package/dist/{scheduler.d.ts → scheduler/index.d.ts} +75 -25
- package/dist/{scheduler.js → scheduler/index.js} +300 -63
- package/dist/{semaphore-client.d.ts → semaphore/client.d.ts} +1 -1
- package/dist/{semaphore-client.js → semaphore/client.js} +2 -2
- package/dist/{semaphore.d.ts → semaphore/index.d.ts} +10 -10
- package/dist/{semaphore.js → semaphore/index.js} +7 -7
- package/dist/{semaphore-leases.d.ts → semaphore/leases.d.ts} +17 -6
- package/dist/{semaphore-leases.js → semaphore/leases.js} +28 -12
- package/dist/semaphore/migrations.d.ts +18 -0
- package/dist/semaphore/migrations.js +444 -0
- package/dist/{semaphore-protocol.d.ts → semaphore/protocol.d.ts} +16 -8
- package/dist/{semaphore-protocol.js → semaphore/protocol.js} +37 -14
- package/dist/{semaphore-queue-store.d.ts → semaphore/queue-store.d.ts} +12 -6
- package/dist/{semaphore-queue-store.js → semaphore/queue-store.js} +69 -61
- package/dist/{semaphore-queue.d.ts → semaphore/queue.d.ts} +15 -5
- package/dist/{semaphore-queue.js → semaphore/queue.js} +105 -46
- package/dist/{semaphore-server.d.ts → semaphore/server.d.ts} +12 -4
- package/dist/{semaphore-server.js → semaphore/server.js} +51 -24
- package/dist/{semaphore-store.d.ts → semaphore/store.d.ts} +8 -3
- package/dist/{semaphore-store.js → semaphore/store.js} +35 -33
- package/dist/{client.d.ts → socket/client.d.ts} +3 -3
- package/dist/{client.js → socket/client.js} +4 -4
- package/dist/{socket.d.ts → socket/index.d.ts} +15 -4
- package/dist/{socket.js → socket/index.js} +66 -20
- package/dist/socket/migrations.d.ts +9 -0
- package/dist/socket/migrations.js +57 -0
- package/dist/{socket-protocol.d.ts → socket/protocol.d.ts} +1 -1
- package/dist/storage.d.ts +4 -0
- package/dist/storage.js +13 -6
- package/dist/{topology-client.d.ts → topology/client.d.ts} +2 -2
- package/dist/{topology-client.js → topology/client.js} +3 -3
- package/dist/{topology.d.ts → topology/index.d.ts} +17 -7
- package/dist/{topology.js → topology/index.js} +92 -58
- package/dist/topology/migrations.d.ts +10 -0
- package/dist/topology/migrations.js +138 -0
- package/dist/{tracker.d.ts → tracker/index.d.ts} +3 -3
- package/dist/{tracker.js → tracker/index.js} +2 -2
- package/package.json +11 -11
- /package/dist/{stored-message.js → fanout/stored-message.js} +0 -0
- /package/dist/{routing-client.d.ts → routing/client.d.ts} +0 -0
- /package/dist/{load.js → routing/load.js} +0 -0
- /package/dist/{socket-protocol.js → socket/protocol.js} +0 -0
- /package/dist/{topology-snapshot.d.ts → topology/snapshot.d.ts} +0 -0
- /package/dist/{topology-snapshot.js → topology/snapshot.js} +0 -0
- /package/dist/{tracked-source.d.ts → tracker/source.d.ts} +0 -0
- /package/dist/{tracked-source.js → tracker/source.js} +0 -0
package/README.md
CHANGED
|
@@ -205,7 +205,69 @@ export class ImageJobs extends DurableObject<Env> {
|
|
|
205
205
|
}
|
|
206
206
|
```
|
|
207
207
|
|
|
208
|
-
A helper configured with `context` instead of `scheduler`
|
|
208
|
+
A Durable Object has one scheduler. A helper configured with `context` instead of `scheduler`, and any further `DurabilityScheduler` for the same object, joins it, so helpers never reconcile the physical alarm against only their own records. Scheduler options (`alarmConcurrency`, `alarmMinDelayMs`, `alarmHandoffMs`, and the experimental ones) can be set by whichever attaches; an option left out never conflicts, and a different value for one already set throws. The object's `alarm` method can delegate to the scheduler or to any helper's `alarm`: each runs the due work of every helper on the object.
|
|
209
|
+
|
|
210
|
+
This lets a library use durability without asking for a scheduler. It builds its helpers from the context it is given, and the application only forwards `alarm`:
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
// In a library
|
|
214
|
+
export const alerts = (ctx: DurableObjectState) => {
|
|
215
|
+
const alarms = new DurabilityAlarms({
|
|
216
|
+
context: ctx,
|
|
217
|
+
handlers: { checkAlerts: () => runChecks(ctx) },
|
|
218
|
+
});
|
|
219
|
+
return {
|
|
220
|
+
start: () => alarms.checkAlerts(Date.now()),
|
|
221
|
+
alarm: (info?: AlarmInvocationInfo) => alarms.alarm(info),
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
// In the application
|
|
226
|
+
export class Billing extends DurableObject<Env> {
|
|
227
|
+
private readonly jobs = new Durability({ context: this.ctx, handlers });
|
|
228
|
+
private readonly alerts = alerts(this.ctx);
|
|
229
|
+
|
|
230
|
+
alarm(info?: AlarmInvocationInfo) {
|
|
231
|
+
return this.jobs.alarm(info); // also runs the library's alarms
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
Install durability once per Worker, as a peer dependency of libraries that use it: a second copy reaching the same object throws instead of fighting the first over its alarm.
|
|
237
|
+
|
|
238
|
+
### Channels
|
|
239
|
+
|
|
240
|
+
`Durability`, `DurabilityAlarms`, `DurabilityFanout`, `DurabilityOutbox`, `DurabilitySchedule`, `DurabilityLog`, `DurabilitySemaphore`, `DurabilitySemaphoreQueue`, `DurabilityTopology`, and `DurabilitySocket` accept `channel`, defaulting to `default`. Each channel of a helper kind holds one instance. Instances on different channels share the scheduler but not their records: IDs and names are scoped to a channel, and each instance's operations affect only its own state. A library should use a channel of its own, so the application can use the same helper and the same IDs without either seeing the other's work:
|
|
241
|
+
|
|
242
|
+
```ts
|
|
243
|
+
// In a library
|
|
244
|
+
const jobs = new Durability({
|
|
245
|
+
context: ctx,
|
|
246
|
+
channel: 'durable-alerts',
|
|
247
|
+
handlers: { notify },
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// In the application, on the default channel
|
|
251
|
+
const jobs = new Durability({ context: this.ctx, handlers: { charge } });
|
|
252
|
+
// Both can register { id: 'invoice-1' }; they are different calls.
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
Attaching a second instance to a channel already in use throws `Durability for channel "durable-alerts" is already attached to this scheduler`. Due operations, alarms, fanout deliveries and outbox entries of a channel no instance attached are retained and postponed a minute, with a `durability.channel_missing` report. Custom-timed schedule and semaphore state is retained unchanged while its owner is absent; reattaching it reconciles any pending work. Data-only log and socket history remains stored. Channel identity is part of persisted keys; existing records and memoized results migrate to `default` without changing their IDs or names. Schema downgrades refuse to discard non-default-channel state. Deliberately remove that state before rolling back: draining or cancelling work may still leave counters or tombstones. `DurabilityAlarms` and `DurabilityOutbox` take the same `channel` option: an application and a library can both have an alarm called `check`, each with its own handler, policy, and lifecycle hook:
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
const reminders = new DurabilityAlarms({
|
|
259
|
+
context: this.ctx,
|
|
260
|
+
handlers: { check },
|
|
261
|
+
});
|
|
262
|
+
const alerts = new DurabilityAlarms({
|
|
263
|
+
context: this.ctx,
|
|
264
|
+
channel: 'durable-alerts',
|
|
265
|
+
retries: { maxAttempts: 5 },
|
|
266
|
+
handlers: { check: () => runChecks(this.ctx) },
|
|
267
|
+
});
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
Within a channel, a due alarm whose name has no handler, such as one a deploy removed, fails through that channel's instance.
|
|
209
271
|
|
|
210
272
|
## Memoize
|
|
211
273
|
|
|
@@ -377,7 +439,7 @@ The purge runs in one transaction. It best-effort aborts matching active handler
|
|
|
377
439
|
|
|
378
440
|
## Experimental options
|
|
379
441
|
|
|
380
|
-
These options, methods, and capabilities are opt-in: none takes effect unless you set, call, or construct it, so leaving them alone keeps every behavior described above. Their names and behavior may change in a minor release. Scheduler options also
|
|
442
|
+
These options, methods, and capabilities are opt-in: none takes effect unless you set, call, or construct it, so leaving them alone keeps every behavior described above. Their names and behavior may change in a minor release. Scheduler options can also be passed to a helper configured with `context`.
|
|
381
443
|
|
|
382
444
|
```ts
|
|
383
445
|
const scheduler = new DurabilityScheduler({
|
|
@@ -420,7 +482,7 @@ const scheduler = new DurabilityScheduler({
|
|
|
420
482
|
|
|
421
483
|
### `DurabilitySchedule`
|
|
422
484
|
|
|
423
|
-
`DurabilitySchedule` is exported from `durability/experimental`. It runs one recurring schedule per Durable Object: each tick starts a run, runs overlap, every run is tracked on its own, and its outcome is reported once and then forgotten. It replaces a `DurabilityAlarms` tick that registers a `Durability` operation per run, which needs retained records to deduplicate and keeps them until purged.
|
|
485
|
+
`DurabilitySchedule` is exported from `durability/experimental`. It runs one recurring schedule per channel of a Durable Object: each tick starts a run, runs overlap, every run is tracked on its own, and its outcome is reported once and then forgotten. It replaces a `DurabilityAlarms` tick that registers a `Durability` operation per run, which needs retained records to deduplicate and keeps them until purged.
|
|
424
486
|
|
|
425
487
|
```ts
|
|
426
488
|
import { DurableObject } from 'cloudflare:workers';
|
|
@@ -464,14 +526,16 @@ export class Cron extends DurableObject<Env> {
|
|
|
464
526
|
- **Jitter.** `set({ expression, version, jitterMs })` delays every tick by one fixed offset, so many objects sharing a popular expression do not all fire at the same instant. `jitterMs` is either a non-negative integer, the offset itself, or `{ maxMs }`, which picks an offset uniformly in `[0, maxMs]` when the version is stored. It defaults to 0, and both forms are at most 2^31 - 1 ms. The offset is stored in the schedule's row, so it costs no write per run, and resending the same version keeps it; only a new version picks a new one. A tick's nominal time `scheduledTime` is what `nextOccurrence` returned, and it fires at `dueAt`, which is `scheduledTime + jitterMs`. `idempotencyKey` derives from `scheduledTime`, and the next tick is always searched from nominal times, so the offset never drifts. Keep `jitterMs` below the expression's interval; durability cannot know the interval of an opaque expression. A larger offset is allowed: every tick still fires once, in nominal order, each `jitterMs` after its nominal time, even when that is after the next tick's nominal time.
|
|
465
527
|
- **Cancel.** `cancel(version?)` stops future ticks and resolves `true` when a schedule was live. Runs already due or running still finish and report. With a version, the cancel is ignored when the stored version is higher, and is otherwise kept as a one-row tombstone so a delayed `set` of that version or lower cannot revive the schedule.
|
|
466
528
|
- **One tick, one run.** A tick inserts its run in the transaction that advances the stored next tick, so a replayed or duplicated alarm finds the cursor already moved and cannot fire the tick again; deduplication needs no finished-run records. When ticks were missed, for example while the object was down, they collapse into one run for the earliest missed time, and the next tick is the first whose `dueAt` is after now. A `nextOccurrence` that throws or returns an invalid time rolls the tick back and fails the alarm run, which is retried at `alarmMinDelayMs` and reported through `onAlarmRetry`.
|
|
467
|
-
- **Runs.** `run({ scheduledTime, dueAt, idempotencyKey, signal })` starts outside the alarm pass that recorded it, so a long run never delays later ticks. `idempotencyKey` is `<objectId>:<scheduledTime
|
|
529
|
+
- **Runs.** `run({ scheduledTime, dueAt, idempotencyKey, signal })` starts outside the alarm pass that recorded it, so a long run never delays later ticks. `idempotencyKey` is `<objectId>:<scheduledTime>`; on a named channel it ends with `:` and the JSON-encoded channel name, such as `<objectId>:<scheduledTime>:"library"`, so it never equals a default-channel key or another channel's. At most `maxConcurrentRuns` (default 10) are in flight; later runs wait durably in tick order. This limit is separate from the scheduler's `alarmConcurrency`. A run that throws, or exceeds `runTimeoutMs` (default 15 minutes, which aborts its `signal`), is `errored`. A run is attempted once and never retried. A timed-out run keeps its slot until its handler actually settles.
|
|
468
530
|
- **Keeping the object alive.** While runs or outcome deliveries are in flight, the alarm pass stays pending, so an alarm invocation stays open and holds the object in memory; it fires the next ticks and outcome retries itself, at their exact times. Before the wall-time limit the scheduler hands off at `alarmHandoffMs`, and the next alarm joins the same loop. A tick started by an in-memory timer is joined by the next physical alarm. Other helpers on the same scheduler still run on each alarm pass, but work of theirs that becomes due during the hold waits for it unless the scheduler uses `liveTimers`.
|
|
469
531
|
- **Interrupted runs.** A run still marked running when no live instance holds it, for example after the object was evicted, reset, or redeployed mid-run, is reported as `errored` with `ScheduledRunInterruptedError` on the next alarm, and is not run again. While a run or outcome delivery is in flight, the physical alarm stays on the next tick, which is its eviction fallback, so an interrupted run is reported by the next tick at the latest, or within `alarmMinDelayMs` when no tick is left.
|
|
470
532
|
- **Outcomes.** `onOutcome(run, outcome)` receives `{ scheduledTime, dueAt, idempotencyKey, attempt }` and `{ status: 'complete' }` or `{ status: 'errored', error: { name, message } }`. The outcome is persisted before the first delivery, so delivery is at least once with the same outcome, even across a crash. Returning acknowledges it and deletes the run's row; nothing about the run is kept afterwards. Throwing, or exceeding `outcomeTimeoutMs` (default 60 seconds), retries it with `outcomeRetries.delay(attempt)` (default jittered exponential capped at 5 minutes) up to `outcomeRetries.maxAttempts` (default 10) deliveries. After the last, the row is deleted, `durability.schedule.outcome_dead_lettered` is reported, and a `dead_lettered` lifecycle event is emitted.
|
|
471
|
-
- **Storage.** Two
|
|
533
|
+
- **Storage.** Two tables keyed by channel with no secondary indexes: `durability_schedule` holds one row per channel's schedule and `durability_schedule_runs` holds one small row per run from its tick until its outcome is delivered. A run writes four rows: the tick's cursor update and run insert, the outcome update, and the delete on delivery. It also arms the physical alarm once, for the next tick, and each `setAlarm()` is billed as a row written. A run that waits for a slot writes one more row, and each failed delivery one more.
|
|
472
534
|
- **Lifecycle events.** `onLifecycleEvent` receives `scheduled_run` events keyed by `idempotencyKey`: `due` (with `queued`), `started`, `settled` (with `outcome`, `error`, and `durationMs`), `outcome_retry`, and `dead_lettered`. Like every lifecycle hook, it is best effort.
|
|
473
535
|
|
|
474
|
-
|
|
536
|
+
- **Channels.** `channel` (default `default`) gives each instance its own schedule, versions, cancels, runs, outcome deliveries, and hooks, so a library can keep a schedule of its own in an object whose application has one; both may tick at the same scheduled time. One instance attaches per channel; attaching a second throws `DurabilitySchedule for channel "library" is already attached to this scheduler`. A channel whose runs hold the alarm invocation open does not delay another channel's ticks. Interrupted-run recovery only looks at the instance's own channel. The schedule and runs of a channel no instance attached, such as one a deploy removed, are kept unchanged and do not arm the alarm, with a `durability.channel_missing` report; reattaching that channel restores its alarm without requiring another application write, and its next alarm pass reports interrupted runs and fires missed ticks under the rules above. The `durability_0018_schedule_channels` migration moves an existing schedule and its runs to the default channel without changing their idempotency keys. Downgrading refuses to discard the schedules, including cancelled ones, and runs of named channels; let their outcomes deliver and remove their rows before rolling back.
|
|
537
|
+
|
|
538
|
+
Construct each channel's schedule once per object instance, as with every helper. `DurabilitySchedule.migrate(this.ctx, null)` removes its schema.
|
|
475
539
|
|
|
476
540
|
## Lifecycle metrics hook
|
|
477
541
|
|
|
@@ -669,6 +733,33 @@ An inbound handler is either a bare function, which receives no input or `unknow
|
|
|
669
733
|
|
|
670
734
|
Every outbound event is persisted to `durability_socket_events` with a sequence number before it is pushed. A reconnecting client subscribes from the last sequence it saw: events still within `retention` (default 1000) are replayed in order, otherwise the client is told to resync and should rebuild its state from an inbound snapshot call. Payloads must be JSON. Keep-alive pings are answered by the runtime through `setWebSocketAutoResponse`, so idle connections never wake the object. Frames over 1 MiB are rejected; malformed frames close the connection with code `4002`.
|
|
671
735
|
|
|
736
|
+
### Socket channels
|
|
737
|
+
|
|
738
|
+
Like the other helpers, `DurabilitySocket` takes a `channel` option, `default` unless given one, and each channel holds one socket per object. A library can bring its own socket into an object that already has one, even with the same method and event names:
|
|
739
|
+
|
|
740
|
+
```ts
|
|
741
|
+
readonly librarySocket = new DurabilitySocket({
|
|
742
|
+
context: this.ctx,
|
|
743
|
+
channel: 'durable-alerts',
|
|
744
|
+
inbound: { list: () => this.alerts.list() },
|
|
745
|
+
outbound: { updated: event<Alert>() },
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
fetch(request: Request) {
|
|
749
|
+
// Which socket accepts an upgrade is up to the application.
|
|
750
|
+
return new URL(request.url).pathname.endsWith('/alerts')
|
|
751
|
+
? this.librarySocket.fetch(request)
|
|
752
|
+
: this.socket.fetch(request);
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
webSocketMessage(ws: WebSocket, message: string | ArrayBuffer) {
|
|
756
|
+
// Any socket routes a frame to the socket of the connection's channel.
|
|
757
|
+
return this.socket.webSocketMessage(ws, message);
|
|
758
|
+
}
|
|
759
|
+
```
|
|
760
|
+
|
|
761
|
+
A connection belongs to the channel of the socket whose `fetch` accepted it; the channel is stored in its WebSocket attachment next to its metadata and subscriptions, so it survives hibernation. Its calls and subscriptions run only that socket's handlers, whichever socket the object's `webSocketMessage` delegates to, and an outbound method pushes only to its own channel's subscribers. Each channel numbers, retains, and replays its events independently, so one channel's traffic never trims another's history or makes its clients resync. Connections accepted before channels existed belong to `default`. A frame from a connection whose channel no socket attached, such as one a deploy removed, closes it with code `1011` and a `durability.socket.channel_missing` report; it never reaches another channel, and the channel's events are kept for when its socket attaches again. Attaching a second socket to a channel already in use throws. Events use the composite primary key `(channel, seq)`; existing events move to `default` with their sequence numbers. A schema downgrade refuses to discard non-default-channel events: revert those channels' sockets and delete their events first. The wire protocol and browser client are unchanged; point a client at the endpoint the application routes to that channel.
|
|
762
|
+
|
|
672
763
|
The browser client derives every method and event type from the socket:
|
|
673
764
|
|
|
674
765
|
```ts
|
|
@@ -854,6 +945,9 @@ const grant = await limits.with(timeout, 5_000).request(accountId, {
|
|
|
854
945
|
|
|
855
946
|
`usage()` reports the limit, outstanding permits, lease footprint, and per-group usage. `DurabilitySemaphore.migrate(this.ctx, null)` removes the schema.
|
|
856
947
|
|
|
948
|
+
- **Pools.** One object can hold several limits: give each `DurabilitySemaphore` its own `channel` (default `default`) and expose each through its own RPC method, then build one `DurabilitySemaphore.client()` per method. Each pool sees only its own limit, groups, rounds, grants, leases, and lease provisioning; a report sent to the wrong pool is answered `not_found` and frees nothing. Attaching a second semaphore to a channel in use throws. Grants and leases of a pool no instance attached are kept, untouched, until it attaches again.
|
|
949
|
+
- **Client identity.** The holder counts a client as its `clientId` together with the optional `clientChannel` of the queue that sent the message, `default` when omitted. Two queues on one object may share a `clientId` and a pool: neither can claim, release, or settle the other's grants or leases, and each receives only its own lease updates. Default clients keep protocol version 1 and its original response shape. Named queues use version 2 with `clientChannel`; the holder echoes both the version and channel. Old holders reject version 2 before allocating permits, even if they previously understood channels and were downgraded while idle.
|
|
950
|
+
|
|
857
951
|
### Leases
|
|
858
952
|
|
|
859
953
|
Rounds cost a round trip per batch of admissions. For bursty arrivals, the holder can hand each `(client, group)` with queued demand a **lease**: a block of permits the client may draw locally, one per item, with no round trip. Where that capacity comes from is the `provisioning` mode:
|
|
@@ -876,7 +970,7 @@ new DurabilitySemaphore({
|
|
|
876
970
|
- **`under`** — a reserve inside the limit is kept for leases; grants may use at most `limit - reserve`. Still never exceeds the limit, and leases have guaranteed headroom. The reserve never takes the last permit: with a reserve at or above the limit (including after lowering the limit), grants keep one, so groups excluded from leases and clients without lease support still make progress.
|
|
877
971
|
- **`over`** — leases come from a separate budget above the limit (the original gatekeeper behaviour); the effective ceiling is `limit + extra`.
|
|
878
972
|
|
|
879
|
-
Leases are issued only to clients that report `leases` (
|
|
973
|
+
Leases are issued only to clients that report `leases` (both default and named queues do), to groups without their own limit unless `limitedGroups: 'withinGroupLimit'`, and only to a client that holds no undrawn permits for that group. Each round shares the remaining lease budget equally among candidates, oldest demand first, up to `permitsPerLease`; with fewer permits left than candidates, the oldest get one each. When a round leaves demand unserved, the holder marks its replies `contended`, and each client returns the undrawn permits of leases in groups where it has nothing queued, so an idle lease cannot keep other clients waiting. A client that stops talking to the holder keeps its lease until `ttlMs`. Reports (`drawn`, `released` high-water marks) heartbeat a lease, and the queue reports a lease with permits in use at half its lifetime even when nothing else is happening, so long-running work keeps its permits counted; a lease unreported for `ttlMs` is retired rather than forgotten: its client may be cut off from the holder with work still running, so, like a claimed grant permit, its footprint stays counted until the client reports what it drew and released. The queue keeps reporting a stopped lease until the holder settles it. A report for a lease the holder never knew is answered retired and its live footprint charged against that round. On the client, a drawn permit is one-shot: releasing the item returns footprint to the holder but never makes the permit drawable again. Lowering a group's eligibility, or the limit, retires leases on the next round. A retired lease stops drawing at once but stays counted in full until its client confirms how much it drew, since it may have drawn after its last report.
|
|
880
974
|
|
|
881
975
|
### Queueing work in a client Durable Object
|
|
882
976
|
|
|
@@ -937,9 +1031,10 @@ export class JobShard extends DurableObject<Env> {
|
|
|
937
1031
|
- **Releasing.** Call `release(id)` when the work ends, and pass `finished(ids)` so a release that never happens — a lifecycle hook is best-effort, a crash can come between the work ending and the call — does not hold the permit forever. The queue asks `finished` about each admitted item `finishedCheckMs` (default 60 seconds) after `admit()` returned, and again at that interval, releasing the ones it reports done. Without `finished`, a lost `release()` leaks the permit until the item is released by hand.
|
|
938
1032
|
- **Requests.** Each key is asked at most once per `nextRequestAt`, with demand aggregated per group from the oldest queued items. A failed request, or one unanswered after `attemptTimeoutMs`, is retried with backoff. Items whose key already holds unclaimed permits — a grant sized for demand that was cancelled, say — are admitted without a request.
|
|
939
1033
|
- **Blocked attribution.** `status(id)` on a queued item carries the holder's most recent reason for that key and group (`limit`, `group_limit`, …); `blocked` lifecycle events report counts per group. Attribution is in-memory and best-effort.
|
|
940
|
-
- **Composition.** The queue shares the scheduler with everything else in the object and creates its own outbox channel
|
|
1034
|
+
- **Composition.** The queue shares the scheduler with everything else in the object and creates its own outbox channel on it: `durability.semaphore-queue` for the default queue, and the JSON tuple `["durability.semaphore-queue","<channel>"]` for a named one. Pass `DurabilitySemaphore.client()` as `server` — caller transforms apply to every request and sync.
|
|
1035
|
+
- **Channels.** Several queues share one object through `channel` (default `default`); each channel holds one queue. Items, grants, keys, and leases are scoped to the queue, so two queues may use the same item IDs and keys. A named queue sends version-2 messages with `clientChannel`, so queues sharing a `clientId` stay apart at the holder; which holder pool a key reaches is still decided by `server`. Old version-1 holders reject these messages before allocating grants or leases. The queue checks the response version and channel before applying it: incompatible peers leave its items queued and its reports pending for retry. Upgrade holders before giving queues a channel. The default queue's wire format and outbox channel are unchanged. Records of a queue channel no instance attached are kept, untouched, until it attaches again.
|
|
941
1036
|
|
|
942
|
-
`DurabilitySemaphoreQueue.migrate(this.ctx, null)` removes the queue schema. See the typechecked [account concurrency composition](../../examples/account-concurrency).
|
|
1037
|
+
`DurabilitySemaphoreQueue.migrate(this.ctx, null)` removes the queue schema. Upgrading moves existing grants, leases, items, keys, and pending reports to the default pool, client channel, and queue without changing their IDs; a schema downgrade refuses to run while any pool, client channel, or queue other than `default` has records. See the typechecked [account concurrency composition](../../examples/account-concurrency).
|
|
943
1038
|
|
|
944
1039
|
## Routing and fanout
|
|
945
1040
|
|
|
@@ -1001,6 +1096,8 @@ const result = await routing.push([{ id: 'job:42', body: job }], {
|
|
|
1001
1096
|
|
|
1002
1097
|
The application chooses the RPC method via `invoke`; routing does not know about messages or fanout. A single push selects one shard for the entire input. Static fanout target IDs identify independent delivery obligations; each accepted message snapshots its recipients. Targets added later do not receive old work, and a removed target's work remains durable with an error and backoff until that ID is restored. Target callbacks/storage references are fixed at capability construction.
|
|
1003
1098
|
|
|
1099
|
+
Several fanouts share one scheduler and one set of tables, partitioned by `channel` (default `default`), so a library can attach its own fanout beside the application's. Target IDs, message IDs, and deduplication keys are independent in each channel, and each instance delivers, settles, memoizes, and counts `pendingDeliveries` only for its own channel, with its own targets, policies, `dlq`, and lifecycle hooks. Attaching two fanouts with the same channel to one scheduler throws. Deliveries of a channel no fanout attaches are retained and postponed, with a `durability.channel_missing` report, until that channel's fanout attaches again.
|
|
1100
|
+
|
|
1004
1101
|
### Acceptance, not completion
|
|
1005
1102
|
|
|
1006
1103
|
`fanout.enqueue` accepts a message or batch and returns:
|
|
@@ -1276,6 +1373,24 @@ applyLimits(update: TopologyStateUpdate<Limits>) {
|
|
|
1276
1373
|
|
|
1277
1374
|
`onLifecycleEvent` receives the outbox events of those channels: `channel` names the publication or state channel, and `id` is the group, or `[group, member]` as JSON for a delivery.
|
|
1278
1375
|
|
|
1376
|
+
### Instance channels
|
|
1377
|
+
|
|
1378
|
+
A `channel` option, `default` unless given one, lets several topologies share one coordinator and scheduler, for example an application's and a library's. Each channel holds one instance, with its own groups, state values, versions, publisher, and `deliver` callbacks: the same group and state names in two channels are unrelated groups, and deleting or updating one leaves the other alone. Constructing a second topology on a channel already in use throws `DurabilityTopology for channel "library" is already attached to this scheduler`, with or without a publisher.
|
|
1379
|
+
|
|
1380
|
+
```ts
|
|
1381
|
+
private readonly libraryTopology = new DurabilityTopology({
|
|
1382
|
+
scheduler: this.scheduler,
|
|
1383
|
+
channel: 'library',
|
|
1384
|
+
publish: {
|
|
1385
|
+
write: (snapshot, encoded) =>
|
|
1386
|
+
this.env.TOPOLOGY.put(`library/${snapshot.group}`, encoded),
|
|
1387
|
+
delete: (group) => this.env.TOPOLOGY.delete(`library/${group}`),
|
|
1388
|
+
},
|
|
1389
|
+
});
|
|
1390
|
+
```
|
|
1391
|
+
|
|
1392
|
+
Snapshots do not name the instance channel, and both instances record the same coordinator, so instances sharing one read store must publish under different keys, as above. The default instance keeps the outbox channels `durability.topology` and `durability.topology.<state>`; any other instance uses `["durability.topology", <channel>, "publication"]` and `["durability.topology", <channel>, "state", <state>]` as JSON, so no two instances or states can share an outbox channel, whatever their names contain. Pending publications and deliveries of an instance no longer constructed are kept and postponed, never handed to another instance. Groups, states, and write records are keyed by `owner_channel` plus their existing keys; existing rows migrate to `default` unchanged. A schema downgrade refuses to drop rows of other instance channels, including the tombstones `delete` leaves behind: delete those rows explicitly before rolling back.
|
|
1393
|
+
|
|
1279
1394
|
### Reading
|
|
1280
1395
|
|
|
1281
1396
|
Stateless Workers read through a client. It has one method:
|
|
@@ -1321,8 +1436,8 @@ export class Partition extends DurableObject<Env> {
|
|
|
1321
1436
|
// Flushed records stay readable: `write` returns a locator the log keeps,
|
|
1322
1437
|
// and `read` hands the segment back when a consumer asks for it.
|
|
1323
1438
|
cold: {
|
|
1324
|
-
write: async ({ records, firstOffset }) => {
|
|
1325
|
-
const locator = `segment/${this.ctx.id}/${firstOffset}`;
|
|
1439
|
+
write: async ({ channel = 'default', records, firstOffset }) => {
|
|
1440
|
+
const locator = `segment/${this.ctx.id}/${encodeURIComponent(channel)}/${firstOffset}`;
|
|
1326
1441
|
await this.env.COLD.put(locator, JSON.stringify(records), {
|
|
1327
1442
|
onlyIf: { etagDoesNotMatch: '*' },
|
|
1328
1443
|
});
|
|
@@ -1382,7 +1497,7 @@ type LogColdStorage<Body> = {
|
|
|
1382
1497
|
};
|
|
1383
1498
|
```
|
|
1384
1499
|
|
|
1385
|
-
`write` receives one contiguous run of records, oldest first, and returns a locator — any string identifying where the segment went. The log stores that locator in a durable segment index **before** deleting the records, so a crash in between leaves a written, readable segment rather than records pointing nowhere. A failed write propagates with the records still in the object, so configured storage cannot be skipped, and writes are at-least-once, so key segments by first offset and refuse to overwrite.
|
|
1500
|
+
`write` receives one contiguous run of records, oldest first, with the log's `channel`, and returns a locator — any string identifying where the segment went. The log stores that locator in a durable segment index **before** deleting the records, so a crash in between leaves a written, readable segment rather than records pointing nowhere. A failed write propagates with the records still in the object, so configured storage cannot be skipped, and writes are at-least-once, so key segments by first offset and refuse to overwrite.
|
|
1386
1501
|
|
|
1387
1502
|
`read` is what makes a flush **rehydratable**. A consumer reading a flushed offset gets its records back transparently, so falling behind the hot window is no longer fatal; `bounds()` reports `oldestOffset` (oldest readable, including flushed segments) alongside `hotOffset` (lowest offset still in the object). Cold reads cost a round trip and open input gates, so they serve only consumers that fell behind — never the hot path.
|
|
1388
1503
|
|
|
@@ -1416,6 +1531,24 @@ A range that keeps failing would be redelivered forever. `maxAttempts` bounds th
|
|
|
1416
1531
|
|
|
1417
1532
|
Leases are opt-in. Without the `leases` option no lease tables are created, `read`/`commit` behave exactly as before, and the lease methods throw.
|
|
1418
1533
|
|
|
1534
|
+
### Channels
|
|
1535
|
+
|
|
1536
|
+
A log attaches to a channel, `default` unless given one, and each channel holds one `DurabilityLog` per scheduler; a second instance on a channel in use throws `DurabilityLog for channel "events" is already attached to this scheduler`. Channels share the object but nothing else: each counts its own offsets from 0, and deduplication keys, consumer cursors, retention, byte budgets, cold segments, leases, batch ids, and `load()` are all scoped to the channel. A library can keep a log next to the application's, using the same keys and consumer names, and a `trim()` or lease on one never touches the other:
|
|
1537
|
+
|
|
1538
|
+
```ts
|
|
1539
|
+
const events = new DurabilityLog<Event>({ scheduler, cold });
|
|
1540
|
+
const audit = new DurabilityLog<AuditEntry>({
|
|
1541
|
+
scheduler,
|
|
1542
|
+
channel: 'audit',
|
|
1543
|
+
retention: { maxRecords: 10_000 },
|
|
1544
|
+
cold,
|
|
1545
|
+
});
|
|
1546
|
+
```
|
|
1547
|
+
|
|
1548
|
+
Because two channels both flush a segment starting at offset 0, cold storage shared between them must include `segment.channel ?? 'default'` in the location it writes to, as in the example above; otherwise one channel's segment overwrites or refuses the other's. The stored locator stays opaque and is read back only by the channel that wrote it, so locators written before channels existed keep working on `default`.
|
|
1549
|
+
|
|
1550
|
+
Records use composite keys such as `(channel, offset)` and `(channel, consumer)`; existing records, cursors, segments, leases, and allocations migrate to `default` unchanged. A log whose channel no instance attaches keeps its data and runs nothing, since a log schedules no work of its own. A schema downgrade refuses to discard a non-default channel's records, cursors, segments, offset counter, leases, or allocations.
|
|
1551
|
+
|
|
1419
1552
|
### What it is not
|
|
1420
1553
|
|
|
1421
1554
|
There is no per-record retry, timeout, or dead letter here: a log tracks positions, not attempts. When a single record must be retried independently of its neighbours, that is fanout's job, and the two compose on one object.
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { type DurabilityLifecycleEvent, type LifecycleHook } from '
|
|
2
|
-
import { type DurabilityMigrationResult } from '
|
|
3
|
-
import { type DurabilityAlarmMethodOptions, type ExecutionPolicyOptions } from '
|
|
4
|
-
import { type SchedulerAttachment } from '
|
|
5
|
-
import { type DurableMemoize } from '
|
|
1
|
+
import { type DurabilityLifecycleEvent, type LifecycleHook } from '../lifecycle.js';
|
|
2
|
+
import { type DurabilityMigrationResult } from '../migrations.js';
|
|
3
|
+
import { type DurabilityAlarmMethodOptions, type ExecutionPolicyOptions } from '../policy.js';
|
|
4
|
+
import { type SchedulerAttachment } from '../scheduler/index.js';
|
|
5
|
+
import { type DurableMemoize } from '../memoize/index.js';
|
|
6
6
|
/** Information passed to a named alarm handler for one execution attempt. */
|
|
7
7
|
export type DurableAlarmInfo = {
|
|
8
8
|
/** Configured name of the logical alarm. */
|
|
@@ -51,6 +51,14 @@ export type DurabilityAlarmsOptions<Names extends string = string> = ExecutionPo
|
|
|
51
51
|
export type DurabilityAlarmsConfig<Names extends string = string> = DurabilityAlarmsOptions<Names> & SchedulerAttachment & {
|
|
52
52
|
/** Alarm handlers keyed by the scheduling method names they become. */
|
|
53
53
|
handlers: Record<Names, DurableAlarmHandler>;
|
|
54
|
+
/**
|
|
55
|
+
* Stable channel ID. Instances on one scheduler share storage,
|
|
56
|
+
* partitioned by channel; alarm names are unique within a channel, and
|
|
57
|
+
* each instance runs, lists and purges only its own. A library should
|
|
58
|
+
* use its own channel so its alarms never meet the application's.
|
|
59
|
+
* Defaults to `default`.
|
|
60
|
+
*/
|
|
61
|
+
channel?: string;
|
|
54
62
|
};
|
|
55
63
|
/** A lifecycle event for one of this instance's named alarms. */
|
|
56
64
|
export type DurabilityAlarmEvent = Extract<DurabilityLifecycleEvent, {
|
|
@@ -9,19 +9,22 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _DurabilityAlarmsCore_instances, _DurabilityAlarmsCore_engine, _DurabilityAlarmsCore_handlers, _DurabilityAlarmsCore_policy, _DurabilityAlarmsCore_lifecycle, _DurabilityAlarmsCore_emit, _DurabilityAlarmsCore_active, _DurabilityAlarmsCore_trackedSource, _DurabilityAlarmsCore_runDue, _DurabilityAlarmsCore_schedule, _DurabilityAlarmsCore_cancel, _DurabilityAlarmsCore_execute;
|
|
12
|
+
var _DurabilityAlarmsCore_instances, _DurabilityAlarmsCore_engine, _DurabilityAlarmsCore_channel, _DurabilityAlarmsCore_table, _DurabilityAlarmsCore_handlers, _DurabilityAlarmsCore_policy, _DurabilityAlarmsCore_lifecycle, _DurabilityAlarmsCore_emit, _DurabilityAlarmsCore_active, _DurabilityAlarmsCore_trackedSource, _DurabilityAlarmsCore_runDue, _DurabilityAlarmsCore_schedule, _DurabilityAlarmsCore_cancel, _DurabilityAlarmsCore_execute;
|
|
13
13
|
import { z } from 'zod';
|
|
14
|
-
import { traceFunction, traceMethods } from '
|
|
15
|
-
import { DurableAlarmTimeoutError, DurableAttemptsExhaustedError, isErrorInstance, NonRetryableError, } from '
|
|
16
|
-
import { createLifecycleHooks, } from '
|
|
17
|
-
import { durabilityNamedAlarmMigrations
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
14
|
+
import { traceFunction, traceMethods } from '../dev.js';
|
|
15
|
+
import { DurableAlarmTimeoutError, DurableAttemptsExhaustedError, isErrorInstance, NonRetryableError, } from '../errors.js';
|
|
16
|
+
import { createLifecycleHooks, } from '../lifecycle.js';
|
|
17
|
+
import { durabilityNamedAlarmMigrations } from './migrations.js';
|
|
18
|
+
import { durabilityMemoMigrations } from '../memoize/migrations.js';
|
|
19
|
+
import { migrateCapability, } from '../migrations.js';
|
|
20
|
+
import { alarmMethodOptionsSchema, createPolicyResolver, } from '../policy.js';
|
|
21
|
+
import { assertChannel, defaultChannel, engineFor, } from '../scheduler/index.js';
|
|
22
|
+
import { createMemoize, deleteMemos, deleteMemosOfDroppedTable, memoizedKeys, } from '../memoize/index.js';
|
|
23
|
+
import { deleteAttempt, listCreated } from '../storage.js';
|
|
24
|
+
import { registerTrackedSource, } from '../tracker/source.js';
|
|
23
25
|
const alarmRowSchema = z.object({
|
|
24
26
|
name: z.string(),
|
|
27
|
+
channel: z.string(),
|
|
25
28
|
generation_id: z.string(),
|
|
26
29
|
status: z.enum(['pending', 'failed']),
|
|
27
30
|
scheduled_at: z.number().int().nonnegative(),
|
|
@@ -33,10 +36,10 @@ const alarmRowSchema = z.object({
|
|
|
33
36
|
});
|
|
34
37
|
const alarmsTable = { name: 'durability_alarms', key: 'name' };
|
|
35
38
|
const alarmHandlerSchema = z.custom((value) => typeof value === 'function');
|
|
36
|
-
const readAlarm = (sql, name) => alarmRowSchema
|
|
39
|
+
const readAlarm = (sql, channel, name) => alarmRowSchema
|
|
37
40
|
.optional()
|
|
38
41
|
.parse(sql
|
|
39
|
-
.exec('SELECT * FROM durability_alarms WHERE name = ?', name)
|
|
42
|
+
.exec('SELECT * FROM durability_alarms WHERE channel = ? AND name = ?', channel, name)
|
|
40
43
|
.toArray()[0]);
|
|
41
44
|
const alarmRecord = (sql, row) => ({
|
|
42
45
|
id: row.name,
|
|
@@ -49,7 +52,7 @@ const alarmRecord = (sql, row) => ({
|
|
|
49
52
|
attempt: row.attempt,
|
|
50
53
|
nextAttemptAt: row.next_attempt_at,
|
|
51
54
|
lastError: row.last_error,
|
|
52
|
-
memoized: memoizedKeys(sql, alarmsTable, row.name, row.generation_id),
|
|
55
|
+
memoized: memoizedKeys(sql, { ...alarmsTable, channel: row.channel }, row.name, row.generation_id),
|
|
53
56
|
}
|
|
54
57
|
: {
|
|
55
58
|
status: 'failed',
|
|
@@ -70,11 +73,15 @@ class DurabilityAlarmsCore {
|
|
|
70
73
|
constructor(config) {
|
|
71
74
|
_DurabilityAlarmsCore_instances.add(this);
|
|
72
75
|
_DurabilityAlarmsCore_engine.set(this, void 0);
|
|
76
|
+
_DurabilityAlarmsCore_channel.set(this, void 0);
|
|
77
|
+
_DurabilityAlarmsCore_table.set(this, void 0);
|
|
73
78
|
_DurabilityAlarmsCore_handlers.set(this, void 0);
|
|
74
79
|
_DurabilityAlarmsCore_policy.set(this, void 0);
|
|
75
80
|
_DurabilityAlarmsCore_lifecycle.set(this, void 0);
|
|
76
81
|
_DurabilityAlarmsCore_emit.set(this, void 0);
|
|
77
82
|
_DurabilityAlarmsCore_active.set(this, new Map());
|
|
83
|
+
__classPrivateFieldSet(this, _DurabilityAlarmsCore_channel, assertChannel('DurabilityAlarms', config.channel ?? defaultChannel), "f");
|
|
84
|
+
__classPrivateFieldSet(this, _DurabilityAlarmsCore_table, { ...alarmsTable, channel: __classPrivateFieldGet(this, _DurabilityAlarmsCore_channel, "f") }, "f");
|
|
78
85
|
__classPrivateFieldSet(this, _DurabilityAlarmsCore_engine, engineFor(config), "f");
|
|
79
86
|
__classPrivateFieldSet(this, _DurabilityAlarmsCore_handlers, new Map(Object.entries(config.handlers).map(([name, handler]) => [name, alarmHandlerSchema.parse(handler)])), "f");
|
|
80
87
|
__classPrivateFieldSet(this, _DurabilityAlarmsCore_policy, createPolicyResolver(config, config.methods ?? {}, alarmMethodOptionsSchema), "f");
|
|
@@ -95,8 +102,10 @@ class DurabilityAlarmsCore {
|
|
|
95
102
|
}
|
|
96
103
|
__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").migrateSchema('namedAlarms', durabilityNamedAlarmMigrations);
|
|
97
104
|
__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").migrateSchema('memos', durabilityMemoMigrations);
|
|
98
|
-
__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").
|
|
99
|
-
table:
|
|
105
|
+
__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").registerChannel({
|
|
106
|
+
table: __classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"),
|
|
107
|
+
channel: __classPrivateFieldGet(this, _DurabilityAlarmsCore_channel, "f"),
|
|
108
|
+
helper: 'DurabilityAlarms',
|
|
100
109
|
runDue: (now, info) => __classPrivateFieldGet(this, _DurabilityAlarmsCore_instances, "m", _DurabilityAlarmsCore_runDue).call(this, now, info),
|
|
101
110
|
});
|
|
102
111
|
registerTrackedSource(this, __classPrivateFieldGet(this, _DurabilityAlarmsCore_instances, "m", _DurabilityAlarmsCore_trackedSource).call(this));
|
|
@@ -123,7 +132,7 @@ class DurabilityAlarmsCore {
|
|
|
123
132
|
* timestamp, regardless of status, and returns how many were removed.
|
|
124
133
|
*/
|
|
125
134
|
async purgeBefore(before) {
|
|
126
|
-
const count = await __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").purge(
|
|
135
|
+
const count = await __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").purge(__classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), before, __classPrivateFieldGet(this, _DurabilityAlarmsCore_active, "f"), 'Durable alarm was purged');
|
|
127
136
|
__classPrivateFieldGet(this, _DurabilityAlarmsCore_emit, "f").call(this, {
|
|
128
137
|
type: 'purged',
|
|
129
138
|
entityKind: 'named_alarm',
|
|
@@ -134,15 +143,15 @@ class DurabilityAlarmsCore {
|
|
|
134
143
|
return count;
|
|
135
144
|
}
|
|
136
145
|
}
|
|
137
|
-
_DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_handlers = new WeakMap(), _DurabilityAlarmsCore_policy = new WeakMap(), _DurabilityAlarmsCore_lifecycle = new WeakMap(), _DurabilityAlarmsCore_emit = new WeakMap(), _DurabilityAlarmsCore_active = new WeakMap(), _DurabilityAlarmsCore_instances = new WeakSet(), _DurabilityAlarmsCore_trackedSource = function _DurabilityAlarmsCore_trackedSource() {
|
|
146
|
+
_DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_channel = new WeakMap(), _DurabilityAlarmsCore_table = new WeakMap(), _DurabilityAlarmsCore_handlers = new WeakMap(), _DurabilityAlarmsCore_policy = new WeakMap(), _DurabilityAlarmsCore_lifecycle = new WeakMap(), _DurabilityAlarmsCore_emit = new WeakMap(), _DurabilityAlarmsCore_active = new WeakMap(), _DurabilityAlarmsCore_instances = new WeakSet(), _DurabilityAlarmsCore_trackedSource = function _DurabilityAlarmsCore_trackedSource() {
|
|
138
147
|
const sql = __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").sql;
|
|
139
148
|
return {
|
|
140
149
|
get: async (name) => {
|
|
141
|
-
const row = readAlarm(sql, name);
|
|
150
|
+
const row = readAlarm(sql, __classPrivateFieldGet(this, _DurabilityAlarmsCore_channel, "f"), name);
|
|
142
151
|
return row && alarmRecord(sql, row);
|
|
143
152
|
},
|
|
144
153
|
list: async ({ limit, cursor, status, name }) => {
|
|
145
|
-
const page = listCreated(sql,
|
|
154
|
+
const page = listCreated(sql, __classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), 'name', {
|
|
146
155
|
limit,
|
|
147
156
|
after: cursor,
|
|
148
157
|
status,
|
|
@@ -156,26 +165,28 @@ _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_handlers = n
|
|
|
156
165
|
? { records }
|
|
157
166
|
: { records, cursor: page.next };
|
|
158
167
|
},
|
|
159
|
-
watch: (name, listener) =>
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
168
|
+
watch: (name, listener) => {
|
|
169
|
+
return __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").watch(__classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), name, (completed) => {
|
|
170
|
+
if (!completed) {
|
|
171
|
+
listener();
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
listener({
|
|
175
|
+
id: name,
|
|
176
|
+
name,
|
|
177
|
+
generation: completed.generation_id,
|
|
178
|
+
createdAt: completed.created_at,
|
|
179
|
+
state: { status: 'completed', result: undefined },
|
|
180
|
+
});
|
|
170
181
|
});
|
|
171
|
-
}
|
|
182
|
+
},
|
|
172
183
|
};
|
|
173
184
|
}, _DurabilityAlarmsCore_runDue = async function _DurabilityAlarmsCore_runDue(now, info) {
|
|
174
185
|
const due = alarmRowSchema.array().parse(__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").sql
|
|
175
186
|
.exec(`SELECT * FROM durability_alarms
|
|
176
|
-
WHERE status = 'pending' AND next_attempt_at <= ?
|
|
187
|
+
WHERE channel = ? AND status = 'pending' AND next_attempt_at <= ?
|
|
177
188
|
ORDER BY next_attempt_at ASC
|
|
178
|
-
LIMIT 100`, now)
|
|
189
|
+
LIMIT 100`, __classPrivateFieldGet(this, _DurabilityAlarmsCore_channel, "f"), now)
|
|
179
190
|
.toArray());
|
|
180
191
|
// With liveTimers a running occurrence re-arms itself when it settles, so
|
|
181
192
|
// skipping it keeps a long handler from blocking a concurrent alarm pass.
|
|
@@ -198,14 +209,15 @@ _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_handlers = n
|
|
|
198
209
|
if (!Number.isSafeInteger(scheduledTime) || scheduledTime < 0) {
|
|
199
210
|
throw new RangeError('scheduledTime must be a non-negative safe integer');
|
|
200
211
|
}
|
|
212
|
+
const key = name;
|
|
201
213
|
const generation = crypto.randomUUID();
|
|
202
214
|
const createdAt = Date.now();
|
|
203
215
|
await __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").transaction((sql) => {
|
|
204
216
|
sql.exec(`INSERT INTO durability_alarms
|
|
205
|
-
(name, generation_id, status, scheduled_at,
|
|
206
|
-
attempt, last_error, last_error_name, created_at)
|
|
207
|
-
VALUES (?, ?, 'pending', ?, ?, 0, NULL, NULL, ?)
|
|
208
|
-
ON CONFLICT(name) DO UPDATE SET
|
|
217
|
+
(name, channel, generation_id, status, scheduled_at,
|
|
218
|
+
next_attempt_at, attempt, last_error, last_error_name, created_at)
|
|
219
|
+
VALUES (?, ?, ?, 'pending', ?, ?, 0, NULL, NULL, ?)
|
|
220
|
+
ON CONFLICT(channel, name) DO UPDATE SET
|
|
209
221
|
generation_id = excluded.generation_id,
|
|
210
222
|
status = excluded.status,
|
|
211
223
|
scheduled_at = excluded.scheduled_at,
|
|
@@ -213,12 +225,12 @@ _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_handlers = n
|
|
|
213
225
|
attempt = excluded.attempt,
|
|
214
226
|
last_error = excluded.last_error,
|
|
215
227
|
last_error_name = excluded.last_error_name,
|
|
216
|
-
created_at = excluded.created_at`,
|
|
228
|
+
created_at = excluded.created_at`, key, __classPrivateFieldGet(this, _DurabilityAlarmsCore_channel, "f"), generation, scheduledTime, scheduledTime, createdAt);
|
|
217
229
|
// Memoized results belong to the replaced occurrence, never to the new one.
|
|
218
|
-
deleteMemos(sql,
|
|
230
|
+
deleteMemos(sql, __classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), key);
|
|
219
231
|
});
|
|
220
232
|
// A waiter on the replaced occurrence resolves now instead of at its timeout.
|
|
221
|
-
__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").notify(
|
|
233
|
+
__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").notify(__classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), key);
|
|
222
234
|
__classPrivateFieldGet(this, _DurabilityAlarmsCore_emit, "f").call(this, {
|
|
223
235
|
type: 'scheduled',
|
|
224
236
|
entityKind: 'named_alarm',
|
|
@@ -230,14 +242,15 @@ _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_handlers = n
|
|
|
230
242
|
scheduledTime,
|
|
231
243
|
});
|
|
232
244
|
}, _DurabilityAlarmsCore_cancel = async function _DurabilityAlarmsCore_cancel(name) {
|
|
245
|
+
const key = name;
|
|
233
246
|
const cancelled = await __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").transaction((sql) => {
|
|
234
247
|
const removed = sql
|
|
235
248
|
.exec(`DELETE FROM durability_alarms
|
|
236
|
-
WHERE name = ? AND status = 'pending'
|
|
237
|
-
RETURNING name`,
|
|
249
|
+
WHERE channel = ? AND name = ? AND status = 'pending'
|
|
250
|
+
RETURNING name`, __classPrivateFieldGet(this, _DurabilityAlarmsCore_channel, "f"), key)
|
|
238
251
|
.toArray().length > 0;
|
|
239
252
|
if (removed) {
|
|
240
|
-
deleteMemos(sql,
|
|
253
|
+
deleteMemos(sql, __classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), key);
|
|
241
254
|
}
|
|
242
255
|
return removed;
|
|
243
256
|
});
|
|
@@ -247,42 +260,43 @@ _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_handlers = n
|
|
|
247
260
|
// Every settlement write is conditional on the deleted row, so a running
|
|
248
261
|
// attempt can no longer retry; abort it as purging does.
|
|
249
262
|
__classPrivateFieldGet(this, _DurabilityAlarmsCore_active, "f")
|
|
250
|
-
.get(
|
|
263
|
+
.get(key)
|
|
251
264
|
?.controller?.abort(new Error(`Durable alarm "${name}" was cancelled`));
|
|
252
|
-
__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").notify(
|
|
265
|
+
__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").notify(__classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), key);
|
|
253
266
|
return true;
|
|
254
267
|
}, _DurabilityAlarmsCore_execute = function _DurabilityAlarmsCore_execute(row, platform) {
|
|
255
268
|
const running = __classPrivateFieldGet(this, _DurabilityAlarmsCore_active, "f").get(row.name);
|
|
256
269
|
if (running) {
|
|
257
270
|
return running.settled;
|
|
258
271
|
}
|
|
259
|
-
const
|
|
272
|
+
const name = row.name;
|
|
273
|
+
const policy = __classPrivateFieldGet(this, _DurabilityAlarmsCore_policy, "f").call(this, name);
|
|
260
274
|
const retryTimeouts = policy.method?.retryTimeouts ?? false;
|
|
261
275
|
return __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").startExecution({
|
|
262
|
-
table:
|
|
276
|
+
table: __classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"),
|
|
263
277
|
key: row.name,
|
|
264
278
|
row,
|
|
265
|
-
label: `named alarm "${
|
|
279
|
+
label: `named alarm "${name}"`,
|
|
266
280
|
entity: alarmEntity(row),
|
|
267
281
|
active: __classPrivateFieldGet(this, _DurabilityAlarmsCore_active, "f"),
|
|
268
282
|
policy,
|
|
269
283
|
emit: __classPrivateFieldGet(this, _DurabilityAlarmsCore_emit, "f"),
|
|
270
|
-
exhaustedError: () => new DurableAttemptsExhaustedError('named alarm',
|
|
271
|
-
timeoutError: () => new DurableAlarmTimeoutError(
|
|
284
|
+
exhaustedError: () => new DurableAttemptsExhaustedError('named alarm', name, policy.maxAttempts),
|
|
285
|
+
timeoutError: () => new DurableAlarmTimeoutError(name, policy.attemptTimeoutMs),
|
|
272
286
|
invoke: (attempt, signal) => {
|
|
273
|
-
const handler = __classPrivateFieldGet(this, _DurabilityAlarmsCore_handlers, "f").get(
|
|
287
|
+
const handler = __classPrivateFieldGet(this, _DurabilityAlarmsCore_handlers, "f").get(name);
|
|
274
288
|
if (!handler) {
|
|
275
|
-
throw new NonRetryableError(`No named alarm handler registered for "${
|
|
289
|
+
throw new NonRetryableError(`No named alarm handler registered for "${name}"`);
|
|
276
290
|
}
|
|
277
291
|
return handler({
|
|
278
|
-
name
|
|
292
|
+
name,
|
|
279
293
|
scheduledTime: row.scheduled_at,
|
|
280
294
|
attempt,
|
|
281
295
|
isRetry: attempt > 1,
|
|
282
296
|
retryCount: attempt - 1,
|
|
283
297
|
idempotencyKey: `durability-alarm:v1:${row.generation_id}`,
|
|
284
298
|
signal,
|
|
285
|
-
memoize: createMemoize(__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").sql,
|
|
299
|
+
memoize: createMemoize(__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").sql, __classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), {
|
|
286
300
|
key: row.name,
|
|
287
301
|
generation: row.generation_id,
|
|
288
302
|
attempt,
|
|
@@ -292,7 +306,7 @@ _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_handlers = n
|
|
|
292
306
|
},
|
|
293
307
|
complete: (_result, attempt) => {
|
|
294
308
|
const timestamp = Date.now();
|
|
295
|
-
const removed = deleteAttempt(__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").sql,
|
|
309
|
+
const removed = deleteAttempt(__classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").sql, __classPrivateFieldGet(this, _DurabilityAlarmsCore_table, "f"), {
|
|
296
310
|
key: row.name,
|
|
297
311
|
generation: row.generation_id,
|
|
298
312
|
attempt,
|