29 lines
1.6 KiB
JavaScript
29 lines
1.6 KiB
JavaScript
;(function (global) {
|
|
'use strict'
|
|
|
|
function text(value, max) { return value === undefined || value === null ? '' : String(value).slice(0, max || 120) }
|
|
function makeId(prefix) { return text(prefix || 'run', 24) + '-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2, 8) }
|
|
function create(input) {
|
|
const source = input || {}
|
|
const now = Date.now()
|
|
return {
|
|
runId: text(source.runId || makeId('run'), 120),
|
|
nodeId: text(source.nodeId, 120),
|
|
taskId: text(source.taskId, 120),
|
|
state: text(source.state || 'queued', 40),
|
|
startedAt: Number(source.startedAt) || now,
|
|
finishedAt: 0,
|
|
retryCount: Math.max(0, Number(source.retryCount) || 0),
|
|
cancelled: false,
|
|
diagnostics: []
|
|
}
|
|
}
|
|
function addRetry(context, count) { if (!context) return context; context.retryCount = Math.max(0, context.retryCount + Math.max(0, Number(count) || 0)); return context }
|
|
function finish(context, state) { if (!context) return context; context.state = text(state || 'succeeded', 40); context.finishedAt = Date.now(); context.cancelled = context.state === 'cancelled'; return context }
|
|
function diagnostic(context, input) {
|
|
const source = input || {}
|
|
return Object.assign({}, source, { runId: text(source.runId || (context && context.runId), 120), retryCount: Math.max(0, Number(source.retryCount === undefined ? context && context.retryCount : source.retryCount) || 0) })
|
|
}
|
|
global.IWBRuntimeRunContext = { create, addRetry, finish, diagnostic }
|
|
})(typeof window !== 'undefined' ? window : globalThis)
|