@hotmeshio/hotmesh
Version:
Serverless Workflow
36 lines (35 loc) • 1.32 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.sleepFor = void 0;
const common_1 = require("./common");
const didRun_1 = require("./didRun");
/**
* Sleeps the workflow for a specified duration, deterministically.
* On replay, it will not actually sleep again, but resume after sleep.
*
* @param {string} duration - A human-readable duration string (e.g., '1m', '2 hours').
* @returns {Promise<number>} The resolved duration in seconds.
*/
async function sleepFor(duration) {
const [didRunAlready, execIndex, result] = await (0, didRun_1.didRun)('sleep');
if (didRunAlready) {
return result.duration;
}
const store = common_1.asyncLocalStorage.getStore();
const interruptionRegistry = store.get('interruptionRegistry');
const workflowId = store.get('workflowId');
const workflowDimension = store.get('workflowDimension') ?? '';
const interruptionMessage = {
workflowId,
duration: (0, common_1.s)(duration),
index: execIndex,
workflowDimension,
};
interruptionRegistry.push({
code: common_1.HMSH_CODE_MESHFLOW_SLEEP,
...interruptionMessage,
});
await (0, common_1.sleepImmediate)();
throw new common_1.MeshFlowSleepError(interruptionMessage);
}
exports.sleepFor = sleepFor;
;