UNPKG

n8n

Version:

n8n Workflow Automation Tool

104 lines 3.69 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WorkflowPublicationLifecycleLock = void 0; const di_1 = require("@n8n/di"); let WorkflowPublicationLifecycleLock = class WorkflowPublicationLifecycleLock { constructor() { this.stateByWorkflowId = new Map(); } isLocked(workflowId) { return this.stateByWorkflowId.has(workflowId); } getLockedWorkflowIds() { return Array.from(this.stateByWorkflowId.keys()); } async runExclusive(workflowId, fn) { await this.acquire(workflowId); try { return await fn(); } finally { this.release(workflowId); } } async runExclusiveOrTimeout(workflowId, fn, timeoutMs) { const acquired = await this.acquireWithTimeout(workflowId, timeoutMs); if (!acquired) { await fn(); return { timedOut: true }; } try { await fn(); } finally { this.release(workflowId); } return { timedOut: false }; } getOrCreateState(workflowId) { let state = this.stateByWorkflowId.get(workflowId); if (!state) { state = { locked: false, waiters: [] }; this.stateByWorkflowId.set(workflowId, state); } return state; } async acquire(workflowId) { const state = this.getOrCreateState(workflowId); if (!state.locked) { state.locked = true; return; } await new Promise((resolve) => state.waiters.push(resolve)); } release(workflowId) { const state = this.stateByWorkflowId.get(workflowId); if (!state) return; const next = state.waiters.shift(); if (next) { next(); } else { this.stateByWorkflowId.delete(workflowId); } } async acquireWithTimeout(workflowId, timeoutMs) { const state = this.getOrCreateState(workflowId); if (!state.locked) { state.locked = true; return true; } return await new Promise((resolve) => { let settled = false; const waiter = () => { if (settled) return; settled = true; clearTimeout(timer); resolve(true); }; const timer = setTimeout(() => { if (settled) return; settled = true; const index = state.waiters.indexOf(waiter); if (index !== -1) state.waiters.splice(index, 1); resolve(false); }, timeoutMs); state.waiters.push(waiter); }); } }; exports.WorkflowPublicationLifecycleLock = WorkflowPublicationLifecycleLock; exports.WorkflowPublicationLifecycleLock = WorkflowPublicationLifecycleLock = __decorate([ (0, di_1.Service)() ], WorkflowPublicationLifecycleLock); //# sourceMappingURL=workflow-publication-lifecycle-lock.js.map