UNPKG

durabull

Version:

A durable workflow engine built on top of BullMQ and Redis

36 lines (35 loc) 1.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getVirtualTimestamp = exports.runInWorkflowContext = exports.getWorkflowContext = void 0; const async_hooks_1 = require("async_hooks"); const workflowContext = new async_hooks_1.AsyncLocalStorage(); function getWorkflowContext() { return workflowContext.getStore(); } exports.getWorkflowContext = getWorkflowContext; function runInWorkflowContext(ctx, fn) { return workflowContext.run(ctx, fn); } exports.runInWorkflowContext = runInWorkflowContext; const getVirtualTimestamp = (workflowId) => { const ctx = workflowContext.getStore(); if (!ctx || !workflowId) { return Date.now(); } // Use clock cursor for deterministic replay const cursor = ctx.clockCursor ?? 0; const existing = ctx.record.clockEvents?.[cursor]; if (typeof existing === 'number' && !Number.isNaN(existing)) { ctx.clockCursor = cursor + 1; return existing; } // First execution - record timestamp const timestamp = Date.now(); if (!ctx.record.clockEvents) { ctx.record.clockEvents = []; } ctx.record.clockEvents[cursor] = timestamp; ctx.clockCursor = cursor + 1; return timestamp; }; exports.getVirtualTimestamp = getVirtualTimestamp;