import * as v from 'valibot';
import { trace, traceMethods, traceStorage } from '../dev.js';
import {
DurableRetryPolicyError,
isErrorInstance,
isNonRetryable,
reportFailure,
serializeError,
type SerializedError,
} from '../errors.js';
import {
createEmitter,
type DurabilityLifecycleEvent,
type Emit,
type LifecycleEntity,
} from '../lifecycle.js';
import {
isSchemaCurrent,
migrateCapability,
type MigrationCapability,
} from '../migrations.js';
import { assertPositiveInteger, type AttemptPolicy } from '../policy.js';
import { isFunction, isNonEmptyString } from '../predicates.js';
import { deleteMemos, deleteStaleMemos } from '../memoize/index.js';
import { SharedTimer } from '../shared-timer.js';
import {
channelBindings,
channelClause,
claimAttempt,
exhaustAttempts,
tableOwner,
updateAttempt,
type DurableTable,
} from '../storage.js';
import type { DurableMigrations } from '@durability/storage';
/** The Durable Object state durability needs: storage plus optional `waitUntil`. */
export type DurabilityContext = Pick<DurableObjectState, 'storage'> &
Partial<Pick<DurableObjectState, 'waitUntil'>>;
/** Options for the scheduler shared by every durability helper in one Durable Object. */
export type DurabilitySchedulerOptions = {
/** Maximum handlers running at once across eager operations and alarm work. Defaults to 10. */
alarmConcurrency?: number;
/** Physical-alarm delay from 1 to 15 seconds. Defaults to 15 seconds. */
alarmMinDelayMs?: number;
/** Time before a running alarm hands unfinished work to a new alarm. Defaults to 14 minutes. */
alarmHandoffMs?: number;
/**
* Experimental. Start due work from in-memory timers at its exact scheduled
* time, independent of `alarmMinDelayMs`, and let newly due work run without
* waiting behind long-running handlers. The physical alarm stays armed as the
* eviction fallback. Off by default.
*/
liveTimers?: boolean;
/**
* Called each time an alarm run fails and the scheduler re-arms its own
* fallback alarm instead of letting the platform retry, for example to send
* the error to an external service. Best effort: it runs after the fallback is
* armed, a returned promise is passed to `waitUntil`, and a throw or rejection
* is only logged, so it never changes scheduling.
*/
onAlarmRetry?: (info: AlarmRetryInfo) => void | Promise<void>;
};
/** Details of a failed alarm run passed to `onAlarmRetry`. */
export type AlarmRetryInfo = {
error: SerializedError;
/** Delay until the re-armed fallback alarm fires. */
retryInMs: number;
/** The platform's retry count for this alarm invocation, when it provided one. */
retryCount?: number;
/** Whether the platform marked this invocation as a retry, when it provided one. */
isRetry?: boolean;
/** The owning Durable Object's ID, when the context carries one. */
objectId?: string;
};
/** Configuration for a scheduler shared by several helpers. */
export type DurabilitySchedulerConfig = DurabilitySchedulerOptions & {
context: DurabilityContext;
};
/**
* How a helper attaches to the object's scheduler: through a
* {@link DurabilityScheduler}, or from the Durable Object context, which joins
* the scheduler already there or creates it with the given options.
*/
export type SchedulerAttachment =
| { scheduler: DurabilityScheduler; context?: never }
| (DurabilitySchedulerConfig & { scheduler?: never });
const minAlarmDelayMs = 1_000;
const failedRunRetryMs = 1_000;
const defaultAlarmConcurrency = 10;
const schedulerOptionKeys = [
'alarmConcurrency',
'alarmMinDelayMs',
'alarmHandoffMs',
'liveTimers',
'onAlarmRetry',
] as const satisfies ReadonlyArray<keyof DurabilitySchedulerOptions>;
const maxAlarmDelayMs = 15_000;
const pendingHeadRowSchema = v.object({
pk: v.string(),
channel: v.optional(v.string()),
next_attempt_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
});
const countRowSchema = v.object({
count: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
});
type PhysicalAlarm = Pick<
DurableObjectTransaction,
'getAlarm' | 'setAlarm' | 'deleteAlarm'
>;
/** One record table attached to the scheduler: how to find and run its due work. */
export type Participant = {
table: DurableTable;
runDue(now: number, info: AlarmInvocationInfo | undefined): Promise<void>;
/** Earliest due time, for tables without `status` and `next_attempt_at` columns. */
nextPendingAt?(sql: SqlStorage): number | undefined;
/** Earliest work not already executing; active work still keeps its physical fallback. */
nextTimerAt?(sql: SqlStorage): number | undefined;
/**
* True while the participant runs its own due work from an alarm invocation
* it keeps open. The in-memory timer then ignores its pending time, which only
* arms the physical alarm as the eviction fallback.
*/
holding?(): boolean;
};
/** The channel every helper uses when it is not given one. */
export const defaultChannel = 'default';
/**
* One helper instance's share of a table that several instances partition by
* a `channel` column. Each runs only its own channel's due rows.
*/
export type ChannelParticipant = Participant & {
channel: string;
/** How errors name the helper, such as `An outbox`. */
helper: string;
/** Other tables carrying this capability's channel identity, such as its configuration. */
channelTables?: readonly string[];
};
/** How long records of a channel no instance attached wait before they are looked at again. */
const unattachedChannelRetryMs = 60_000;
const channelRowSchema = v.object({ channel: v.string() });
const activeKey = (channel: string | undefined, key: string): string =>
JSON.stringify([channel ?? null, key]);
/** Rejects a channel name a helper would otherwise store rows under. */
export const assertChannel = (helper: string, channel: string): string => {
if (!isNonEmptyString(channel)) {
throw new TypeError(`${helper} channel must be a non-empty string`);
}
return channel;
};
/**
* Called after a record may have settled. Successful completions pass the
* executed row because some tables delete it on success; other changes pass
* nothing and listeners re-read storage.
*/
export type SettlementListener = (completed?: ExecutionRow) => void;
export type ActiveExecution = {
generation: string;
createdAt: number;
settled: Promise<void>;
controller?: AbortController;
};
type ExecutionRow = { generation_id: string; created_at: number };
/** Everything that differs between running a durable operation and a named alarm attempt. */
export type ExecutionSpec<Result> = {
table: DurableTable;
key: string;
row: ExecutionRow;
label: string;
entity: LifecycleEntity;
active: Map<string, ActiveExecution>;
policy: AttemptPolicy;
emit: Emit;
/** When true, the completed event carries the handler result; omitted by default to keep results out of telemetry. */
includeResult?: boolean;
exhaustedError: () => Error;
timeoutError: () => Error;
invoke: (attempt: number, signal: AbortSignal) => Result | Promise<Result>;
/**
* Persists a successful attempt and returns its settlement timestamp, or
* undefined when a newer generation won. May throw to turn the success into
* a failure.
*/
complete: (result: Result, attempt: number) => number | undefined;
/** Kind-specific terminal rule beyond non-retryable errors and exhausted attempts. */
isTerminal: (cause: unknown) => boolean;
};
type PendingTimes = {
physical?: number | undefined;
timer?: number | undefined;
};
type RetryDecision = {
error: unknown;
terminal: boolean;
timestamp: number;
nextAttemptAt: number;
};
const decideRetry = <Result>(
spec: ExecutionSpec<Result>,
attempt: number,
cause: unknown
): RetryDecision => {
const terminal =
isNonRetryable(cause) ||
attempt >= spec.policy.maxAttempts ||
spec.isTerminal(cause);
if (terminal) {
const timestamp = Date.now();
return { error: cause, terminal, timestamp, nextAttemptAt: timestamp };
}
try {
const rawDelay = spec.policy.delay(attempt);
if (!Number.isFinite(rawDelay) || rawDelay < 0) {
throw new DurableRetryPolicyError(spec.label);
}
const delay = Math.round(rawDelay);
const timestamp = Date.now();
if (
!Number.isSafeInteger(delay) ||
!Number.isSafeInteger(timestamp + delay)
) {
throw new DurableRetryPolicyError(spec.label);
}
return {
error: cause,
terminal: false,
timestamp,
nextAttemptAt: timestamp + delay,
};
} catch (policyError) {
const timestamp = Date.now();
return {
error: isErrorInstance(policyError, DurableRetryPolicyError)
? policyError
: new DurableRetryPolicyError(spec.label, policyError),
terminal: true,
timestamp,
nextAttemptAt: timestamp,
};
}
};
/**
* Shared machinery behind {@link DurabilityScheduler}: transactions, the physical
* alarm, the concurrency pool, and the attempt lifecycle. Package-internal.
*/
export class Engine {
readonly sql: SqlStorage;
/** Holds the timeouts of every helper on the object behind one native timer. */
readonly timer = new SharedTimer();
private context: DurabilityContext;
private readonly storage: DurableObjectStorage;
private alarmConcurrency = defaultAlarmConcurrency;
private alarmMinDelayMs = 15_000;
private alarmHandoffMs = 14 * 60_000;
liveTimers = false;
private onAlarmRetry: DurabilitySchedulerOptions['onAlarmRetry'];
// Only options someone set can conflict; an omitted one takes any value.
private explicitOptions: DurabilitySchedulerOptions = {};
private readonly participants: Participant[] = [];
private readonly channelsByTable = new Map<
string,
Map<string, ChannelParticipant>
>();
private readonly claimedChannels = new Map<string, Set<string>>();
private readonly reportedMissingChannels = new Set<string>();
private readonly migrated = new Set<MigrationCapability>();
private availablePermits = defaultAlarmConcurrency;
private readonly permitWaiters: Array<(release: () => void) => void> = [];
private alarmRefresh: Promise<void> | undefined;
private alarmRefreshRequested = false;
private activeAlarms = 0;
private failedRuns = 0;
private earlyAlarm: { timestamp: number; cancel: () => void } | undefined;
private readonly watchers = new Map<
string,
Map<string, Set<SettlementListener>>
>();
// Keys whose attempt is currently executing, per table. The in-memory timer
// skips these so a still-due active row cannot drive a zero-delay timer loop.
private readonly activeByTable = new Map<string, Set<string>>();
constructor(context: DurabilityContext, options: DurabilitySchedulerOptions) {
this.context = context;
this.storage = traceStorage(context.storage);
this.sql = this.storage.sql;
this.configure(options);
}
/**
* Joins one more helper or scheduler handle to this object's scheduler. The
* first context that can `waitUntil` is kept, so a bare `{ storage }` context
* attached first does not hide it from the rest.
*/
attach(
context: DurabilityContext,
options: DurabilitySchedulerOptions
): void {
if (
this.context.waitUntil === undefined &&
context.waitUntil !== undefined
) {
this.context = context;
}
this.configure(options);
}
private configure(options: DurabilitySchedulerOptions): void {
const merged: DurabilitySchedulerOptions = { ...this.explicitOptions };
for (const key of schedulerOptionKeys) {
const value = options[key];
if (value === undefined) {
continue;
}
const current = this.explicitOptions[key];
if (current !== undefined && current !== value) {
throw new Error(
`Scheduler option "${key}" is already set to a different value for this Durable Object`
);
}
Object.assign(merged, { [key]: value });
}
const alarmConcurrency = merged.alarmConcurrency ?? defaultAlarmConcurrency;
const alarmMinDelayMs = merged.alarmMinDelayMs ?? 15_000;
const alarmHandoffMs = merged.alarmHandoffMs ?? 14 * 60_000;
if (merged.onAlarmRetry !== undefined && !isFunction(merged.onAlarmRetry)) {
throw new TypeError('onAlarmRetry must be a function');
}
assertPositiveInteger('alarmConcurrency', alarmConcurrency);
assertPositiveInteger('alarmMinDelayMs', alarmMinDelayMs);
if (
alarmMinDelayMs < minAlarmDelayMs ||
alarmMinDelayMs > maxAlarmDelayMs
) {
throw new RangeError(
`alarmMinDelayMs must be between ${minAlarmDelayMs} and ${maxAlarmDelayMs}`
);
}
assertPositiveInteger('alarmHandoffMs', alarmHandoffMs);
// Permits already handed out stay out; raising the limit frees more now.
this.availablePermits += alarmConcurrency - this.alarmConcurrency;
this.alarmConcurrency = alarmConcurrency;
this.alarmMinDelayMs = alarmMinDelayMs;
this.alarmHandoffMs = alarmHandoffMs;
this.liveTimers = merged.liveTimers ?? false;
this.onAlarmRetry = merged.onAlarmRetry;
this.explicitOptions = merged;
while (this.availablePermits > 0 && this.permitWaiters.length > 0) {
const waiter = this.permitWaiters.shift()!;
this.availablePermits -= 1;
waiter(this.createRelease());
}
}
register(participant: Participant): void {
const { name } = participant.table;
if (this.participants.some(({ table }) => table.name === name)) {
throw new Error(`A ${name} helper is already attached to this scheduler`);
}
this.participants.push(participant);
}
/** Reserves an instance identity, including for helpers that do not schedule work. */
claimChannel(kind: string, channel: string, helper: string): void {
const claimed = this.claimedChannels.get(kind) ?? new Set<string>();
if (claimed.has(channel)) {
throw new Error(
`${helper} for channel "${channel}" is already attached to this scheduler`
);
}
claimed.add(channel);
this.claimedChannels.set(kind, claimed);
}
/**
* Attaches one channel of a table several helper instances share. The first
* attachment registers the table; each run then runs every attached
* channel's due rows and retains the rows of channels nothing attached.
*/
registerChannel(participant: ChannelParticipant): void {
const { table, channel, helper } = participant;
let channels = this.channelsByTable.get(table.name);
if (!channels) {
const created = new Map<string, ChannelParticipant>();
this.register({
table: { name: table.name, key: table.key },
runDue: async (now, info) => {
await this.postponeUnattachedChannels(table, created);
await Promise.all(
[...created.values()].map((attached) => attached.runDue(now, info))
);
},
});
this.channelsByTable.set(table.name, created);
channels = created;
}
this.claimChannel(table.name, channel, helper);
channels.set(channel, {
...participant,
table: { ...table, channel },
});
if (participant.nextPendingAt !== undefined) {
const onError = (cause: unknown) =>
reportFailure(
'durability.channel_resume_failed',
{ table: table.name, channel },
cause
);
const resumed = Promise.resolve()
.then(async () => {
if (this.activeAlarms > 0) {
return undefined;
}
const nextAt = participant.nextPendingAt?.(this.sql);
if (nextAt === undefined) {
return undefined;
}
const currentAlarm = await this.storage.getAlarm();
if (this.activeAlarms > 0) {
return undefined;
}
const now = Date.now();
if (
!this.liveTimers &&
nextAt >= now + this.alarmMinDelayMs &&
currentAlarm === nextAt
) {
return undefined;
}
return this.scheduleNextAlarm();
})
.catch(async (cause: unknown) => {
if (await this.armFallbackAlarm()) {
this.retryFailedRun();
}
onError(cause);
});
this.waitUntil(resumed, onError);
}
}
/**
* Due rows of a channel no instance attached, such as one a deploy removed
* or a library not yet constructed, would keep the alarm awake forever. They
* are kept and looked at again later, since their owner may return.
*/
private async postponeUnattachedChannels(
table: DurableTable,
channels: ReadonlyMap<string, ChannelParticipant>
): Promise<void> {
const now = Date.now();
const customTiming = [...channels.values()].some(
(participant) => participant.nextPendingAt !== undefined
);
const channelTables = new Set([
table.name,
...[...channels.values()].flatMap(
(participant) => participant.channelTables ?? []
),
]);
const query = customTiming
? [...channelTables]
.map((name) => `SELECT channel FROM ${name}`)
.join(' UNION ')
: `SELECT DISTINCT channel FROM ${table.name}
WHERE status = 'pending' AND next_attempt_at <= ?`;
const unattached = v
.parse(
v.array(channelRowSchema),
this.sql.exec(query, ...(customTiming ? [] : [now])).toArray()
)
.map((row) => row.channel)
.filter((channel) => !channels.has(channel));
if (unattached.length === 0) {
return;
}
for (const channel of unattached) {
const identity = JSON.stringify([table.name, channel]);
if (customTiming && this.reportedMissingChannels.has(identity)) {
continue;
}
this.reportedMissingChannels.add(identity);
reportFailure(
'durability.channel_missing',
{ table: table.name, channel },
new Error(
`Channel "${channel}" of ${table.name} has no helper attached; retaining its records`
)
);
}
// Custom timing may represent leases or recurring configurations, not pending
// attempts. Retain those records unchanged until their channel returns.
if (customTiming) {
return;
}
await this.transaction((sql) => {
for (const channel of unattached) {
sql.exec(
`UPDATE ${table.name} SET next_attempt_at = ?
WHERE channel = ? AND status = 'pending' AND next_attempt_at <= ?`,
now + unattachedChannelRetryMs,
channel,
now
);
}
});
}
waitUntil(
promise: Promise<unknown>,
onError: (cause: unknown) => void
): void {
if (!this.context.waitUntil) {
return;
}
try {
this.context.waitUntil(promise);
} catch (error) {
onError(error);
}
}
/**
* The owning Durable Object's ID. The public context type omits it, but the
* `DurableObjectState` applications pass at runtime carries it.
*/
objectId(): string | undefined {
const id = 'id' in this.context ? this.context.id : undefined;
return id === undefined || id === null ? undefined : String(id);
}
emitter<
Event extends { entityKind: string; type: string; id?: string } =
DurabilityLifecycleEvent,
>(
hook: ((event: Event) => void | Promise<void>) | undefined
): (event: Event) => void {
return createEmitter<Event>(hook, (promise) =>
this.context.waitUntil?.(promise)
);
}
/** Applies a helper's SQLite migrations. */
migrateSchema(
capability: MigrationCapability,
migrations: DurableMigrations
): void {
// Helpers of one kind on one scheduler share their tables; checking the
// schema again for each would only add statements to every construction.
if (this.migrated.has(capability)) {
return;
}
if (!isSchemaCurrent(this.sql, capability, migrations)) {
migrateCapability(this.storage, capability, migrations, undefined);
}
this.migrated.add(capability);
}
/** Runs synchronous writes atomically without touching the physical alarm. */
transactionSync<T>(write: (sql: SqlStorage) => T): T {
return this.storage.transactionSync(() => write(this.sql));
}
/**
* Runs the writes and reconciles the physical alarm in one storage
* transaction, then publishes the in-memory timer only after commit.
*/
async transaction<T>(write: (sql: SqlStorage) => T): Promise<T> {
const result = await this.storage.transaction(async (transaction) => {
const value = write(this.sql);
await this.reconcile(transaction);
return value;
});
this.refreshEarlyAlarm();
return result;
}
private trackActive(table: string, key: string): void {
let keys = this.activeByTable.get(table);
if (!keys) {
keys = new Set();
this.activeByTable.set(table, keys);
}
keys.add(key);
}
private releaseActive(table: string, key: string): void {
const keys = this.activeByTable.get(table);
if (!keys) {
return;
}
keys.delete(key);
if (keys.size === 0) {
this.activeByTable.delete(table);
}
}
/**
* The earliest pending time for one participant: `physical` arms the physical
* alarm (eviction fallback) and includes active work; `timer` drives the
* in-memory timer and skips work that is already executing
* so a still-due active row cannot re-fire a zero-delay timer in a loop.
*/
private pendingTimes(participant: Participant): PendingTimes {
const channels = this.channelsByTable.get(participant.table.name);
if (
participant.table.channel === undefined &&
channels !== undefined &&
[...channels.values()].some(
(channel) => channel.nextPendingAt !== undefined
)
) {
let physical: number | undefined;
let timer: number | undefined;
for (const channel of channels.values()) {
const next = this.pendingTimes(channel);
if (next.physical !== undefined) {
physical = Math.min(physical ?? Infinity, next.physical);
}
if (next.timer !== undefined) {
timer = Math.min(timer ?? Infinity, next.timer);
}
}
return { physical, timer };
}
if (participant.nextPendingAt) {
const at = participant.nextPendingAt(this.sql);
if (at === undefined) {
return {};
}
return participant.holding?.()
? { physical: at }
: {
physical: at,
timer: participant.nextTimerAt
? participant.nextTimerAt(this.sql)
: at,
};
}
const active = this.activeByTable.get(participant.table.name);
const partitioned = this.channelsByTable.has(participant.table.name);
const limit = (active?.size ?? 0) + 1;
const rows = v.parse(
v.array(pendingHeadRowSchema),
this.sql
.exec(
`SELECT ${participant.table.key} AS pk, next_attempt_at${partitioned ? ', channel' : ''}
FROM ${participant.table.name}
WHERE status = 'pending'
ORDER BY next_attempt_at ASC
LIMIT ?`,
limit
)
.toArray()
);
const physical = rows[0]?.next_attempt_at;
const eligible = rows.find(
(row) => !active?.has(activeKey(row.channel, row.pk))
);
return { physical, timer: eligible?.next_attempt_at };
}
/**
* Arms the physical alarm from the earliest pending time (the eviction
* fallback) and returns the earliest time the in-memory timer should fire.
*/
private async reconcile(physicalAlarm: PhysicalAlarm): Promise<void> {
const physicalTimes: number[] = [];
for (const participant of this.participants) {
const { physical } = this.pendingTimes(participant);
if (physical !== undefined) {
physicalTimes.push(physical);
}
}
const physicalNextAt =
physicalTimes.length === 0 ? undefined : Math.min(...physicalTimes);
const currentAlarm = await physicalAlarm.getAlarm();
if (physicalNextAt === undefined) {
if (currentAlarm !== null) {
await physicalAlarm.deleteAlarm();
}
return;
}
const target = Math.max(physicalNextAt, Date.now() + this.alarmMinDelayMs);
// Preserve a fallback inside this range so frequent reconciliation cannot push it later.
if (
currentAlarm === null ||
currentAlarm < physicalNextAt ||
currentAlarm > target
) {
await physicalAlarm.setAlarm(target);
}
}
private scheduleEarlyAlarm(timestamp: number | undefined): void {
if (this.earlyAlarm?.timestamp === timestamp) {
return;
}
if (this.earlyAlarm) {
this.earlyAlarm.cancel();
this.earlyAlarm = undefined;
}
if (timestamp === undefined) {
return;
}
const delay = timestamp - Date.now();
// Without liveTimers the in-memory timer only covers the sub-min-delay
// window; the physical alarm handles everything beyond it. With liveTimers
// it fires at the exact due time no matter how far out.
if (!this.liveTimers && delay >= this.alarmMinDelayMs) {
return;
}
const cancel = this.timer.schedule(() => {
if (this.earlyAlarm?.cancel !== cancel) {
return;
}
this.earlyAlarm = undefined;
trace('scheduler.early_alarm', { scheduledTime: timestamp });
const onError = (cause: unknown) =>
reportFailure(
'durability.background_execution.failed',
{ scheduledTime: timestamp },
cause
);
const execution = this.alarm().catch(onError);
this.waitUntil(execution, onError);
}, delay);
this.earlyAlarm = { timestamp, cancel };
}
refreshEarlyAlarm(): void {
const timerTimes: number[] = [];
for (const participant of this.participants) {
const { timer } = this.pendingTimes(participant);
if (timer !== undefined) {
timerTimes.push(timer);
}
}
this.scheduleEarlyAlarm(
timerTimes.length === 0 ? undefined : Math.min(...timerTimes)
);
}
/** Coalesces refreshes without dropping requests arriving after the pending-work read. */
scheduleNextAlarm(): Promise<void> {
this.alarmRefreshRequested = true;
if (this.alarmRefresh) {
return this.alarmRefresh;
}
this.alarmRefresh = (async () => {
try {
do {
// eslint-disable-next-line no-await-in-loop
await this.transaction(() => {
this.alarmRefreshRequested = false;
});
} while (this.alarmRefreshRequested);
} finally {
this.alarmRefresh = undefined;
}
})();
return this.alarmRefresh;
}
/**
* Aborts matching active handlers, then deletes one kind's records created
* before the timestamp and reconciles the alarm in the same transaction.
*/
async purge(
table: DurableTable,
before: number,
active: Map<string, ActiveExecution>,
abortReason: string
): Promise<number> {
if (!Number.isSafeInteger(before) || before < 0) {
throw new RangeError('timestamp must be a non-negative safe integer');
}
for (const running of active.values()) {
if (running.createdAt < before) {
running.controller?.abort(new Error(abortReason));
}
}
const where = `created_at < ?${channelClause(table)}`;
const params = [before, ...channelBindings(table)];
const removed = await this.transaction((sql) => {
const { count } = v.parse(
countRowSchema,
sql
.exec(
`SELECT COUNT(*) AS count FROM ${table.name} WHERE ${where}`,
...params
)
.toArray()[0]
);
sql.exec(`DELETE FROM ${table.name} WHERE ${where}`, ...params);
deleteStaleMemos(sql, table);
return count;
});
if (removed > 0) {
for (const key of this.watchers.get(tableOwner(table))?.keys() ?? []) {
this.notify(table, key);
}
}
return removed;
}
/** In-memory only: listeners never survive eviction. */
watch(
table: DurableTable,
key: string,
listener: SettlementListener
): () => void {
const owner = tableOwner(table);
const byKey =
this.watchers.get(owner) ?? new Map<string, Set<SettlementListener>>();
this.watchers.set(owner, byKey);
const listeners = byKey.get(key) ?? new Set<SettlementListener>();
byKey.set(key, listeners);
listeners.add(listener);
return () => {
listeners.delete(listener);
if (listeners.size === 0 && byKey.get(key) === listeners) {
byKey.delete(key);
}
};
}
/** Call only after the change has committed. */
notify(table: DurableTable, key: string, completed?: ExecutionRow): void {
for (const listener of this.watchers.get(tableOwner(table))?.get(key) ??
[]) {
listener(completed);
}
}
tryAcquirePermit(): (() => void) | undefined {
if (this.availablePermits <= 0) {
return undefined;
}
this.availablePermits -= 1;
return this.createRelease();
}
acquirePermit(): Promise<() => void> {
const release = this.tryAcquirePermit();
if (release) {
return Promise.resolve(release);
}
return new Promise((resolve) => {
this.permitWaiters.push(resolve);
});
}
private createRelease(): () => void {
let released = false;
return () => {
if (released) {
return;
}
released = true;
this.availablePermits += 1;
if (this.availablePermits <= 0) {
return;
}
const waiter = this.permitWaiters.shift();
if (waiter) {
this.availablePermits -= 1;
waiter(this.createRelease());
}
};
}
async runConcurrent<Item>(
items: Item[],
worker: (item: Item) => Promise<void>
): Promise<void> {
let nextIndex = 0;
await Promise.all(
Array.from(
{ length: Math.min(this.alarmConcurrency, items.length) },
async () => {
while (nextIndex < items.length) {
const item = items[nextIndex++];
if (item !== undefined) {
// eslint-disable-next-line no-await-in-loop
await worker(item);
}
}
}
)
);
}
/**
* Runs due work and re-arms the physical alarm. A failure that would
* otherwise propagate to the platform is turned into a retry of our own,
* because the platform drops an alarm after a few failed deliveries.
*/
async alarm(info?: AlarmInvocationInfo): Promise<void> {
try {
await this.runAlarm(info);
this.failedRuns = 0;
} catch (error) {
// Throwing lets the platform retry, but it gives up after a few failures
// and drops the alarm, stranding pending work. Keeping our own alarm
// armed turns the failure into a retry we control instead.
if (!(await this.armFallbackAlarm())) {
throw error;
}
reportFailure(
'durability.alarm.failed',
{ retryInMs: this.alarmMinDelayMs },
error
);
this.notifyAlarmRetry(serializeError(error), info);
this.retryFailedRun();
}
}
/**
* A transient storage fault would otherwise leave all due work to the
* fallback alarm, up to `alarmMinDelayMs` away: longer than a holder's
* grant window, for one. The in-memory timer retries sooner, backing off to
* that fallback while runs keep failing.
*/
private retryFailedRun(): void {
this.failedRuns += 1;
const delay = Math.min(
failedRunRetryMs * 2 ** (this.failedRuns - 1),
this.alarmMinDelayMs
);
this.scheduleEarlyAlarm(Date.now() + delay);
}
private notifyAlarmRetry(
error: SerializedError,
invocation: AlarmInvocationInfo | undefined
): void {
const hook = this.onAlarmRetry;
if (!hook) {
return;
}
const objectId = this.objectId();
const info: AlarmRetryInfo = {
error,
retryInMs: this.alarmMinDelayMs,
};
if (invocation !== undefined) {
info.retryCount = invocation.retryCount;
info.isRetry = invocation.isRetry;
}
if (objectId !== undefined) {
info.objectId = objectId;
}
const report = (cause: unknown): void =>
reportFailure(
'durability.alarm.retry_hook_failed',
{ retryInMs: info.retryInMs },
cause
);
try {
const result = hook(info);
if (result !== undefined) {
this.waitUntil(Promise.resolve(result).catch(report), report);
}
} catch (hookError) {
report(hookError);
}
}
/**
* Runs every participant's due work, hands off to a fresh alarm if the
* invocation approaches its wall-time limit, and re-arms the physical alarm.
*/
private async runAlarm(info?: AlarmInvocationInfo): Promise<void> {
const startedAt = Date.now();
const handoff = Symbol('alarm handoff');
let cancelTimeout: (() => void) | undefined;
let result: void | typeof handoff;
this.activeAlarms += 1;
try {
const execution = Promise.all(
this.participants.map((participant) =>
participant.runDue(startedAt, info)
)
).then(() => undefined);
void execution.catch(() => undefined);
const remaining = this.alarmHandoffMs - (Date.now() - startedAt);
result = await Promise.race([
execution,
new Promise<typeof handoff>((resolve) => {
cancelTimeout = this.timer.schedule(
() => resolve(handoff),
remaining
);
}),
]);
} finally {
cancelTimeout?.();
this.activeAlarms -= 1;
}
if (result === handoff) {
await this.armForHandoff(Date.now());
return;
}
await this.scheduleNextAlarm();
}
/** Arms the physical alarm directly, outside any transaction; the next run reconciles properly. */
private async armFallbackAlarm(): Promise<boolean> {
try {
await this.storage.setAlarm(Date.now() + this.alarmMinDelayMs);
return true;
} catch {
return false;
}
}
private armForHandoff(timestamp: number): Promise<void> {
return this.storage.transaction(async (physicalAlarm) => {
const currentAlarm = await physicalAlarm.getAlarm();
const target = Math.max(timestamp, Date.now() + this.alarmMinDelayMs);
if (currentAlarm === null || currentAlarm > target) {
await physicalAlarm.setAlarm(target);
}
});
}
/**
* Registers the execution in its active map synchronously, then claims and
* runs one attempt in the background. The returned promise settles when the
* attempt's outcome has been persisted. The key leaves the active map and
* its permit is released then too, whether or not a timed-out handler has
* settled: nothing can stop a handler, and one that never settles would
* otherwise keep its retry from ever starting.
*/
startExecution<Result>(
spec: ExecutionSpec<Result>,
eagerPermit?: () => void
): Promise<void> {
const { key, row, policy } = spec;
let settleActive!: () => void;
const settled = new Promise<void>((resolve) => {
settleActive = resolve;
});
const activeExecution: ActiveExecution = {
generation: row.generation_id,
createdAt: row.created_at,
settled,
};
spec.active.set(key, activeExecution);
this.trackActive(spec.table.name, activeKey(spec.table.channel, key));
let releasePermit = eagerPermit;
const execution = (async (): Promise<void> => {
releasePermit ??= await this.acquirePermit();
const attempt = claimAttempt(
this.sql,
spec.table,
key,
row.generation_id,
policy.maxAttempts
);
if (attempt === undefined) {
await this.failExhausted(spec);
return;
}
const startedAt = Date.now();
spec.emit({
...spec.entity,
type: 'attempt_started',
timestamp: startedAt,
attempt,
});
const controller = new AbortController();
activeExecution.controller = controller;
let cancelTimeout: (() => void) | undefined;
try {
const handlerResult = Promise.resolve().then(() =>
spec.invoke(attempt, controller.signal)
);
// After a timeout nothing awaits the handler, which may still reject.
void handlerResult.catch(() => undefined);
const timeoutResult = new Promise<never>((_resolve, reject) => {
cancelTimeout = this.timer.schedule(() => {
const error = spec.timeoutError();
controller.abort(error);
reject(error);
}, policy.attemptTimeoutMs);
});
const result = await Promise.race([handlerResult, timeoutResult]);
// One transaction: a failed memo cleanup must undo the completion, not orphan the memos.
const timestamp = this.transactionSync((sql) => {
const completedAt = spec.complete(result, attempt);
if (completedAt !== undefined) {
deleteMemos(sql, spec.table, key);
}
return completedAt;
});
if (timestamp !== undefined) {
this.notify(spec.table, key, row);
const completed: Extract<
DurabilityLifecycleEvent,
{ outcome: 'completed' }
> = {
...spec.entity,
type: 'attempt_settled',
timestamp,
attempt,
durationMs: Math.max(0, timestamp - startedAt),
outcome: 'completed',
};
if (spec.includeResult) {
completed.result = result;
}
spec.emit(completed);
}
} catch (caught) {
await this.settleFailure(spec, attempt, startedAt, caught);
} finally {
cancelTimeout?.();
}
})().finally(() => {
releasePermit?.();
settleActive();
});
void settled.finally(() => {
if (spec.active.get(key) === activeExecution) {
spec.active.delete(key);
}
this.releaseActive(spec.table.name, activeKey(spec.table.channel, key));
// Re-arm the in-memory timer now that this key is eligible again, so work
// the timer skipped while it executed is picked up at its exact time.
void this.scheduleNextAlarm().catch((error) =>
reportFailure(
'durability.scheduler.refresh_failed',
{ table: spec.table.name, key },
error
)
);
});
return execution;
}
private async failExhausted<Result>(
spec: ExecutionSpec<Result>
): Promise<void> {
const { key, row, policy } = spec;
const exhausted = serializeError(spec.exhaustedError());
const attempt = await this.transaction((sql) => {
const exhaustedAttempt = exhaustAttempts(
sql,
spec.table,
key,
row.generation_id,
policy.maxAttempts,
exhausted
);
if (exhaustedAttempt !== undefined) {
deleteMemos(sql, spec.table, key);
}
return exhaustedAttempt;
});
if (attempt === undefined) {
return;
}
this.notify(spec.table, key);
spec.emit({
...spec.entity,
type: 'terminal',
timestamp: Date.now(),
attempt,
reason: 'attempts_exhausted',
error: exhausted,
});
}
private async settleFailure<Result>(
spec: ExecutionSpec<Result>,
attempt: number,
startedAt: number,
cause: unknown
): Promise<void> {
const { key, row } = spec;
const decision = decideRetry(spec, attempt, cause);
const serializedError = serializeError(decision.error);
const updated = await this.transaction((sql) => {
const owned = updateAttempt(
sql,
spec.table,
{ key, generation: row.generation_id, attempt },
'status = ?, next_attempt_at = ?, last_error = ?, last_error_name = ?',
decision.terminal ? 'failed' : 'pending',
decision.nextAttemptAt,
serializedError.message,
serializedError.name
);
if (owned && decision.terminal) {
deleteMemos(sql, spec.table, key);
}
return owned;
});
if (!updated) {
return;
}
if (decision.terminal) {
this.notify(spec.table, key);
}
const settled = {
...spec.entity,
type: 'attempt_settled' as const,
timestamp: decision.timestamp,
attempt,
durationMs: Math.max(0, decision.timestamp - startedAt),
error: serializedError,
};
spec.emit(
decision.terminal
? { ...settled, outcome: 'failed' }
: {
...settled,
outcome: 'retry_scheduled',
nextAttemptAt: decision.nextAttemptAt,
}
);
}
}
const engines = new WeakMap<DurabilityScheduler, Engine>();
type ObjectScheduler = { engine: unknown; installation: symbol };
// Every copy of this package in an isolate shares the registry, so a second
// copy reaching the same object is caught instead of fighting over its alarm.
const installation = Symbol('durability installation');
const registryKey: unique symbol = Symbol.for('durability.object-schedulers');
// SAFETY: registryKey is this package's registered symbol, and every copy
// stores only a WeakMap<object, ObjectScheduler> under it on globalThis.
const globalRegistry = globalThis as {
[registryKey]?: WeakMap<object, ObjectScheduler>;
};
const objectSchedulers = (globalRegistry[registryKey] ??= new WeakMap<
object,
ObjectScheduler
>());
/** The one engine behind every scheduler and helper of a Durable Object. */
const engineForObject = (config: DurabilitySchedulerConfig): Engine => {
const { context } = config;
const existing = objectSchedulers.get(context.storage);
if (existing === undefined) {
const engine = new Engine(context, config);
objectSchedulers.set(context.storage, { engine, installation });
return engine;
}
if (existing.installation !== installation) {
throw new Error(
'Another copy of durability is already attached to this Durable Object; install durability once, for example as a peer dependency'
);
}
// SAFETY: the installation matches, so this copy of the module created the
// entry above with `new Engine`.
const engine = existing.engine as Engine;
engine.attach(context, config);
return engine;
};
/**
* Owns the Durable Object's physical alarm, storage, and concurrency pool so
* every durability helper on the object shares them.
*
* A Durable Object has one scheduler. Constructing another for the same
* object, or a helper from the object's context, joins it; scheduler options
* merge, and a different value for an option already set throws. Delegate the
* object's `alarm` method to {@link DurabilityScheduler.alarm} or to any
* helper's `alarm`: each runs the due work of every helper on the object.
*
* @example
* ```ts
* class Jobs extends DurableObject<Env> {
* private readonly scheduler = new DurabilityScheduler({ context: this.ctx });
* private readonly durability = new Durability({
* scheduler: this.scheduler,
* handlers,
* });
* private readonly alarms = new DurabilityAlarms({
* scheduler: this.scheduler,
* handlers: alarmHandlers,
* });
*
* alarm(info?: AlarmInvocationInfo) {
* return this.scheduler.alarm(info);
* }
* }
* ```
*/
export class DurabilityScheduler {
constructor(config: DurabilitySchedulerConfig) {
engines.set(this, engineForObject(config));
}
/** Runs due work for every attached helper and re-arms the physical alarm. */
alarm(info?: AlarmInvocationInfo): Promise<void> {
return engineFor({ scheduler: this }).alarm(info);
}
/**
* Runs `callback` once after `delayMs`, like the global `setTimeout`, and
* returns a function that cancels it. The timeout shares one native timer
* with every helper on the object, so any number of them can be pending
* without reaching the runtime's limit of 10,000 native timers.
*
* The timeout exists only in memory and an eviction drops it; schedule work
* that must survive one with `DurabilityAlarms`. Callbacks due at the same
* time run one after another in the same task, and one that throws is
* reported as `durability.timer.callback_failed` instead of being thrown.
*/
setTimeout(callback: () => void, delayMs: number): () => void {
if (!isFunction(callback)) {
throw new TypeError('callback must be a function');
}
return engineFor({ scheduler: this }).timer.schedule(callback, delayMs);
}
}
traceMethods(DurabilityScheduler.prototype, 'DurabilityScheduler');
/** Resolves the engine a helper attaches to: the object's one scheduler. */
export const engineFor = (attachment: SchedulerAttachment): Engine => {
if (attachment.scheduler === undefined) {
return engineForObject(attachment);
}
const engine = engines.get(attachment.scheduler);
if (!engine) {
throw new Error('Unknown DurabilityScheduler instance');
}
return engine;
};