UNPKG

@hotmeshio/hotmesh

Version:

Serverless Workflow

34 lines (33 loc) 1.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isSideEffectAllowed = void 0; const common_1 = require("./common"); /** * Checks if a side-effect is allowed to run. This ensures certain actions * are executed exactly once. * @private * @param {HotMesh} hotMeshClient - The HotMesh client. * @param {string} prefix - A unique prefix representing the action (e.g., 'trace', 'emit', etc.) * @returns {Promise<boolean>} True if the side effect can run, false otherwise. */ async function isSideEffectAllowed(hotMeshClient, prefix) { const store = common_1.asyncLocalStorage.getStore(); const workflowId = store.get('workflowId'); const workflowDimension = store.get('workflowDimension') ?? ''; const COUNTER = store.get('counter'); const execIndex = COUNTER.counter = COUNTER.counter + 1; const sessionId = `-${prefix}${workflowDimension}-${execIndex}-`; const replay = store.get('replay'); if (sessionId in replay) { return false; } const keyParams = { appId: hotMeshClient.appId, jobId: workflowId, }; const workflowGuid = common_1.KeyService.mintKey(hotMeshClient.namespace, common_1.KeyType.JOB_STATE, keyParams); const searchClient = hotMeshClient.engine.search; const guidValue = await searchClient.incrementFieldByFloat(workflowGuid, sessionId, 1); return guidValue === 1; } exports.isSideEffectAllowed = isSideEffectAllowed;