durability 5.1.0 → 5.2.1
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 +154 -21
- package/dist/{durability-alarms.d.ts → alarms/index.d.ts} +13 -5
- package/dist/{durability-alarms.js → alarms/index.js} +85 -74
- 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} +91 -83
- 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} +27 -20
- package/dist/{log.js → log/index.js} +200 -166
- 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} +23 -21
- 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 +12 -628
- package/dist/{durability.d.ts → operations/index.d.ts} +13 -5
- package/dist/{durability.js → operations/index.js} +76 -70
- 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} +35 -77
- package/dist/outbox/migrations.d.ts +9 -0
- package/dist/outbox/migrations.js +43 -0
- package/dist/policy.d.ts +19 -19
- package/dist/policy.js +13 -12
- 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} +97 -83
- 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} +308 -70
- package/dist/{semaphore-client.d.ts → semaphore/client.d.ts} +1 -1
- package/dist/{semaphore-client.js → semaphore/client.js} +8 -7
- 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 +451 -0
- package/dist/semaphore/protocol.js +114 -0
- package/dist/semaphore/queue-store.d.ts +107 -0
- package/dist/semaphore/queue-store.js +258 -0
- 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} +56 -28
- package/dist/semaphore/store.d.ts +53 -0
- package/dist/semaphore/store.js +107 -0
- package/dist/serialization.d.ts +5 -0
- package/dist/serialization.js +41 -16
- 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 +22 -15
- 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} +106 -72
- package/dist/topology/migrations.d.ts +10 -0
- package/dist/topology/migrations.js +138 -0
- package/dist/topology/snapshot.d.ts +50 -0
- package/dist/topology/snapshot.js +38 -0
- package/dist/{tracker.d.ts → tracker/index.d.ts} +3 -3
- package/dist/{tracker.js → tracker/index.js} +9 -9
- package/package.json +11 -11
- package/dist/semaphore-protocol.d.ts +0 -231
- package/dist/semaphore-protocol.js +0 -106
- package/dist/semaphore-queue-store.d.ts +0 -95
- package/dist/semaphore-queue-store.js +0 -252
- package/dist/semaphore-store.d.ts +0 -43
- package/dist/semaphore-store.js +0 -108
- package/dist/topology-snapshot.d.ts +0 -70
- package/dist/topology-snapshot.js +0 -48
- /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/{tracked-source.d.ts → tracker/source.d.ts} +0 -0
- /package/dist/{tracked-source.js → tracker/source.js} +0 -0
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { type DurabilityLifecycleEvent, type LifecycleHook } from '
|
|
2
|
-
import { type DurabilityMigrationResult } from '
|
|
3
|
-
import { type DurabilityMethodOptions, 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 DurabilityMethodOptions, type ExecutionPolicyOptions } from '../policy.js';
|
|
4
|
+
import { type SchedulerAttachment } from '../scheduler/index.js';
|
|
5
|
+
import { type DurableMemoize } from '../memoize/index.js';
|
|
6
6
|
/**
|
|
7
7
|
* The context passed to a durable operation handler for each attempt.
|
|
8
8
|
*
|
|
@@ -149,6 +149,14 @@ export type DurabilityOptions<Handlers extends HandlerMap = HandlerMap> = Execut
|
|
|
149
149
|
export type DurabilityConfig<Handlers extends HandlerMap = HandlerMap> = DurabilityOptions<Handlers> & SchedulerAttachment & {
|
|
150
150
|
/** Operation handlers keyed by the method names they become. */
|
|
151
151
|
handlers: Handlers;
|
|
152
|
+
/**
|
|
153
|
+
* Stable channel ID. Instances on one scheduler share storage,
|
|
154
|
+
* partitioned by channel; call IDs are unique within a channel, and
|
|
155
|
+
* each instance runs, lists and purges only its own. A library should
|
|
156
|
+
* use its own channel so its calls never meet the application's.
|
|
157
|
+
* Defaults to `default`.
|
|
158
|
+
*/
|
|
159
|
+
channel?: string;
|
|
152
160
|
};
|
|
153
161
|
declare class DurabilityCore<Handlers extends HandlerMap> {
|
|
154
162
|
#private;
|
|
@@ -9,36 +9,39 @@ 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 _DurabilityCore_instances, _DurabilityCore_engine, _DurabilityCore_handlers, _DurabilityCore_policy, _DurabilityCore_lifecycle, _DurabilityCore_emit, _DurabilityCore_includeResults, _DurabilityCore_active, _DurabilityCore_executionStarts, _DurabilityCore_trackedSource, _DurabilityCore_runDue, _DurabilityCore_getResult, _DurabilityCore_run, _DurabilityCore_execute, _DurabilityCore_start;
|
|
13
|
-
import
|
|
14
|
-
import { traceFunction, traceMethods } from '
|
|
15
|
-
import { DuplicateDurableCallError, DuplicateDurablePayloadError, DurableAttemptsExhaustedError, DurableAttemptTimeoutError, DurableResultSerializationError, isErrorInstance, NonRetryableError, reportFailure, } from '
|
|
16
|
-
import { createLifecycleHooks, } from '
|
|
17
|
-
import { durabilityOperationMigrations
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
12
|
+
var _DurabilityCore_instances, _DurabilityCore_engine, _DurabilityCore_channel, _DurabilityCore_table, _DurabilityCore_handlers, _DurabilityCore_policy, _DurabilityCore_lifecycle, _DurabilityCore_emit, _DurabilityCore_includeResults, _DurabilityCore_active, _DurabilityCore_executionStarts, _DurabilityCore_trackedSource, _DurabilityCore_runDue, _DurabilityCore_getResult, _DurabilityCore_run, _DurabilityCore_execute, _DurabilityCore_start;
|
|
13
|
+
import * as v from 'valibot';
|
|
14
|
+
import { traceFunction, traceMethods } from '../dev.js';
|
|
15
|
+
import { DuplicateDurableCallError, DuplicateDurablePayloadError, DurableAttemptsExhaustedError, DurableAttemptTimeoutError, DurableResultSerializationError, isErrorInstance, NonRetryableError, reportFailure, } from '../errors.js';
|
|
16
|
+
import { createLifecycleHooks, } from '../lifecycle.js';
|
|
17
|
+
import { durabilityOperationMigrations } from './migrations.js';
|
|
18
|
+
import { durabilityMemoMigrations } from '../memoize/migrations.js';
|
|
19
|
+
import { migrateCapability, } from '../migrations.js';
|
|
20
|
+
import { createPolicyResolver, methodOptionsSchema, } from '../policy.js';
|
|
21
|
+
import { assertChannel, defaultChannel, engineFor, } from '../scheduler/index.js';
|
|
22
|
+
import { canonical, deserialize, serialize } from '../serialization.js';
|
|
23
|
+
import { createMemoize, deleteMemosOfDroppedTable, memoizedKeys, } from '../memoize/index.js';
|
|
24
|
+
import { listCreated, updateAttempt } from '../storage.js';
|
|
25
|
+
import { registerTrackedSource, } from '../tracker/source.js';
|
|
26
|
+
const callRowSchema = v.object({
|
|
27
|
+
id: v.string(),
|
|
28
|
+
channel: v.string(),
|
|
29
|
+
operation: v.string(),
|
|
30
|
+
payload: v.string(),
|
|
31
|
+
status: v.picklist(['pending', 'completed', 'failed']),
|
|
32
|
+
result: v.nullable(v.string()),
|
|
33
|
+
attempt: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
34
|
+
next_attempt_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
35
|
+
last_error: v.nullable(v.string()),
|
|
36
|
+
last_error_name: v.nullable(v.string()),
|
|
37
|
+
completed_at: v.nullable(v.pipe(v.number(), v.safeInteger(), v.minValue(0))),
|
|
38
|
+
created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
39
|
+
generation_id: v.string(),
|
|
37
40
|
});
|
|
38
41
|
const callsTable = { name: 'durability_calls', key: 'id' };
|
|
39
|
-
const readCall = (sql, id) => callRowSchema
|
|
40
|
-
.
|
|
41
|
-
.
|
|
42
|
+
const readCall = (sql, channel, id) => v.parse(v.optional(callRowSchema), sql
|
|
43
|
+
.exec('SELECT * FROM durability_calls WHERE channel = ? AND id = ?', channel, id)
|
|
44
|
+
.toArray()[0]);
|
|
42
45
|
const assertOperation = (id, existingOperation, requestedOperation) => {
|
|
43
46
|
if (existingOperation !== requestedOperation) {
|
|
44
47
|
throw new DuplicateDurableCallError(id, existingOperation, requestedOperation);
|
|
@@ -78,7 +81,7 @@ const callRecord = (sql, call) => {
|
|
|
78
81
|
state: state.status === 'pending'
|
|
79
82
|
? {
|
|
80
83
|
...state,
|
|
81
|
-
memoized: memoizedKeys(sql, callsTable, call.id, call.generation_id),
|
|
84
|
+
memoized: memoizedKeys(sql, { ...callsTable, channel: call.channel }, call.id, call.generation_id),
|
|
82
85
|
}
|
|
83
86
|
: state,
|
|
84
87
|
};
|
|
@@ -93,6 +96,8 @@ class DurabilityCore {
|
|
|
93
96
|
constructor(config) {
|
|
94
97
|
_DurabilityCore_instances.add(this);
|
|
95
98
|
_DurabilityCore_engine.set(this, void 0);
|
|
99
|
+
_DurabilityCore_channel.set(this, void 0);
|
|
100
|
+
_DurabilityCore_table.set(this, void 0);
|
|
96
101
|
_DurabilityCore_handlers.set(this, void 0);
|
|
97
102
|
_DurabilityCore_policy.set(this, void 0);
|
|
98
103
|
_DurabilityCore_lifecycle.set(this, void 0);
|
|
@@ -101,6 +106,8 @@ class DurabilityCore {
|
|
|
101
106
|
_DurabilityCore_active.set(this, new Map());
|
|
102
107
|
_DurabilityCore_executionStarts.set(this, new Map());
|
|
103
108
|
const { handlers } = config;
|
|
109
|
+
__classPrivateFieldSet(this, _DurabilityCore_channel, assertChannel('Durability', config.channel ?? defaultChannel), "f");
|
|
110
|
+
__classPrivateFieldSet(this, _DurabilityCore_table, { ...callsTable, channel: __classPrivateFieldGet(this, _DurabilityCore_channel, "f") }, "f");
|
|
104
111
|
__classPrivateFieldSet(this, _DurabilityCore_engine, engineFor(config), "f");
|
|
105
112
|
__classPrivateFieldSet(this, _DurabilityCore_handlers, handlers, "f");
|
|
106
113
|
__classPrivateFieldSet(this, _DurabilityCore_policy, createPolicyResolver(config, config.methods ?? {}, methodOptionsSchema), "f");
|
|
@@ -122,8 +129,10 @@ class DurabilityCore {
|
|
|
122
129
|
}
|
|
123
130
|
__classPrivateFieldGet(this, _DurabilityCore_engine, "f").migrateSchema('operations', durabilityOperationMigrations);
|
|
124
131
|
__classPrivateFieldGet(this, _DurabilityCore_engine, "f").migrateSchema('memos', durabilityMemoMigrations);
|
|
125
|
-
__classPrivateFieldGet(this, _DurabilityCore_engine, "f").
|
|
126
|
-
table:
|
|
132
|
+
__classPrivateFieldGet(this, _DurabilityCore_engine, "f").registerChannel({
|
|
133
|
+
table: __classPrivateFieldGet(this, _DurabilityCore_table, "f"),
|
|
134
|
+
channel: __classPrivateFieldGet(this, _DurabilityCore_channel, "f"),
|
|
135
|
+
helper: 'Durability',
|
|
127
136
|
runDue: (now) => __classPrivateFieldGet(this, _DurabilityCore_instances, "m", _DurabilityCore_runDue).call(this, now),
|
|
128
137
|
});
|
|
129
138
|
registerTrackedSource(this, __classPrivateFieldGet(this, _DurabilityCore_instances, "m", _DurabilityCore_trackedSource).call(this));
|
|
@@ -150,7 +159,7 @@ class DurabilityCore {
|
|
|
150
159
|
* timestamp, regardless of status, and returns how many were removed.
|
|
151
160
|
*/
|
|
152
161
|
async purgeBefore(before) {
|
|
153
|
-
const count = await __classPrivateFieldGet(this, _DurabilityCore_engine, "f").purge(
|
|
162
|
+
const count = await __classPrivateFieldGet(this, _DurabilityCore_engine, "f").purge(__classPrivateFieldGet(this, _DurabilityCore_table, "f"), before, __classPrivateFieldGet(this, _DurabilityCore_active, "f"), 'Durable operation was purged');
|
|
154
163
|
__classPrivateFieldGet(this, _DurabilityCore_emit, "f").call(this, {
|
|
155
164
|
type: 'purged',
|
|
156
165
|
entityKind: 'operation',
|
|
@@ -161,74 +170,71 @@ class DurabilityCore {
|
|
|
161
170
|
return count;
|
|
162
171
|
}
|
|
163
172
|
}
|
|
164
|
-
_DurabilityCore_engine = new WeakMap(), _DurabilityCore_handlers = new WeakMap(), _DurabilityCore_policy = new WeakMap(), _DurabilityCore_lifecycle = new WeakMap(), _DurabilityCore_emit = new WeakMap(), _DurabilityCore_includeResults = new WeakMap(), _DurabilityCore_active = new WeakMap(), _DurabilityCore_executionStarts = new WeakMap(), _DurabilityCore_instances = new WeakSet(), _DurabilityCore_trackedSource = function _DurabilityCore_trackedSource() {
|
|
173
|
+
_DurabilityCore_engine = new WeakMap(), _DurabilityCore_channel = new WeakMap(), _DurabilityCore_table = new WeakMap(), _DurabilityCore_handlers = new WeakMap(), _DurabilityCore_policy = new WeakMap(), _DurabilityCore_lifecycle = new WeakMap(), _DurabilityCore_emit = new WeakMap(), _DurabilityCore_includeResults = new WeakMap(), _DurabilityCore_active = new WeakMap(), _DurabilityCore_executionStarts = new WeakMap(), _DurabilityCore_instances = new WeakSet(), _DurabilityCore_trackedSource = function _DurabilityCore_trackedSource() {
|
|
165
174
|
const sql = __classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql;
|
|
166
175
|
return {
|
|
167
176
|
get: async (id) => {
|
|
168
|
-
const call = readCall(sql, id);
|
|
177
|
+
const call = readCall(sql, __classPrivateFieldGet(this, _DurabilityCore_channel, "f"), id);
|
|
169
178
|
return call && callRecord(sql, call);
|
|
170
179
|
},
|
|
171
180
|
list: async ({ limit, cursor, status, name }) => {
|
|
172
|
-
const page = listCreated(sql,
|
|
181
|
+
const page = listCreated(sql, __classPrivateFieldGet(this, _DurabilityCore_table, "f"), 'operation', {
|
|
173
182
|
limit,
|
|
174
183
|
after: cursor,
|
|
175
184
|
status,
|
|
176
185
|
name,
|
|
177
186
|
});
|
|
178
|
-
const records =
|
|
179
|
-
.array()
|
|
180
|
-
.parse(page.rows)
|
|
187
|
+
const records = v
|
|
188
|
+
.parse(v.array(callRowSchema), page.rows)
|
|
181
189
|
.map((call) => callRecord(sql, call));
|
|
182
190
|
return page.next === undefined
|
|
183
191
|
? { records }
|
|
184
192
|
: { records, cursor: page.next };
|
|
185
193
|
},
|
|
186
|
-
watch: (id, listener) => __classPrivateFieldGet(this, _DurabilityCore_engine, "f").watch(
|
|
194
|
+
watch: (id, listener) => __classPrivateFieldGet(this, _DurabilityCore_engine, "f").watch(__classPrivateFieldGet(this, _DurabilityCore_table, "f"), id, () => listener()),
|
|
187
195
|
};
|
|
188
196
|
}, _DurabilityCore_runDue = async function _DurabilityCore_runDue(now) {
|
|
189
|
-
const due =
|
|
190
|
-
.object({ id: z.string() })
|
|
191
|
-
.array()
|
|
192
|
-
.parse(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql
|
|
197
|
+
const due = v.parse(v.array(v.object({ id: v.string() })), __classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql
|
|
193
198
|
.exec(`SELECT id FROM durability_calls
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
199
|
+
WHERE channel = ? AND status = 'pending' AND next_attempt_at <= ?
|
|
200
|
+
ORDER BY next_attempt_at ASC
|
|
201
|
+
LIMIT 100`, __classPrivateFieldGet(this, _DurabilityCore_channel, "f"), now)
|
|
197
202
|
.toArray());
|
|
198
203
|
// With liveTimers an active operation reschedules itself when it settles, so
|
|
199
204
|
// skipping it keeps a long handler from blocking a concurrent alarm pass.
|
|
200
205
|
const pending = __classPrivateFieldGet(this, _DurabilityCore_engine, "f").liveTimers
|
|
201
|
-
? due.filter(({ id }) => !__classPrivateFieldGet(this, _DurabilityCore_active, "f").has(
|
|
206
|
+
? due.filter(({ id: key }) => !__classPrivateFieldGet(this, _DurabilityCore_active, "f").has(key))
|
|
202
207
|
: due;
|
|
203
|
-
await __classPrivateFieldGet(this, _DurabilityCore_engine, "f").runConcurrent(pending, async ({ id }) => {
|
|
208
|
+
await __classPrivateFieldGet(this, _DurabilityCore_engine, "f").runConcurrent(pending, async ({ id: key }) => {
|
|
204
209
|
try {
|
|
205
|
-
await __classPrivateFieldGet(this, _DurabilityCore_instances, "m", _DurabilityCore_execute).call(this,
|
|
210
|
+
await __classPrivateFieldGet(this, _DurabilityCore_instances, "m", _DurabilityCore_execute).call(this, key);
|
|
206
211
|
}
|
|
207
212
|
catch {
|
|
208
213
|
return;
|
|
209
214
|
}
|
|
210
215
|
});
|
|
211
216
|
}, _DurabilityCore_getResult = async function _DurabilityCore_getResult(operation, idempotencyKey) {
|
|
212
|
-
const call = readCall(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql, idempotencyKey);
|
|
217
|
+
const call = readCall(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql, __classPrivateFieldGet(this, _DurabilityCore_channel, "f"), idempotencyKey);
|
|
213
218
|
if (!call) {
|
|
214
219
|
return { status: 'not_found' };
|
|
215
220
|
}
|
|
216
221
|
assertOperation(idempotencyKey, call.operation, operation);
|
|
217
222
|
return callState(call);
|
|
218
223
|
}, _DurabilityCore_run = async function _DurabilityCore_run(input) {
|
|
224
|
+
const key = input.id;
|
|
219
225
|
const payload = serialize(input.payload);
|
|
220
226
|
const now = Date.now();
|
|
221
227
|
const generation = crypto.randomUUID();
|
|
222
228
|
const { inserted, winner } = await __classPrivateFieldGet(this, _DurabilityCore_engine, "f").transaction((sql) => {
|
|
223
229
|
const created = sql
|
|
224
230
|
.exec(`INSERT INTO durability_calls
|
|
225
|
-
(id, operation, payload, status, attempt,
|
|
226
|
-
created_at, generation_id)
|
|
227
|
-
VALUES (?, ?, ?, 'pending', 0, ?, ?, ?)
|
|
228
|
-
ON CONFLICT(id) DO NOTHING
|
|
229
|
-
RETURNING id`,
|
|
231
|
+
(id, channel, operation, payload, status, attempt,
|
|
232
|
+
next_attempt_at, created_at, generation_id)
|
|
233
|
+
VALUES (?, ?, ?, ?, 'pending', 0, ?, ?, ?)
|
|
234
|
+
ON CONFLICT(channel, id) DO NOTHING
|
|
235
|
+
RETURNING id`, key, __classPrivateFieldGet(this, _DurabilityCore_channel, "f"), input.operation, payload, now, now, generation)
|
|
230
236
|
.toArray().length > 0;
|
|
231
|
-
const current = readCall(sql,
|
|
237
|
+
const current = readCall(sql, __classPrivateFieldGet(this, _DurabilityCore_channel, "f"), key);
|
|
232
238
|
if (!current) {
|
|
233
239
|
throw new Error(`Durable call "${input.id}" was not persisted`);
|
|
234
240
|
}
|
|
@@ -255,31 +261,31 @@ _DurabilityCore_engine = new WeakMap(), _DurabilityCore_handlers = new WeakMap()
|
|
|
255
261
|
return;
|
|
256
262
|
}
|
|
257
263
|
const onError = (error) => reportFailure('durability.background_execution.failed', { operation: input.operation, id: input.id }, error);
|
|
258
|
-
const background = __classPrivateFieldGet(this, _DurabilityCore_instances, "m", _DurabilityCore_execute).call(this,
|
|
264
|
+
const background = __classPrivateFieldGet(this, _DurabilityCore_instances, "m", _DurabilityCore_execute).call(this, key, eagerPermit)
|
|
259
265
|
.then(() => __classPrivateFieldGet(this, _DurabilityCore_engine, "f").scheduleNextAlarm())
|
|
260
266
|
.catch(onError);
|
|
261
267
|
__classPrivateFieldGet(this, _DurabilityCore_engine, "f").waitUntil(background, onError);
|
|
262
268
|
}, _DurabilityCore_execute =
|
|
263
269
|
/**
|
|
264
|
-
* Looks up a call and starts it unless the same generation is
|
|
265
|
-
* running. Lookups for one
|
|
266
|
-
* cannot both miss the active map while awaiting storage.
|
|
270
|
+
* Looks up a call by row key and starts it unless the same generation is
|
|
271
|
+
* already running. Lookups for one key are serialized so two concurrent
|
|
272
|
+
* callers cannot both miss the active map while awaiting storage.
|
|
267
273
|
*/
|
|
268
|
-
async function _DurabilityCore_execute(
|
|
269
|
-
const previousStart = __classPrivateFieldGet(this, _DurabilityCore_executionStarts, "f").get(
|
|
274
|
+
async function _DurabilityCore_execute(key, eagerPermit) {
|
|
275
|
+
const previousStart = __classPrivateFieldGet(this, _DurabilityCore_executionStarts, "f").get(key) ?? Promise.resolve();
|
|
270
276
|
let releaseStart;
|
|
271
277
|
const currentStart = new Promise((resolve) => {
|
|
272
278
|
releaseStart = resolve;
|
|
273
279
|
});
|
|
274
|
-
__classPrivateFieldGet(this, _DurabilityCore_executionStarts, "f").set(
|
|
280
|
+
__classPrivateFieldGet(this, _DurabilityCore_executionStarts, "f").set(key, currentStart);
|
|
275
281
|
let permitTransferred = false;
|
|
276
282
|
try {
|
|
277
283
|
await previousStart;
|
|
278
|
-
const call = readCall(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql,
|
|
284
|
+
const call = readCall(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql, __classPrivateFieldGet(this, _DurabilityCore_channel, "f"), key);
|
|
279
285
|
if (!call || call.status !== 'pending') {
|
|
280
286
|
return;
|
|
281
287
|
}
|
|
282
|
-
const running = __classPrivateFieldGet(this, _DurabilityCore_active, "f").get(
|
|
288
|
+
const running = __classPrivateFieldGet(this, _DurabilityCore_active, "f").get(key);
|
|
283
289
|
if (running?.generation === call.generation_id) {
|
|
284
290
|
releaseStart();
|
|
285
291
|
await running.settled;
|
|
@@ -292,8 +298,8 @@ async function _DurabilityCore_execute(id, eagerPermit) {
|
|
|
292
298
|
}
|
|
293
299
|
finally {
|
|
294
300
|
releaseStart();
|
|
295
|
-
if (__classPrivateFieldGet(this, _DurabilityCore_executionStarts, "f").get(
|
|
296
|
-
__classPrivateFieldGet(this, _DurabilityCore_executionStarts, "f").delete(
|
|
301
|
+
if (__classPrivateFieldGet(this, _DurabilityCore_executionStarts, "f").get(key) === currentStart) {
|
|
302
|
+
__classPrivateFieldGet(this, _DurabilityCore_executionStarts, "f").delete(key);
|
|
297
303
|
}
|
|
298
304
|
if (!permitTransferred) {
|
|
299
305
|
eagerPermit?.();
|
|
@@ -303,7 +309,7 @@ async function _DurabilityCore_execute(id, eagerPermit) {
|
|
|
303
309
|
const policy = __classPrivateFieldGet(this, _DurabilityCore_policy, "f").call(this, call.operation);
|
|
304
310
|
const handler = __classPrivateFieldGet(this, _DurabilityCore_handlers, "f")[call.operation];
|
|
305
311
|
return __classPrivateFieldGet(this, _DurabilityCore_engine, "f").startExecution({
|
|
306
|
-
table:
|
|
312
|
+
table: __classPrivateFieldGet(this, _DurabilityCore_table, "f"),
|
|
307
313
|
key: call.id,
|
|
308
314
|
row: call,
|
|
309
315
|
label: `operation "${call.operation}"`,
|
|
@@ -324,7 +330,7 @@ async function _DurabilityCore_execute(id, eagerPermit) {
|
|
|
324
330
|
payload: deserialize(call.payload),
|
|
325
331
|
attempt,
|
|
326
332
|
signal,
|
|
327
|
-
memoize: createMemoize(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql,
|
|
333
|
+
memoize: createMemoize(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql, __classPrivateFieldGet(this, _DurabilityCore_table, "f"), {
|
|
328
334
|
key: call.id,
|
|
329
335
|
generation: call.generation_id,
|
|
330
336
|
attempt,
|
|
@@ -340,7 +346,7 @@ async function _DurabilityCore_execute(id, eagerPermit) {
|
|
|
340
346
|
throw new DurableResultSerializationError(call.operation, error);
|
|
341
347
|
}
|
|
342
348
|
const timestamp = Date.now();
|
|
343
|
-
const updated = updateAttempt(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql,
|
|
349
|
+
const updated = updateAttempt(__classPrivateFieldGet(this, _DurabilityCore_engine, "f").sql, __classPrivateFieldGet(this, _DurabilityCore_table, "f"), { key: call.id, generation: call.generation_id, attempt }, `status = 'completed', result = ?, last_error = NULL,
|
|
344
350
|
last_error_name = NULL, completed_at = ?`, serialized, timestamp);
|
|
345
351
|
return updated ? timestamp : undefined;
|
|
346
352
|
},
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ordered SQLite migrations for the `durability_calls` table owned by
|
|
3
|
+
* {@link Durability}. Applied automatically on construction and tracked in the
|
|
4
|
+
* namespaced `durability_migrations` table alongside application migrations.
|
|
5
|
+
*/
|
|
6
|
+
export declare const durabilityOperationMigrations: {
|
|
7
|
+
name: string;
|
|
8
|
+
up: string;
|
|
9
|
+
down: string;
|
|
10
|
+
}[];
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ordered SQLite migrations for the `durability_calls` table owned by
|
|
3
|
+
* {@link Durability}. Applied automatically on construction and tracked in the
|
|
4
|
+
* namespaced `durability_migrations` table alongside application migrations.
|
|
5
|
+
*/
|
|
6
|
+
export const durabilityOperationMigrations = [
|
|
7
|
+
{
|
|
8
|
+
name: 'durability_0001_create_calls',
|
|
9
|
+
up: `
|
|
10
|
+
CREATE TABLE IF NOT EXISTS durability_calls (
|
|
11
|
+
id TEXT PRIMARY KEY,
|
|
12
|
+
operation TEXT NOT NULL,
|
|
13
|
+
payload TEXT NOT NULL,
|
|
14
|
+
status TEXT NOT NULL CHECK (status IN ('pending', 'completed', 'failed')),
|
|
15
|
+
result TEXT,
|
|
16
|
+
attempt INTEGER NOT NULL DEFAULT 0,
|
|
17
|
+
next_attempt_at INTEGER NOT NULL,
|
|
18
|
+
last_error TEXT,
|
|
19
|
+
last_error_name TEXT,
|
|
20
|
+
completed_at INTEGER
|
|
21
|
+
);
|
|
22
|
+
`,
|
|
23
|
+
down: 'DROP TABLE IF EXISTS durability_calls;',
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: 'durability_0002_pending_index',
|
|
27
|
+
up: `
|
|
28
|
+
CREATE INDEX IF NOT EXISTS durability_calls_pending_idx
|
|
29
|
+
ON durability_calls (status, next_attempt_at);
|
|
30
|
+
`,
|
|
31
|
+
down: 'DROP INDEX IF EXISTS durability_calls_pending_idx;',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
name: 'durability_0004_calls_generation_and_created_at',
|
|
35
|
+
up: `
|
|
36
|
+
DROP INDEX IF EXISTS durability_calls_pending_idx;
|
|
37
|
+
ALTER TABLE durability_calls RENAME TO durability_calls_v3;
|
|
38
|
+
CREATE TABLE durability_calls (
|
|
39
|
+
id TEXT PRIMARY KEY,
|
|
40
|
+
operation TEXT NOT NULL,
|
|
41
|
+
payload TEXT NOT NULL,
|
|
42
|
+
status TEXT NOT NULL CHECK (status IN ('pending', 'completed', 'failed')),
|
|
43
|
+
result TEXT,
|
|
44
|
+
attempt INTEGER NOT NULL DEFAULT 0,
|
|
45
|
+
next_attempt_at INTEGER NOT NULL,
|
|
46
|
+
last_error TEXT,
|
|
47
|
+
last_error_name TEXT,
|
|
48
|
+
completed_at INTEGER,
|
|
49
|
+
generation_id TEXT NOT NULL DEFAULT 'legacy',
|
|
50
|
+
created_at INTEGER NOT NULL DEFAULT (CAST(unixepoch('subsec') * 1000 AS INTEGER))
|
|
51
|
+
);
|
|
52
|
+
INSERT INTO durability_calls
|
|
53
|
+
SELECT id, operation, payload, status, result, attempt, next_attempt_at,
|
|
54
|
+
last_error, last_error_name, completed_at, 'legacy:' || id,
|
|
55
|
+
CAST(unixepoch('subsec') * 1000 AS INTEGER)
|
|
56
|
+
FROM durability_calls_v3;
|
|
57
|
+
DROP TABLE durability_calls_v3;
|
|
58
|
+
CREATE INDEX durability_calls_pending_idx
|
|
59
|
+
ON durability_calls (status, next_attempt_at);
|
|
60
|
+
CREATE INDEX durability_calls_created_idx
|
|
61
|
+
ON durability_calls (created_at);
|
|
62
|
+
`,
|
|
63
|
+
down: `
|
|
64
|
+
DROP INDEX IF EXISTS durability_calls_pending_idx;
|
|
65
|
+
DROP INDEX IF EXISTS durability_calls_created_idx;
|
|
66
|
+
ALTER TABLE durability_calls RENAME TO durability_calls_v4;
|
|
67
|
+
CREATE TABLE durability_calls (
|
|
68
|
+
id TEXT PRIMARY KEY,
|
|
69
|
+
operation TEXT NOT NULL,
|
|
70
|
+
payload TEXT NOT NULL,
|
|
71
|
+
status TEXT NOT NULL CHECK (status IN ('pending', 'completed', 'failed')),
|
|
72
|
+
result TEXT,
|
|
73
|
+
attempt INTEGER NOT NULL DEFAULT 0,
|
|
74
|
+
next_attempt_at INTEGER NOT NULL,
|
|
75
|
+
last_error TEXT,
|
|
76
|
+
last_error_name TEXT,
|
|
77
|
+
completed_at INTEGER
|
|
78
|
+
);
|
|
79
|
+
INSERT INTO durability_calls
|
|
80
|
+
SELECT id, operation, payload, status, result, attempt, next_attempt_at,
|
|
81
|
+
last_error, last_error_name, completed_at
|
|
82
|
+
FROM durability_calls_v4;
|
|
83
|
+
DROP TABLE durability_calls_v4;
|
|
84
|
+
CREATE INDEX durability_calls_pending_idx
|
|
85
|
+
ON durability_calls (status, next_attempt_at);
|
|
86
|
+
`,
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: 'durability_0015_calls_channel',
|
|
90
|
+
up: `
|
|
91
|
+
ALTER TABLE durability_calls RENAME TO durability_calls_before_channels;
|
|
92
|
+
CREATE TABLE durability_calls (
|
|
93
|
+
id TEXT NOT NULL,
|
|
94
|
+
operation TEXT NOT NULL,
|
|
95
|
+
payload TEXT NOT NULL,
|
|
96
|
+
status TEXT NOT NULL CHECK (status IN ('pending', 'completed', 'failed')),
|
|
97
|
+
result TEXT,
|
|
98
|
+
attempt INTEGER NOT NULL DEFAULT 0,
|
|
99
|
+
next_attempt_at INTEGER NOT NULL,
|
|
100
|
+
last_error TEXT,
|
|
101
|
+
last_error_name TEXT,
|
|
102
|
+
completed_at INTEGER,
|
|
103
|
+
generation_id TEXT NOT NULL DEFAULT 'legacy',
|
|
104
|
+
created_at INTEGER NOT NULL DEFAULT (CAST(unixepoch('subsec') * 1000 AS INTEGER)),
|
|
105
|
+
channel TEXT NOT NULL DEFAULT 'default',
|
|
106
|
+
PRIMARY KEY (channel, id)
|
|
107
|
+
);
|
|
108
|
+
INSERT INTO durability_calls
|
|
109
|
+
SELECT *, 'default' FROM durability_calls_before_channels;
|
|
110
|
+
DROP TABLE durability_calls_before_channels;
|
|
111
|
+
CREATE INDEX durability_calls_pending_idx
|
|
112
|
+
ON durability_calls (status, next_attempt_at);
|
|
113
|
+
CREATE INDEX durability_calls_created_idx
|
|
114
|
+
ON durability_calls (created_at);
|
|
115
|
+
CREATE INDEX durability_calls_channel_pending_idx
|
|
116
|
+
ON durability_calls (channel, status, next_attempt_at);
|
|
117
|
+
`,
|
|
118
|
+
down: `
|
|
119
|
+
CREATE TABLE durability_calls_channel_guard (
|
|
120
|
+
channel TEXT CONSTRAINT cannot_downgrade_nondefault_channels CHECK (channel = 'default')
|
|
121
|
+
);
|
|
122
|
+
INSERT INTO durability_calls_channel_guard SELECT DISTINCT channel FROM durability_calls;
|
|
123
|
+
DROP TABLE durability_calls_channel_guard;
|
|
124
|
+
ALTER TABLE durability_calls RENAME TO durability_calls_with_channels;
|
|
125
|
+
CREATE TABLE durability_calls (
|
|
126
|
+
id TEXT PRIMARY KEY,
|
|
127
|
+
operation TEXT NOT NULL,
|
|
128
|
+
payload TEXT NOT NULL,
|
|
129
|
+
status TEXT NOT NULL CHECK (status IN ('pending', 'completed', 'failed')),
|
|
130
|
+
result TEXT,
|
|
131
|
+
attempt INTEGER NOT NULL DEFAULT 0,
|
|
132
|
+
next_attempt_at INTEGER NOT NULL,
|
|
133
|
+
last_error TEXT,
|
|
134
|
+
last_error_name TEXT,
|
|
135
|
+
completed_at INTEGER,
|
|
136
|
+
generation_id TEXT NOT NULL DEFAULT 'legacy',
|
|
137
|
+
created_at INTEGER NOT NULL DEFAULT (CAST(unixepoch('subsec') * 1000 AS INTEGER))
|
|
138
|
+
);
|
|
139
|
+
INSERT INTO durability_calls
|
|
140
|
+
SELECT id, operation, payload, status, result, attempt, next_attempt_at,
|
|
141
|
+
last_error, last_error_name, completed_at, generation_id, created_at
|
|
142
|
+
FROM durability_calls_with_channels;
|
|
143
|
+
DROP TABLE durability_calls_with_channels;
|
|
144
|
+
CREATE INDEX durability_calls_pending_idx
|
|
145
|
+
ON durability_calls (status, next_attempt_at);
|
|
146
|
+
CREATE INDEX durability_calls_created_idx
|
|
147
|
+
ON durability_calls (created_at);
|
|
148
|
+
`,
|
|
149
|
+
},
|
|
150
|
+
];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { DurabilityLifecycleEvent, LifecycleHook } from '
|
|
2
|
-
import { type DurabilityMigrationResult } from '
|
|
3
|
-
import { type DurabilityRetryOptions } from '
|
|
4
|
-
import { type SchedulerAttachment } from '
|
|
1
|
+
import type { DurabilityLifecycleEvent, LifecycleHook } from '../lifecycle.js';
|
|
2
|
+
import { type DurabilityMigrationResult } from '../migrations.js';
|
|
3
|
+
import { type DurabilityRetryOptions } from '../policy.js';
|
|
4
|
+
import { type SchedulerAttachment } from '../scheduler/index.js';
|
|
5
5
|
/** The newest staged version of one key, handed to `publish`. */
|
|
6
6
|
export type OutboxEntry<Value> = {
|
|
7
7
|
key: string;
|