durability 5.2.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 +9 -9
- package/dist/alarms/index.js +18 -21
- package/dist/fanout/index.js +39 -39
- package/dist/log/index.d.ts +12 -17
- package/dist/log/index.js +59 -53
- package/dist/memoize/index.js +9 -10
- package/dist/migrations.js +12 -16
- package/dist/operations/index.js +22 -28
- package/dist/outbox/index.js +19 -17
- package/dist/policy.d.ts +19 -19
- package/dist/policy.js +13 -12
- package/dist/schedule/index.js +34 -36
- package/dist/scheduler/index.js +13 -12
- package/dist/semaphore/client.js +6 -5
- package/dist/semaphore/protocol.d.ts +446 -234
- package/dist/semaphore/protocol.js +46 -61
- package/dist/semaphore/queue-store.d.ts +58 -52
- package/dist/semaphore/queue-store.js +55 -57
- package/dist/semaphore/server.js +5 -4
- package/dist/semaphore/store.d.ts +30 -25
- package/dist/semaphore/store.js +31 -34
- package/dist/serialization.d.ts +5 -0
- package/dist/serialization.js +41 -16
- package/dist/storage.js +9 -9
- package/dist/topology/index.js +16 -16
- package/dist/topology/snapshot.d.ts +20 -40
- package/dist/topology/snapshot.js +18 -28
- package/dist/tracker/index.js +7 -7
- package/package.json +4 -4
package/dist/migrations.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as v from 'valibot';
|
|
2
2
|
import { traceStorage } from './dev.js';
|
|
3
3
|
export const migrationTableName = 'durability_migrations';
|
|
4
4
|
/**
|
|
@@ -7,11 +7,11 @@ export const migrationTableName = 'durability_migrations';
|
|
|
7
7
|
* accidental deploy of an ancient version cannot silently unwind a schema.
|
|
8
8
|
*/
|
|
9
9
|
const maxFutureMigrations = 2;
|
|
10
|
-
const historyRowSchema =
|
|
11
|
-
id:
|
|
12
|
-
name:
|
|
13
|
-
capability:
|
|
14
|
-
down:
|
|
10
|
+
const historyRowSchema = v.object({
|
|
11
|
+
id: v.pipe(v.number(), v.safeInteger()),
|
|
12
|
+
name: v.string(),
|
|
13
|
+
capability: v.nullable(v.string()),
|
|
14
|
+
down: v.nullable(v.string()),
|
|
15
15
|
});
|
|
16
16
|
/**
|
|
17
17
|
* Versions before 3.0 tracked the v4 rewrite of both tables under one name.
|
|
@@ -61,7 +61,7 @@ const readHistory = (sql, scope, migrations) => {
|
|
|
61
61
|
sql.exec(`UPDATE ${scope.table} SET capability = ?
|
|
62
62
|
WHERE name = ? AND capability IS NULL`, scope.capability, name);
|
|
63
63
|
}
|
|
64
|
-
return
|
|
64
|
+
return v.parse(v.array(historyRowSchema), sql
|
|
65
65
|
.exec(`SELECT id, name, capability, down FROM ${scope.table}
|
|
66
66
|
WHERE capability = ? ORDER BY id`, scope.capability)
|
|
67
67
|
.toArray());
|
|
@@ -93,9 +93,9 @@ const revertFutureMigrations = (storage, scope, history, known) => {
|
|
|
93
93
|
});
|
|
94
94
|
return future.map(({ name }) => name);
|
|
95
95
|
};
|
|
96
|
-
const currentRowSchema =
|
|
97
|
-
name:
|
|
98
|
-
down:
|
|
96
|
+
const currentRowSchema = v.object({
|
|
97
|
+
name: v.string(),
|
|
98
|
+
down: v.nullable(v.string()),
|
|
99
99
|
});
|
|
100
100
|
/**
|
|
101
101
|
* Whether a capability's history already lists exactly these migrations with
|
|
@@ -106,9 +106,7 @@ const currentRowSchema = z.object({
|
|
|
106
106
|
export const isSchemaCurrent = (sql, capability, migrations) => {
|
|
107
107
|
let rows;
|
|
108
108
|
try {
|
|
109
|
-
rows = currentRowSchema
|
|
110
|
-
.array()
|
|
111
|
-
.parse(sql
|
|
109
|
+
rows = v.parse(v.array(currentRowSchema), sql
|
|
112
110
|
.exec(`SELECT name, down FROM ${migrationTableName} WHERE capability = ?`, capability)
|
|
113
111
|
.toArray());
|
|
114
112
|
}
|
|
@@ -165,9 +163,7 @@ const migrateScope = (storage, scope, migrations, target) => {
|
|
|
165
163
|
storage.sql.exec(`UPDATE ${scope.table} SET down = ? WHERE name = ?`, migration.down, migration.name);
|
|
166
164
|
}
|
|
167
165
|
});
|
|
168
|
-
const remaining =
|
|
169
|
-
.object({ count: z.number().int() })
|
|
170
|
-
.parse(storage.sql
|
|
166
|
+
const remaining = v.parse(v.object({ count: v.pipe(v.number(), v.safeInteger()) }), storage.sql
|
|
171
167
|
.exec(`SELECT COUNT(*) AS count FROM ${scope.table}`)
|
|
172
168
|
.toArray()[0]).count;
|
|
173
169
|
if (remaining === 0) {
|
package/dist/operations/index.js
CHANGED
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
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
|
|
13
|
+
import * as v from 'valibot';
|
|
14
14
|
import { traceFunction, traceMethods } from '../dev.js';
|
|
15
15
|
import { DuplicateDurableCallError, DuplicateDurablePayloadError, DurableAttemptsExhaustedError, DurableAttemptTimeoutError, DurableResultSerializationError, isErrorInstance, NonRetryableError, reportFailure, } from '../errors.js';
|
|
16
16
|
import { createLifecycleHooks, } from '../lifecycle.js';
|
|
@@ -23,25 +23,23 @@ import { canonical, deserialize, serialize } from '../serialization.js';
|
|
|
23
23
|
import { createMemoize, deleteMemosOfDroppedTable, memoizedKeys, } from '../memoize/index.js';
|
|
24
24
|
import { listCreated, updateAttempt } from '../storage.js';
|
|
25
25
|
import { registerTrackedSource, } from '../tracker/source.js';
|
|
26
|
-
const callRowSchema =
|
|
27
|
-
id:
|
|
28
|
-
channel:
|
|
29
|
-
operation:
|
|
30
|
-
payload:
|
|
31
|
-
status:
|
|
32
|
-
result:
|
|
33
|
-
attempt:
|
|
34
|
-
next_attempt_at:
|
|
35
|
-
last_error:
|
|
36
|
-
last_error_name:
|
|
37
|
-
completed_at:
|
|
38
|
-
created_at:
|
|
39
|
-
generation_id:
|
|
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(),
|
|
40
40
|
});
|
|
41
41
|
const callsTable = { name: 'durability_calls', key: 'id' };
|
|
42
|
-
const readCall = (sql, channel, id) => callRowSchema
|
|
43
|
-
.optional()
|
|
44
|
-
.parse(sql
|
|
42
|
+
const readCall = (sql, channel, id) => v.parse(v.optional(callRowSchema), sql
|
|
45
43
|
.exec('SELECT * FROM durability_calls WHERE channel = ? AND id = ?', channel, id)
|
|
46
44
|
.toArray()[0]);
|
|
47
45
|
const assertOperation = (id, existingOperation, requestedOperation) => {
|
|
@@ -186,9 +184,8 @@ _DurabilityCore_engine = new WeakMap(), _DurabilityCore_channel = new WeakMap(),
|
|
|
186
184
|
status,
|
|
187
185
|
name,
|
|
188
186
|
});
|
|
189
|
-
const records =
|
|
190
|
-
.array()
|
|
191
|
-
.parse(page.rows)
|
|
187
|
+
const records = v
|
|
188
|
+
.parse(v.array(callRowSchema), page.rows)
|
|
192
189
|
.map((call) => callRecord(sql, call));
|
|
193
190
|
return page.next === undefined
|
|
194
191
|
? { records }
|
|
@@ -197,14 +194,11 @@ _DurabilityCore_engine = new WeakMap(), _DurabilityCore_channel = new WeakMap(),
|
|
|
197
194
|
watch: (id, listener) => __classPrivateFieldGet(this, _DurabilityCore_engine, "f").watch(__classPrivateFieldGet(this, _DurabilityCore_table, "f"), id, () => listener()),
|
|
198
195
|
};
|
|
199
196
|
}, _DurabilityCore_runDue = async function _DurabilityCore_runDue(now) {
|
|
200
|
-
const due =
|
|
201
|
-
.object({ id: z.string() })
|
|
202
|
-
.array()
|
|
203
|
-
.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
|
|
204
198
|
.exec(`SELECT id FROM durability_calls
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
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)
|
|
208
202
|
.toArray());
|
|
209
203
|
// With liveTimers an active operation reschedules itself when it settles, so
|
|
210
204
|
// skipping it keeps a long handler from blocking a concurrent alarm pass.
|
package/dist/outbox/index.js
CHANGED
|
@@ -19,24 +19,26 @@ import { assertChannel, defaultChannel, engineFor, } from '../scheduler/index.js
|
|
|
19
19
|
import { deserialize, serialize } from '../serialization.js';
|
|
20
20
|
import { claimAttempt, deleteAttempt, updateAttempt, } from '../storage.js';
|
|
21
21
|
import { exponential, jitter } from '../utils.js';
|
|
22
|
-
import
|
|
22
|
+
import * as v from 'valibot';
|
|
23
23
|
const outboxTable = { name: 'durability_outbox', key: 'id' };
|
|
24
|
-
const outboxRowSchema =
|
|
25
|
-
id:
|
|
26
|
-
channel:
|
|
27
|
-
entry_key:
|
|
28
|
-
version:
|
|
29
|
-
payload:
|
|
30
|
-
status:
|
|
31
|
-
attempt:
|
|
32
|
-
next_attempt_at:
|
|
33
|
-
last_error:
|
|
34
|
-
last_error_name:
|
|
35
|
-
created_at:
|
|
36
|
-
generation_id:
|
|
24
|
+
const outboxRowSchema = v.object({
|
|
25
|
+
id: v.string(),
|
|
26
|
+
channel: v.string(),
|
|
27
|
+
entry_key: v.string(),
|
|
28
|
+
version: v.pipe(v.number(), v.safeInteger(), v.gtValue(0)),
|
|
29
|
+
payload: v.string(),
|
|
30
|
+
status: v.literal('pending'),
|
|
31
|
+
attempt: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
32
|
+
next_attempt_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
33
|
+
last_error: v.nullable(v.string()),
|
|
34
|
+
last_error_name: v.nullable(v.string()),
|
|
35
|
+
created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
36
|
+
generation_id: v.string(),
|
|
37
37
|
});
|
|
38
|
-
const versionRowSchema =
|
|
39
|
-
|
|
38
|
+
const versionRowSchema = v.object({
|
|
39
|
+
version: v.pipe(v.number(), v.safeInteger(), v.gtValue(0)),
|
|
40
|
+
});
|
|
41
|
+
const listDueForChannel = (sql, channel, now, limit, key) => v.parse(v.array(outboxRowSchema), sql
|
|
40
42
|
.exec(`SELECT * FROM durability_outbox
|
|
41
43
|
WHERE channel = ? AND status = 'pending' AND next_attempt_at <= ?
|
|
42
44
|
AND (? IS NULL OR entry_key = ?)
|
|
@@ -45,7 +47,7 @@ const listDueForChannel = (sql, channel, now, limit, key) => outboxRowSchema.arr
|
|
|
45
47
|
.toArray());
|
|
46
48
|
/** Allocates a version from a durable counter that survives row deletion. */
|
|
47
49
|
const nextVersion = (sql) => {
|
|
48
|
-
const { version } =
|
|
50
|
+
const { version } = v.parse(versionRowSchema, sql
|
|
49
51
|
.exec(`UPDATE durability_outbox_seq
|
|
50
52
|
SET next_version = next_version + 1
|
|
51
53
|
WHERE id = 1
|
package/dist/policy.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as v from 'valibot';
|
|
2
2
|
/**
|
|
3
3
|
* Retry policy shared by all methods or overridden for one method.
|
|
4
4
|
*
|
|
@@ -46,23 +46,23 @@ export type AttemptPolicy = {
|
|
|
46
46
|
delay: (attempt: number) => number;
|
|
47
47
|
maxAttempts: number;
|
|
48
48
|
};
|
|
49
|
-
export declare const methodOptionsSchema:
|
|
50
|
-
attemptTimeoutMs:
|
|
51
|
-
retries:
|
|
52
|
-
delay:
|
|
53
|
-
maxAttempts:
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
export declare const alarmMethodOptionsSchema:
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
},
|
|
64
|
-
export type MethodPolicy =
|
|
65
|
-
export type AlarmMethodPolicy =
|
|
49
|
+
export declare const methodOptionsSchema: v.ObjectSchema<{
|
|
50
|
+
readonly attemptTimeoutMs: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
|
|
51
|
+
readonly retries: v.OptionalSchema<v.ObjectSchema<{
|
|
52
|
+
readonly delay: v.OptionalSchema<v.CustomSchema<(attempt: number) => number, undefined>, undefined>;
|
|
53
|
+
readonly maxAttempts: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
|
|
54
|
+
}, undefined>, undefined>;
|
|
55
|
+
}, undefined>;
|
|
56
|
+
export declare const alarmMethodOptionsSchema: v.ObjectSchema<{
|
|
57
|
+
readonly retryTimeouts: v.OptionalSchema<v.BooleanSchema<undefined>, undefined>;
|
|
58
|
+
readonly attemptTimeoutMs: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
|
|
59
|
+
readonly retries: v.OptionalSchema<v.ObjectSchema<{
|
|
60
|
+
readonly delay: v.OptionalSchema<v.CustomSchema<(attempt: number) => number, undefined>, undefined>;
|
|
61
|
+
readonly maxAttempts: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
|
|
62
|
+
}, undefined>, undefined>;
|
|
63
|
+
}, undefined>;
|
|
64
|
+
export type MethodPolicy = v.InferOutput<typeof methodOptionsSchema>;
|
|
65
|
+
export type AlarmMethodPolicy = v.InferOutput<typeof alarmMethodOptionsSchema>;
|
|
66
66
|
export declare const assertPositiveInteger: (name: string, value: number) => void;
|
|
67
67
|
/**
|
|
68
68
|
* Validates global and per-method policy options once and resolves the
|
|
@@ -71,4 +71,4 @@ export declare const assertPositiveInteger: (name: string, value: number) => voi
|
|
|
71
71
|
export type PolicyResolver<Method> = (name: string) => AttemptPolicy & {
|
|
72
72
|
method: Method | undefined;
|
|
73
73
|
};
|
|
74
|
-
export declare const createPolicyResolver: <Method extends MethodPolicy>(options: ExecutionPolicyOptions, methods: Record<string, unknown>, methodSchema:
|
|
74
|
+
export declare const createPolicyResolver: <Method extends MethodPolicy>(options: ExecutionPolicyOptions, methods: Record<string, unknown>, methodSchema: v.GenericSchema<unknown, Method>) => PolicyResolver<Method>;
|
package/dist/policy.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as v from 'valibot';
|
|
2
2
|
import { exponential, jitter } from './utils.js';
|
|
3
|
-
const functionSchema = () =>
|
|
4
|
-
const retryOptionsSchema =
|
|
5
|
-
delay:
|
|
6
|
-
maxAttempts:
|
|
3
|
+
const functionSchema = () => v.custom((value) => typeof value === 'function');
|
|
4
|
+
const retryOptionsSchema = v.object({
|
|
5
|
+
delay: v.optional(functionSchema()),
|
|
6
|
+
maxAttempts: v.optional(v.number()),
|
|
7
7
|
});
|
|
8
|
-
export const methodOptionsSchema =
|
|
9
|
-
attemptTimeoutMs:
|
|
10
|
-
retries:
|
|
8
|
+
export const methodOptionsSchema = v.object({
|
|
9
|
+
attemptTimeoutMs: v.optional(v.number()),
|
|
10
|
+
retries: v.optional(retryOptionsSchema),
|
|
11
11
|
});
|
|
12
|
-
export const alarmMethodOptionsSchema =
|
|
13
|
-
|
|
12
|
+
export const alarmMethodOptionsSchema = v.object({
|
|
13
|
+
...methodOptionsSchema.entries,
|
|
14
|
+
retryTimeouts: v.optional(v.boolean()),
|
|
14
15
|
});
|
|
15
16
|
export const assertPositiveInteger = (name, value) => {
|
|
16
17
|
if (!Number.isInteger(value) || value < 1) {
|
|
@@ -27,13 +28,13 @@ const assertMethodOptions = (prefix, method) => {
|
|
|
27
28
|
};
|
|
28
29
|
export const createPolicyResolver = (options, methods, methodSchema) => {
|
|
29
30
|
const attemptTimeoutMs = options.attemptTimeoutMs ?? 5 * 60000;
|
|
30
|
-
const retries =
|
|
31
|
+
const retries = v.parse(retryOptionsSchema, options.retries ?? {});
|
|
31
32
|
const delay = retries.delay ?? ((attempt) => jitter(exponential(attempt)));
|
|
32
33
|
const maxAttempts = retries.maxAttempts ?? 5;
|
|
33
34
|
assertPositiveInteger('attemptTimeoutMs', attemptTimeoutMs);
|
|
34
35
|
assertPositiveInteger('retries.maxAttempts', maxAttempts);
|
|
35
36
|
const parsed = new Map(Object.entries(methods).map(([name, method]) => {
|
|
36
|
-
const valid =
|
|
37
|
+
const valid = v.parse(methodSchema, method);
|
|
37
38
|
assertMethodOptions(`methods.${name}`, valid);
|
|
38
39
|
return [name, valid];
|
|
39
40
|
}));
|
package/dist/schedule/index.js
CHANGED
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _DurabilitySchedule_instances, _DurabilitySchedule_engine, _DurabilitySchedule_channel, _DurabilitySchedule_nextOccurrence, _DurabilitySchedule_run, _DurabilitySchedule_onOutcome, _DurabilitySchedule_maxConcurrentRuns, _DurabilitySchedule_runTimeoutMs, _DurabilitySchedule_outcomeTimeoutMs, _DurabilitySchedule_maxOutcomeAttempts, _DurabilitySchedule_delay, _DurabilitySchedule_keyPrefix, _DurabilitySchedule_keySuffix, _DurabilitySchedule_emit, _DurabilitySchedule_active, _DurabilitySchedule_delivering, _DurabilitySchedule_loop, _DurabilitySchedule_woken, _DurabilitySchedule_wakeSleeper, _DurabilitySchedule_nextPendingAt, _DurabilitySchedule_nextAfter, _DurabilitySchedule_idempotencyKey, _DurabilitySchedule_describe, _DurabilitySchedule_entity, _DurabilitySchedule_hold, _DurabilitySchedule_drive, _DurabilitySchedule_pass, _DurabilitySchedule_wake, _DurabilitySchedule_sleep, _DurabilitySchedule_nextWakeAt, _DurabilitySchedule_interruptOrphans, _DurabilitySchedule_tick, _DurabilitySchedule_startQueued, _DurabilitySchedule_reserveSlots, _DurabilitySchedule_start, _DurabilitySchedule_record, _DurabilitySchedule_deliverDue, _DurabilitySchedule_deliver, _DurabilitySchedule_deliverOnce, _DurabilitySchedule_failDelivery, _DurabilitySchedule_retryDelay;
|
|
13
|
-
import
|
|
13
|
+
import * as v from 'valibot';
|
|
14
14
|
import { traceLifecycle, traceMethods } from '../dev.js';
|
|
15
15
|
import { reportFailure, ScheduledOutcomeTimeoutError, ScheduledRunInterruptedError, ScheduledRunTimeoutError, serializeError, } from '../errors.js';
|
|
16
16
|
import { durabilityScheduleMigrations } from './migrations.js';
|
|
@@ -25,37 +25,37 @@ const runsTable = {
|
|
|
25
25
|
// setTimeout overflows past 2^31 - 1 ms.
|
|
26
26
|
const maxDelayMs = 2 ** 31 - 1;
|
|
27
27
|
const fallbackRetryMs = 60000;
|
|
28
|
-
const scheduleRowSchema =
|
|
29
|
-
version:
|
|
30
|
-
expression:
|
|
31
|
-
next_at:
|
|
32
|
-
jitter_ms:
|
|
28
|
+
const scheduleRowSchema = v.object({
|
|
29
|
+
version: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
30
|
+
expression: v.nullable(v.string()),
|
|
31
|
+
next_at: v.nullable(v.pipe(v.number(), v.safeInteger(), v.minValue(0))),
|
|
32
|
+
jitter_ms: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
33
33
|
});
|
|
34
|
-
const runStateSchema =
|
|
35
|
-
const runRowSchema =
|
|
36
|
-
scheduled_time:
|
|
37
|
-
due_at:
|
|
34
|
+
const runStateSchema = v.picklist(['queued', 'running', 'complete', 'errored']);
|
|
35
|
+
const runRowSchema = v.object({
|
|
36
|
+
scheduled_time: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
37
|
+
due_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
38
38
|
state: runStateSchema,
|
|
39
|
-
next_at:
|
|
40
|
-
attempt:
|
|
41
|
-
error:
|
|
39
|
+
next_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
40
|
+
attempt: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
41
|
+
error: v.nullable(v.string()),
|
|
42
42
|
});
|
|
43
|
-
const timeRowSchema =
|
|
44
|
-
scheduled_time:
|
|
43
|
+
const timeRowSchema = v.object({
|
|
44
|
+
scheduled_time: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
45
45
|
});
|
|
46
|
-
const dueRowSchema =
|
|
47
|
-
scheduled_time:
|
|
48
|
-
due_at:
|
|
46
|
+
const dueRowSchema = v.object({
|
|
47
|
+
scheduled_time: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
48
|
+
due_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
49
49
|
});
|
|
50
|
-
const timedRowSchema =
|
|
51
|
-
scheduled_time:
|
|
52
|
-
next_at:
|
|
50
|
+
const timedRowSchema = v.object({
|
|
51
|
+
scheduled_time: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
52
|
+
next_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
53
53
|
});
|
|
54
|
-
const serializedErrorSchema =
|
|
55
|
-
name:
|
|
56
|
-
message:
|
|
54
|
+
const serializedErrorSchema = v.object({
|
|
55
|
+
name: v.string(),
|
|
56
|
+
message: v.string(),
|
|
57
57
|
});
|
|
58
|
-
const readSchedule = (sql, channel) =>
|
|
58
|
+
const readSchedule = (sql, channel) => v.parse(v.optional(scheduleRowSchema), sql
|
|
59
59
|
.exec(`SELECT
|
|
60
60
|
version,
|
|
61
61
|
expression,
|
|
@@ -83,14 +83,13 @@ const writeSchedule = (sql, channel, schedule) => {
|
|
|
83
83
|
const firesAt = (schedule) => schedule === undefined || schedule.next_at === null
|
|
84
84
|
? undefined
|
|
85
85
|
: schedule.next_at + schedule.jitter_ms;
|
|
86
|
-
const scheduledTimes = (cursor) =>
|
|
87
|
-
.array()
|
|
88
|
-
.parse(cursor.toArray())
|
|
86
|
+
const scheduledTimes = (cursor) => v
|
|
87
|
+
.parse(v.array(timeRowSchema), cursor.toArray())
|
|
89
88
|
.map((row) => row.scheduled_time);
|
|
90
89
|
const outcomeOf = (row) => row.state === 'errored'
|
|
91
90
|
? {
|
|
92
91
|
status: 'errored',
|
|
93
|
-
error:
|
|
92
|
+
error: v.parse(serializedErrorSchema, JSON.parse(row.error ?? '{}')),
|
|
94
93
|
}
|
|
95
94
|
: { status: 'complete' };
|
|
96
95
|
const assertNonNegative = (name, value) => {
|
|
@@ -291,7 +290,7 @@ export class DurabilitySchedule {
|
|
|
291
290
|
}
|
|
292
291
|
}
|
|
293
292
|
_DurabilitySchedule_engine = new WeakMap(), _DurabilitySchedule_channel = new WeakMap(), _DurabilitySchedule_nextOccurrence = new WeakMap(), _DurabilitySchedule_run = new WeakMap(), _DurabilitySchedule_onOutcome = new WeakMap(), _DurabilitySchedule_maxConcurrentRuns = new WeakMap(), _DurabilitySchedule_runTimeoutMs = new WeakMap(), _DurabilitySchedule_outcomeTimeoutMs = new WeakMap(), _DurabilitySchedule_maxOutcomeAttempts = new WeakMap(), _DurabilitySchedule_delay = new WeakMap(), _DurabilitySchedule_keyPrefix = new WeakMap(), _DurabilitySchedule_keySuffix = new WeakMap(), _DurabilitySchedule_emit = new WeakMap(), _DurabilitySchedule_active = new WeakMap(), _DurabilitySchedule_delivering = new WeakMap(), _DurabilitySchedule_loop = new WeakMap(), _DurabilitySchedule_woken = new WeakMap(), _DurabilitySchedule_wakeSleeper = new WeakMap(), _DurabilitySchedule_instances = new WeakSet(), _DurabilitySchedule_nextPendingAt = function _DurabilitySchedule_nextPendingAt(sql) {
|
|
294
|
-
const rows =
|
|
293
|
+
const rows = v.parse(v.array(timedRowSchema), sql
|
|
295
294
|
.exec(`SELECT scheduled_time, next_at FROM durability_schedule_runs
|
|
296
295
|
WHERE channel = ?`, __classPrivateFieldGet(this, _DurabilitySchedule_channel, "f"))
|
|
297
296
|
.toArray());
|
|
@@ -400,9 +399,8 @@ _DurabilitySchedule_engine = new WeakMap(), _DurabilitySchedule_channel = new We
|
|
|
400
399
|
}
|
|
401
400
|
}, _DurabilitySchedule_nextWakeAt = function _DurabilitySchedule_nextWakeAt() {
|
|
402
401
|
const sql = __classPrivateFieldGet(this, _DurabilitySchedule_engine, "f").sql;
|
|
403
|
-
const times =
|
|
404
|
-
.array()
|
|
405
|
-
.parse(sql
|
|
402
|
+
const times = v
|
|
403
|
+
.parse(v.array(runRowSchema), sql
|
|
406
404
|
.exec(`SELECT * FROM durability_schedule_runs
|
|
407
405
|
WHERE channel = ? AND state IN ('complete', 'errored')`, __classPrivateFieldGet(this, _DurabilitySchedule_channel, "f"))
|
|
408
406
|
.toArray())
|
|
@@ -463,7 +461,7 @@ async function _DurabilitySchedule_interruptOrphans() {
|
|
|
463
461
|
const nextAt = __classPrivateFieldGet(this, _DurabilitySchedule_instances, "m", _DurabilitySchedule_nextAfter).call(this, schedule.expression, Math.max(scheduledTime, now - schedule.jitter_ms));
|
|
464
462
|
sql.exec('UPDATE durability_schedule SET next_at = ? WHERE channel = ? AND id = 1', nextAt ?? null, __classPrivateFieldGet(this, _DurabilitySchedule_channel, "f"));
|
|
465
463
|
const queued = free <= 0;
|
|
466
|
-
const [row] =
|
|
464
|
+
const [row] = v.parse(v.array(dueRowSchema), sql
|
|
467
465
|
.exec(`INSERT INTO durability_schedule_runs
|
|
468
466
|
(channel, scheduled_time, due_at, state, next_at)
|
|
469
467
|
VALUES (?, ?, ?, ?, ?)
|
|
@@ -504,7 +502,7 @@ async function _DurabilitySchedule_interruptOrphans() {
|
|
|
504
502
|
const started = await __classPrivateFieldGet(this, _DurabilitySchedule_instances, "m", _DurabilitySchedule_reserveSlots).call(this, (sql, free) => {
|
|
505
503
|
const rows = free <= 0
|
|
506
504
|
? []
|
|
507
|
-
:
|
|
505
|
+
: v.parse(v.array(dueRowSchema), sql
|
|
508
506
|
.exec(`UPDATE durability_schedule_runs
|
|
509
507
|
SET state = 'running', next_at = ?
|
|
510
508
|
WHERE channel = ? AND scheduled_time IN (
|
|
@@ -593,7 +591,7 @@ async function _DurabilitySchedule_reserveSlots(mark) {
|
|
|
593
591
|
});
|
|
594
592
|
__classPrivateFieldGet(this, _DurabilitySchedule_instances, "m", _DurabilitySchedule_deliver).call(this, run, outcome, 1);
|
|
595
593
|
}, _DurabilitySchedule_deliverDue = function _DurabilitySchedule_deliverDue() {
|
|
596
|
-
const due =
|
|
594
|
+
const due = v.parse(v.array(runRowSchema), __classPrivateFieldGet(this, _DurabilitySchedule_engine, "f").sql
|
|
597
595
|
.exec(`SELECT * FROM durability_schedule_runs
|
|
598
596
|
WHERE channel = ? AND state IN ('complete', 'errored') AND next_at <= ?`, __classPrivateFieldGet(this, _DurabilitySchedule_channel, "f"), Date.now())
|
|
599
597
|
.toArray());
|
package/dist/scheduler/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as v from 'valibot';
|
|
2
2
|
import { trace, traceMethods, traceStorage } from '../dev.js';
|
|
3
3
|
import { DurableRetryPolicyError, isErrorInstance, isNonRetryable, reportFailure, serializeError, } from '../errors.js';
|
|
4
4
|
import { createEmitter, } from '../lifecycle.js';
|
|
@@ -20,17 +20,19 @@ const maxAlarmDelayMs = 15000;
|
|
|
20
20
|
// setTimeout treats delays past this as zero, so anything further out can only
|
|
21
21
|
// be served by the physical alarm fallback.
|
|
22
22
|
const maxTimerMs = 2 ** 31 - 1;
|
|
23
|
-
const pendingHeadRowSchema =
|
|
24
|
-
pk:
|
|
25
|
-
channel:
|
|
26
|
-
next_attempt_at:
|
|
23
|
+
const pendingHeadRowSchema = v.object({
|
|
24
|
+
pk: v.string(),
|
|
25
|
+
channel: v.optional(v.string()),
|
|
26
|
+
next_attempt_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
27
|
+
});
|
|
28
|
+
const countRowSchema = v.object({
|
|
29
|
+
count: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
27
30
|
});
|
|
28
|
-
const countRowSchema = z.object({ count: z.number().int().nonnegative() });
|
|
29
31
|
/** The channel every helper uses when it is not given one. */
|
|
30
32
|
export const defaultChannel = 'default';
|
|
31
33
|
/** How long records of a channel no instance attached wait before they are looked at again. */
|
|
32
34
|
const unattachedChannelRetryMs = 60000;
|
|
33
|
-
const channelRowSchema =
|
|
35
|
+
const channelRowSchema = v.object({ channel: v.string() });
|
|
34
36
|
const activeKey = (channel, key) => JSON.stringify([channel ?? null, key]);
|
|
35
37
|
/** Rejects a channel name a helper would otherwise store rows under. */
|
|
36
38
|
export const assertChannel = (helper, channel) => {
|
|
@@ -239,9 +241,8 @@ export class Engine {
|
|
|
239
241
|
.join(' UNION ')
|
|
240
242
|
: `SELECT DISTINCT channel FROM ${table.name}
|
|
241
243
|
WHERE status = 'pending' AND next_attempt_at <= ?`;
|
|
242
|
-
const unattached =
|
|
243
|
-
.array()
|
|
244
|
-
.parse(this.sql.exec(query, ...(customTiming ? [] : [now])).toArray())
|
|
244
|
+
const unattached = v
|
|
245
|
+
.parse(v.array(channelRowSchema), this.sql.exec(query, ...(customTiming ? [] : [now])).toArray())
|
|
245
246
|
.map((row) => row.channel)
|
|
246
247
|
.filter((channel) => !channels.has(channel));
|
|
247
248
|
if (unattached.length === 0) {
|
|
@@ -378,7 +379,7 @@ export class Engine {
|
|
|
378
379
|
const active = this.activeByTable.get(participant.table.name);
|
|
379
380
|
const partitioned = this.channelsByTable.has(participant.table.name);
|
|
380
381
|
const limit = (active?.size ?? 0) + 1;
|
|
381
|
-
const rows =
|
|
382
|
+
const rows = v.parse(v.array(pendingHeadRowSchema), this.sql
|
|
382
383
|
.exec(`SELECT ${participant.table.key} AS pk, next_attempt_at${partitioned ? ', channel' : ''}
|
|
383
384
|
FROM ${participant.table.name}
|
|
384
385
|
WHERE status = 'pending'
|
|
@@ -493,7 +494,7 @@ export class Engine {
|
|
|
493
494
|
const where = `created_at < ?${channelClause(table)}`;
|
|
494
495
|
const params = [before, ...channelBindings(table)];
|
|
495
496
|
const removed = await this.transaction((sql) => {
|
|
496
|
-
const { count } =
|
|
497
|
+
const { count } = v.parse(countRowSchema, sql
|
|
497
498
|
.exec(`SELECT COUNT(*) AS count FROM ${table.name} WHERE ${where}`, ...params)
|
|
498
499
|
.toArray()[0]);
|
|
499
500
|
sql.exec(`DELETE FROM ${table.name} WHERE ${where}`, ...params);
|
package/dist/semaphore/client.js
CHANGED
|
@@ -10,6 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _SemaphoreClient_instances, _SemaphoreClient_config, _SemaphoreClient_nextRequestAt, _SemaphoreClient_address;
|
|
13
|
+
import * as v from 'valibot';
|
|
13
14
|
import { SemaphoreProtocolError } from '../errors.js';
|
|
14
15
|
import { permitGrantSchema, permitSyncResultSchema, } from './protocol.js';
|
|
15
16
|
/**
|
|
@@ -29,11 +30,11 @@ export class SemaphoreClient {
|
|
|
29
30
|
}
|
|
30
31
|
async request(key, message) {
|
|
31
32
|
const response = await __classPrivateFieldGet(this, _SemaphoreClient_config, "f").invoke(__classPrivateFieldGet(this, _SemaphoreClient_instances, "m", _SemaphoreClient_address).call(this, key), message);
|
|
32
|
-
const parsed =
|
|
33
|
+
const parsed = v.safeParse(permitGrantSchema, response);
|
|
33
34
|
if (!parsed.success) {
|
|
34
35
|
throw new SemaphoreProtocolError(`Unexpected response to a permit request for "${key}"`);
|
|
35
36
|
}
|
|
36
|
-
__classPrivateFieldGet(this, _SemaphoreClient_nextRequestAt, "f").set(key, parsed.
|
|
37
|
+
__classPrivateFieldGet(this, _SemaphoreClient_nextRequestAt, "f").set(key, parsed.output.nextRequestAt);
|
|
37
38
|
// Advisory state must stay bounded for high-cardinality keys.
|
|
38
39
|
if (__classPrivateFieldGet(this, _SemaphoreClient_nextRequestAt, "f").size > 1024) {
|
|
39
40
|
const oldest = __classPrivateFieldGet(this, _SemaphoreClient_nextRequestAt, "f").keys().next().value;
|
|
@@ -41,15 +42,15 @@ export class SemaphoreClient {
|
|
|
41
42
|
__classPrivateFieldGet(this, _SemaphoreClient_nextRequestAt, "f").delete(oldest);
|
|
42
43
|
}
|
|
43
44
|
}
|
|
44
|
-
return parsed.
|
|
45
|
+
return parsed.output;
|
|
45
46
|
}
|
|
46
47
|
async sync(key, message) {
|
|
47
48
|
const response = await __classPrivateFieldGet(this, _SemaphoreClient_config, "f").invoke(__classPrivateFieldGet(this, _SemaphoreClient_instances, "m", _SemaphoreClient_address).call(this, key), message);
|
|
48
|
-
const parsed =
|
|
49
|
+
const parsed = v.safeParse(permitSyncResultSchema, response);
|
|
49
50
|
if (!parsed.success) {
|
|
50
51
|
throw new SemaphoreProtocolError(`Unexpected response to a permit sync for "${key}"`);
|
|
51
52
|
}
|
|
52
|
-
return parsed.
|
|
53
|
+
return parsed.output;
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
56
|
_SemaphoreClient_config = new WeakMap(), _SemaphoreClient_nextRequestAt = new WeakMap(), _SemaphoreClient_instances = new WeakSet(), _SemaphoreClient_address = function _SemaphoreClient_address(key) {
|