UNPKG

agents

Version:
118 lines (117 loc) • 4.75 kB
import { RetryOptions } from "../retries.js"; import "../client-storage-D633wI1S.js"; import { Agent, Schedule } from "../index.js"; //#region src/experimental/forever.d.ts type FiberState = { id: string; callback: string; payload: unknown; snapshot: unknown | null; status: "running" | "completed" | "failed" | "interrupted" | "cancelled"; retryCount: number; maxRetries: number; result: unknown | null; error: string | null; startedAt: number | null; updatedAt: number | null; completedAt: number | null; createdAt: number; }; type FiberRecoveryContext = { id: string; methodName: string; payload: unknown; snapshot: unknown | null; retryCount: number; }; type FiberContext = { id: string; snapshot: unknown | null; retryCount: number; }; type FiberCompleteContext = { id: string; methodName: string; payload: unknown; result: unknown; }; type RawFiberRow = { id: string; callback: string; payload: string | null; snapshot: string | null; status: string; retry_count: number; max_retries: number; result: string | null; error: string | null; started_at: number | null; updated_at: number | null; completed_at: number | null; created_at: number; }; type Constructor<T = object> = new (...args: any[]) => T; type AgentLike = Constructor<Pick<Agent<Cloudflare.Env>, "sql" | "scheduleEvery" | "cancelSchedule" | "alarm">>; declare function withFibers<TBase extends AgentLike>(Base: TBase, options?: { debugFibers?: boolean; }): { new (...args: any[]): { /** @internal */_fiberActiveFibers: Set<string>; /** @internal */ _fiberRecoveryInProgress: boolean; /** @internal */ _fiberLastCleanupTime: number; /** @internal */ _fiberDebug(msg: string, ...args: unknown[]): void; /** @internal */ _cf_fiberHeartbeat(): Promise<void>; keepAlive(): Promise<() => void>; spawnFiber(methodName: keyof /*elided*/any, payload?: unknown, options?: { maxRetries?: number; }): string; stashFiber(data: unknown): void; /** * Note: cancellation is cooperative. The status is set to 'cancelled' * in SQLite, and the _runFiber retry loop checks for this status at * the top of each iteration. */ cancelFiber(fiberId: string): boolean; getFiber(fiberId: string): FiberState | null; restartFiber(fiberId: string): void; /** * Manually trigger fiber recovery check. * In production, this runs automatically via the heartbeat schedule. * Useful for testing or when you need immediate recovery after * detecting an eviction. */ checkFibers(): Promise<void>; onFiberComplete(_ctx: FiberCompleteContext): void | Promise<void>; onFiberRecovered(ctx: FiberRecoveryContext): void | Promise<void>; onFibersRecovered(fibers: FiberRecoveryContext[]): Promise<void>; /** @internal */ _getRawFiber(fiberId: string): RawFiberRow | null; /** @internal */ _safeJsonParse(value: string | null): unknown; /** @internal */ _toFiberState(raw: RawFiberRow): FiberState; /** @internal */ _startFiber(id: string, methodName: string, payload: unknown, maxRetries: number): Promise<void>; /** @internal */ _runFiber(id: string, methodName: string, payload: unknown, maxRetries: number, disposeKeepAlive: () => void): Promise<void>; /** @internal */ _checkInterruptedFibers(): Promise<void>; /** @internal */ _cleanupOrphanedHeartbeats(): void; /** @internal */ _maybeCleanupFibers(): void; sql: <T = Record<string, string | number | boolean | null>>(strings: TemplateStringsArray, ...values: (string | number | boolean | null)[]) => T[]; scheduleEvery: <T = string>(intervalSeconds: number, callback: keyof Agent<Cloudflare.Env, unknown, Record<string, unknown>>, payload?: T | undefined, options?: { retry?: RetryOptions; }) => Promise<Schedule<T>>; cancelSchedule: (id: string) => Promise<boolean>; readonly alarm: () => Promise<void>; }; } & TBase; /** * Keep a Durable Object alive via a scheduled heartbeat. * Returns a disposer function that cancels the heartbeat. * * Standalone version usable by any Agent subclass without requiring * the full fiber mixin. The agent must have a no-op method with the * given callbackName for the scheduler to invoke. * * @param agent - The agent instance (must have scheduleEvery and cancelSchedule) * @param callbackName - Name of a no-op method on the agent class (must exist) */ declare function keepAlive(agent: Pick<Agent<Cloudflare.Env>, "scheduleEvery" | "cancelSchedule">, callbackName: string): Promise<() => void>; //#endregion export { FiberCompleteContext, FiberContext, FiberRecoveryContext, FiberState, keepAlive, withFibers }; //# sourceMappingURL=forever.d.ts.map