flowed
Version:
A fast and reliable flow engine for orchestration and more uses in *Node.js*, *Deno* and the browser
115 lines • 5.16 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FlowRunStatus = void 0;
const types_1 = require("../types");
const flow_state_1 = require("./flow-state");
const process_manager_1 = require("./process-manager");
const task_1 = require("./task");
class FlowRunStatus {
constructor(flow, spec, runStatus) {
this.tasksReady = [];
this.tasksByReq = {};
this.resolvers = {};
this.expectedResults = [];
this.results = {};
this.context = {};
this.runOptions = {};
this.flow = flow;
this.processManager = new process_manager_1.ProcessManager();
this.id = FlowRunStatus.nextId;
FlowRunStatus.nextId = (FlowRunStatus.nextId + 1) % Number.MAX_SAFE_INTEGER;
this.states = {
Ready: new flow_state_1.FlowReady(this),
Running: new flow_state_1.FlowRunning(this),
Finished: new flow_state_1.FlowFinished(this),
Pausing: new flow_state_1.FlowPausing(this),
Paused: new flow_state_1.FlowPaused(this),
Stopping: new flow_state_1.FlowStopping(this),
Stopped: new flow_state_1.FlowStopped(this),
};
this.state = this.states[types_1.FlowStateEnum.Ready];
this.initRunStatus(spec, runStatus);
}
initRunStatus(spec, runState) {
var _a, _b, _c;
this.spec = spec;
this.tasks = {};
const provisions = [];
for (const [taskCode, taskSpec] of Object.entries((_a = this.spec.tasks) !== null && _a !== void 0 ? _a : {})) {
provisions.push(...((_b = taskSpec.provides) !== null && _b !== void 0 ? _b : []));
this.tasks[taskCode] = new task_1.Task(taskCode, taskSpec);
}
this.taskProvisions = Array.from(new Set(provisions));
this.options = Object.assign(Object.assign({}, this.spec.configs), this.spec.options);
if (Object.prototype.hasOwnProperty.call(this.spec, 'configs')) {
this.flow.log({ m: "DEPRECATED: 'configs' field in flow spec. Use 'options' instead.", l: 'w' });
}
this.tasksByReq = {};
this.tasksReady = [];
for (const task of Object.values(this.tasks)) {
task.resetRunStatus();
if (task.isReadyToRun()) {
this.tasksReady.push(task);
}
const taskReqs = (_c = task.spec.requires) !== null && _c !== void 0 ? _c : [];
for (const req of taskReqs) {
if (typeof this.tasksByReq[req] === 'undefined') {
this.tasksByReq[req] = {};
}
this.tasksByReq[req][task.code] = task;
}
}
this.results = {};
if (runState) {
this.fromSerializable(runState);
}
}
fromSerializable(runState) {
this.id = runState.id;
this.processManager.nextProcessId = runState.nextProcessId;
this.processManager.processes = [];
this.tasksReady = runState.tasksReady.map(taskCode => this.tasks[taskCode]);
this.tasksByReq = {};
for (const [req, tasks] of Object.entries(runState.tasksByReq)) {
this.tasksByReq[req] = tasks.reduce((accum, taskCode) => {
accum[taskCode] = this.tasks[taskCode];
return accum;
}, {});
}
this.taskProvisions = JSON.parse(JSON.stringify(runState.taskProvisions));
this.expectedResults = JSON.parse(JSON.stringify(runState.expectedResults));
this.results = JSON.parse(JSON.stringify(runState.results));
this.context = JSON.parse(JSON.stringify(runState.context));
this.options = JSON.parse(JSON.stringify(runState.options));
for (const [taskCode, taskStatus] of Object.entries(runState.taskStatuses)) {
this.tasks[taskCode].setSerializableState(taskStatus);
}
}
toSerializable() {
const serialized = {
id: this.id,
nextProcessId: this.processManager.nextProcessId,
tasksReady: this.tasksReady.map(task => task.code),
tasksByReq: {},
taskProvisions: JSON.parse(JSON.stringify(this.taskProvisions)),
expectedResults: JSON.parse(JSON.stringify(this.expectedResults)),
results: JSON.parse(JSON.stringify(this.results)),
context: {},
options: JSON.parse(JSON.stringify(this.options)),
taskStatuses: {},
};
const serializableContext = Object.assign({}, this.context);
delete serializableContext.$flowed;
serialized.context = JSON.parse(JSON.stringify(serializableContext));
for (const [req, taskMap] of Object.entries(this.tasksByReq)) {
serialized.tasksByReq[req] = Object.keys(taskMap);
}
for (const [taskCode, task] of Object.entries(this.tasks)) {
serialized.taskStatuses[taskCode] = JSON.parse(JSON.stringify(task.getSerializableState()));
}
return serialized;
}
}
exports.FlowRunStatus = FlowRunStatus;
FlowRunStatus.nextId = 1;
//# sourceMappingURL=flow-run-status.js.map