// Only this platform-authored module runs in an account Worker. The application // receives its WorkflowEvent and WorkflowStep over RPC in its isolated Worker. export const managedWorkflowBridgeSource = `import { WorkflowEntrypoint } from 'cloudflare:workers'; export class VoidManagedWorkflow extends WorkflowEntrypoint { async run(event, step) { const { instanceId } = event; const startedAt = event.timestamp.getTime(); let target; while (!target) { // Transport or database failures must not terminate a sleeping application. target = await this.env.CONTROL.admit(instanceId, startedAt).catch(() => null); if (target?.denied) { throw new Error(target.denied); } if (!target) { // On wake the engine can replay run() from the beginning. Sleep only until // the next wall-clock deadline, never through the history of past polls. const elapsed = Math.max(0, Date.now() - startedAt); const interval = elapsed < 30_000 ? 5_000 : elapsed < 300_000 ? 30_000 : 60_000; const wakeAt = startedAt + (Math.floor(elapsed / interval) + 1) * interval; await step.sleepUntil('Wait for Void Workflow capacity ' + wakeAt, wakeAt); } } const worker = this.env.APPS.get(target.workerName, {}, { limits: target.limits }); const output = await worker.runVoidWorkflow(target.className, event, step); // Failed or terminated runs are released once their provider status is terminal. const candidates = await this.env.CONTROL.release(instanceId, startedAt).catch(() => []); if (candidates?.length) { this.ctx.waitUntil(Promise.allSettled(candidates.map(async ({ id, at }) => { const previous = await this.env.OWNER.get(id); const { status } = await previous.status(); if (['complete', 'errored', 'terminated'].includes(status)) { await this.env.CONTROL.forget(id, at); } }))); } return output; } } export default { fetch() { return new Response(null, { status: 404 }); } }; `;