64 lines
2.2 KiB
JavaScript
64 lines
2.2 KiB
JavaScript
(function initExecutionGuard(root, factory) {
|
|
const api = factory();
|
|
if (typeof module === 'object' && module.exports) module.exports = api;
|
|
root.LTJTExecutionGuard = api;
|
|
})(typeof self !== 'undefined' ? self : globalThis, function createExecutionGuard() {
|
|
'use strict';
|
|
|
|
const TERMINAL_STATES = new Set(['completed', 'blocked', 'cancelled', 'uncertain']);
|
|
const WRITE_STATES = new Set(['write_started', 'submitted', 'uncertain', 'completed']);
|
|
const RESULT_TIMEOUT_MS = 10 * 60 * 1000;
|
|
const HISTORY_MAINTENANCE_TIMEOUT_MS = 30 * 60 * 1000;
|
|
|
|
function rootTaskId(taskId) {
|
|
return String(taskId || '').split('#')[0];
|
|
}
|
|
|
|
function acceptanceDecision(existing, executionId) {
|
|
const normalizedExecutionId = String(executionId || '').trim();
|
|
if (!normalizedExecutionId) {
|
|
return { accepted: false, status: 'missing_execution_id', reason: '缺少服务端执行编号。' };
|
|
}
|
|
if (!existing) return { accepted: true, status: 'accepted', reason: '' };
|
|
if (String(existing.execution_id || '') !== normalizedExecutionId) {
|
|
return { accepted: false, status: 'duplicate_blocked', reason: '同一任务出现了不同执行编号。' };
|
|
}
|
|
return {
|
|
accepted: false,
|
|
status: TERMINAL_STATES.has(existing.state) ? 'already_finished' : 'already_started',
|
|
reason: '该执行编号已经被插件接收,重复消息不会再次执行。'
|
|
};
|
|
}
|
|
|
|
function failureState(existing) {
|
|
return existing && WRITE_STATES.has(existing.state) ? 'uncertain' : 'blocked';
|
|
}
|
|
|
|
function timeoutDisposition(existing) {
|
|
const writeStarted = Boolean(existing && WRITE_STATES.has(existing.state));
|
|
return {
|
|
state: writeStarted ? 'uncertain' : 'blocked',
|
|
result_status: writeStarted ? 'execution_uncertain' : 'failed',
|
|
write_attempted: writeStarted,
|
|
no_erp_write: !writeStarted,
|
|
reconciliation_required: writeStarted
|
|
};
|
|
}
|
|
|
|
function canTransition(existing, executionId) {
|
|
return Boolean(existing) && String(existing.execution_id || '') === String(executionId || '');
|
|
}
|
|
|
|
return {
|
|
TERMINAL_STATES,
|
|
WRITE_STATES,
|
|
RESULT_TIMEOUT_MS,
|
|
HISTORY_MAINTENANCE_TIMEOUT_MS,
|
|
rootTaskId,
|
|
acceptanceDecision,
|
|
failureState,
|
|
timeoutDisposition,
|
|
canTransition
|
|
};
|
|
});
|