@5minds/processcube_engine
Version:
The ProcessCube Engine. Stores and executes BPMNs.
77 lines • 4.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SequentialActivityExecutionStrategy = void 0;
const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk");
const ActivityExecutionStrategy_1 = require("./ActivityExecutionStrategy");
class SequentialActivityExecutionStrategy extends ActivityExecutionStrategy_1.ActivityExecutionStrategy {
async execute(startToken) {
this.activityHandler.abortSignal.throwIfAborted();
const data = Array.isArray(startToken) ? startToken : [startToken];
const results = [];
let previousFlowNodeInstanceId = this.activityHandler.previousFlowNodeInstanceId;
for (const dataSet of data) {
const instanceContext = {
currentLoopIndex: results.length,
totalLoopElements: data.length,
loopResults: results,
};
const newActivityInstanceHandler = this.createNewActivityInstanceHandler(previousFlowNodeInstanceId, instanceContext);
this.activityInstanceHandlers.push(newActivityInstanceHandler);
const result = await newActivityInstanceHandler.execute(dataSet);
results.push(result);
previousFlowNodeInstanceId = newActivityInstanceHandler.flowNodeInstanceId;
if (this._timeoutBetweenLoopIterations && results.length < data.length) {
await new Promise((resolve) => setTimeout(resolve, this._timeoutBetweenLoopIterations));
}
}
return results;
}
async resume(flowNodeInstancesToResume, startToken) {
this.activityHandler.abortSignal.throwIfAborted();
const hasNoFlowNodeInstance = !flowNodeInstancesToResume || flowNodeInstancesToResume.length <= 0;
if (hasNoFlowNodeInstance) {
throw new processcube_engine_sdk_1.InternalServerError('No flowNodeInstances for resuming were supplied.', 'process');
}
const results = [];
let previousFlowNodeInstanceId = this.activityHandler.previousFlowNodeInstanceId;
const persistedFlowNodeInstances = await this.flowNodeInstanceDatabaseAdapter.loadFlowNodeInstancesForMultiInstance(this.activityHandler.multiInstanceMetadataId);
const persistedFlowNodeInstancesWhichAreAlreadyFinished = persistedFlowNodeInstances.filter((fni) => {
return fni.state === processcube_engine_sdk_1.FlowNodeInstanceState.finished;
});
const persistedFlowNodeInstancesForResuming = persistedFlowNodeInstances.filter((fni) => {
return fni.state === processcube_engine_sdk_1.FlowNodeInstanceState.running || fni.state === processcube_engine_sdk_1.FlowNodeInstanceState.suspended;
});
for (const fni of persistedFlowNodeInstancesWhichAreAlreadyFinished) {
const newActivityInstanceHandler = this.createNewActivityInstanceHandler(fni.previousFlowNodeInstanceId);
newActivityInstanceHandler.initFromFlowNodeInstance(fni);
this.activityInstanceHandlers.push(newActivityInstanceHandler);
}
for (const flowNodeInstance of persistedFlowNodeInstancesForResuming) {
const newActivityInstanceHandler = this.createNewActivityInstanceHandler(flowNodeInstance.previousFlowNodeInstanceId);
this.activityInstanceHandlers.push(newActivityInstanceHandler);
const result = await newActivityInstanceHandler.resume(flowNodeInstance);
results.push(result);
previousFlowNodeInstanceId = newActivityInstanceHandler.flowNodeInstanceId;
}
const allStartedFlowNodeInstances = [...persistedFlowNodeInstancesWhichAreAlreadyFinished, ...persistedFlowNodeInstancesForResuming];
const data = Array.isArray(startToken) ? startToken : [startToken];
const deltaOfResumedFnisToPlannedFnis = data.length - allStartedFlowNodeInstances.length;
if (deltaOfResumedFnisToPlannedFnis > 0) {
for (let i = allStartedFlowNodeInstances.length; i < data.length; i++) {
if (this._timeoutBetweenLoopIterations) {
await new Promise((resolve) => setTimeout(resolve, this._timeoutBetweenLoopIterations));
}
const newActivityInstanceHandler = this.createNewActivityInstanceHandler(previousFlowNodeInstanceId);
const result = await newActivityInstanceHandler.execute(data[i]);
results.push(result);
previousFlowNodeInstanceId = newActivityInstanceHandler.flowNodeInstanceId;
}
}
return results;
}
async endWithError(error, force = false) {
return this.runOnLastFlowNodeInstance((fni) => fni.endWithError(error, force));
}
}
exports.SequentialActivityExecutionStrategy = SequentialActivityExecutionStrategy;
//# sourceMappingURL=SequentialActivityExecutionStrategy.js.map