@5minds/processcube_engine
Version:
The ProcessCube Engine. Stores and executes BPMNs.
210 lines • 11.4 kB
JavaScript
"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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IntermediateSignalCatchEventHandler = void 0;
const inversify_1 = require("inversify");
const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk");
const index_1 = require("../../../Contracts/index");
const AbortablePromise_1 = require("../../../Tools/AbortablePromise");
const index_2 = require("../../../Tools/DatabaseAdaptersSequelize/index");
const EventMiddlewareHandler_1 = require("../../../Tools/EventMiddlewareHandler");
const ProcessInstance_1 = require("../../ProcessInstance");
const SignalEventService_1 = require("../../SignalEventService");
const FlowNodeHandlerFactory_1 = require("../FlowNodeHandlerFactory");
const EventHandler_1 = require("./EventHandler");
let IntermediateSignalCatchEventHandler = class IntermediateSignalCatchEventHandler extends EventHandler_1.EventHandler {
signalEventService;
onSignalSubscriptionId;
acknowledgeEventReceivedId;
triggerValue;
constructor(eventMiddlewareHandler, flowNodeHandlerFactory, flowNodeInstanceDatabaseAdapter, signalCatchEventModel, processInstance, signalEventService) {
super(eventMiddlewareHandler, flowNodeHandlerFactory, flowNodeInstanceDatabaseAdapter, signalCatchEventModel, processInstance, 'signal_catch_event_handler');
this.signalEventService = signalEventService;
}
get signalCatchEvent() {
return this.flowNode;
}
async persistOnSuspend() {
this.abortSignal.signal.throwIfAborted();
await super.persistOnSuspend(undefined, {
type: index_1.FlowNodeInstanceDataTypes.catchEvent,
eventName: this.signalCatchEvent.signalEventDefinition.name,
triggerValue: this.triggerValue,
});
this.publishIntermediateCatchEventReachedNotification();
}
async persistOnExit() {
await super.persistOnExit({
type: index_1.FlowNodeInstanceDataTypes.catchEvent,
eventName: this.signalCatchEvent.signalEventDefinition.name,
triggerValue: this.triggerValue,
triggeredByFlowNodeInstanceId: this.triggeredByFlowNodeInstanceId,
});
this.publishIntermediateCatchEventFinishedNotification();
}
async persistOnError(error) {
await super.persistOnError(error);
this.publishIntermediateCatchEventFinishedNotification();
}
async persistOnTerminate() {
await super.persistOnTerminate();
this.publishIntermediateCatchEventFinishedNotification();
}
async afterExecute() {
await super.afterExecute();
this.signalEventService.acknowledge(this.acknowledgeEventReceivedId);
this.signalEventService.unsubscribe(this.onSignalSubscriptionId);
}
async startExecution(executionCanceledCallback) {
this.abortSignal.signal.throwIfAborted();
this.logger.debug(`Executing SignalCatchEvent instance.`);
return this.executeHandler(executionCanceledCallback);
}
async executeHandler(executionCanceledCallback) {
this.abortSignal.signal.throwIfAborted();
return new AbortablePromise_1.AbortablePromise(async (resolve, reject, onCancel) => {
try {
if (executionCanceledCallback) {
onCancel(() => {
if (this.state === processcube_engine_sdk_1.FlowNodeInstanceState.suspended || this.state === processcube_engine_sdk_1.FlowNodeInstanceState.running || this.state === processcube_engine_sdk_1.FlowNodeInstanceState.canceled) {
executionCanceledCallback();
}
});
}
this.validateSignal();
this.onInterruptedCallback = () => {
this.signalEventService.unsubscribe(this.onSignalSubscriptionId);
this.abortSignal.abort();
};
await this.parseTriggerValue();
const receivedMessage = await this.suspendAndWaitForSignal();
const canceledByEventBasedGateway = await this.passTriggerThroughEventBasedGatwwayIfNecessary();
if (canceledByEventBasedGateway) {
this.cancel(this.processToken);
return;
}
this.processToken = receivedMessage?.currentToken ?? {};
const nextFlowNodeInfo = this.processInstance.getProcessModelFacade().getNextFlowNodesFor(this.signalCatchEvent);
return resolve(nextFlowNodeInfo);
}
catch (error) {
return reject(error);
}
}, { signal: this.abortSignal.signal });
}
async resumeAfterSuspend(flowNodeInstance, resumptionCanceledCallback) {
this.abortSignal.signal.throwIfAborted();
return new AbortablePromise_1.AbortablePromise(async (resolve, reject, onCancel) => {
try {
if (resumptionCanceledCallback) {
onCancel(() => {
if (this.state === processcube_engine_sdk_1.FlowNodeInstanceState.suspended || this.state === processcube_engine_sdk_1.FlowNodeInstanceState.running || this.state === processcube_engine_sdk_1.FlowNodeInstanceState.canceled) {
resumptionCanceledCallback();
}
});
}
this.onInterruptedCallback = () => {
this.signalEventService.unsubscribe(this.onSignalSubscriptionId);
this.abortSignal.abort();
};
this.triggerValue = flowNodeInstance.triggerValue;
const receivedMessagePromise = this.waitForSignal();
this.publishIntermediateCatchEventReachedNotification();
const receivedMessage = await receivedMessagePromise;
const canceledByEventBasedGateway = await this.passTriggerThroughEventBasedGatwwayIfNecessary();
if (canceledByEventBasedGateway) {
this.cancel(this.processToken);
return;
}
this.processToken = receivedMessage?.currentToken ?? {};
const nextFlowNodeInfo = this.processInstance.getProcessModelFacade().getNextFlowNodesFor(this.signalCatchEvent);
return resolve(nextFlowNodeInfo);
}
catch (error) {
return reject(error);
}
}, { signal: this.abortSignal.signal });
}
async suspendAndWaitForSignal() {
this.abortSignal.signal.throwIfAborted();
const waitForSignalPromise = this.waitForSignal();
await this.persistOnSuspend();
return waitForSignalPromise;
}
async waitForSignal() {
this.abortSignal.signal.throwIfAborted();
this.logger.debug(`Waiting for a "${this.signalCatchEvent.signalEventDefinition.name}" Signal`);
return new Promise((resolve) => {
const signalReceivedCallback = async (signal) => {
if (this.abortSignal.signal.aborted) {
this.signalEventService.acknowledge(signal.eventId);
return;
}
this.logger.debug(`SignalCatchEvent instance ${this.flowNodeInstanceId} received signal ${signal.signalReference}:`, {
receivedSignal: signal,
});
this.triggeredByFlowNodeInstanceId = signal.flowNodeInstanceId;
this.acknowledgeEventReceivedId = signal.eventId;
resolve(signal);
};
this.onSignalSubscriptionId = this.signalEventService.onSignal(this.signalCatchEvent.signalEventDefinition.name, signalReceivedCallback, true, this.processInstance.getProcessInstanceId(), this.triggerValue, this.signalCatchEvent.signalChannel);
});
}
validateSignal() {
if (this.signalCatchEvent.signalEventDefinition?.name === undefined) {
const errorToThrow = new processcube_engine_sdk_1.BadRequestError('Intermediate Signal Catch Event does not have a signal name assigned.', 'process');
errorToThrow.additionalInformation = {
flowNodeInstanceId: this.flowNodeInstanceId,
flowNodeId: this.signalCatchEvent.id,
flowNodeName: this.signalCatchEvent.name,
processInstanceId: this.processInstance.getProcessInstanceId(),
processModelId: this.processInstance.getProcessModelId(),
embeddedProcessModelId: this.processInstance.getEmbeddedProcessModelId(),
correlationId: this.processInstance.getCorrelationId(),
};
throw errorToThrow;
}
}
async parseTriggerValue() {
if (!this.signalCatchEvent.triggerValueInToken) {
return;
}
this.abortSignal.signal.throwIfAborted();
try {
this.triggerValue = await this.processInstance.executeRuntimeExpression({
expression: `\`${this.signalCatchEvent.triggerValueInToken}\``,
currentFlowNode: this.signalCatchEvent,
currentToken: this.processToken,
previousFlowNode: this.processInstance.getProcessModelFacade().getPreviousFlowNodesFor(this.flowNode)?.pop(),
allowNonObjectResults: true,
});
}
catch (error) {
const errorToThrow = new processcube_engine_sdk_1.BadRequestError(`SignalCatchEvent configuration for setTriggerValueInToken is invalid!`, error.category);
errorToThrow.additionalInformation = {
setTriggerValueInToken: this.signalCatchEvent.triggerValueInToken,
processInstanceId: this.processInstance.getProcessInstanceId(),
correlationId: this.processInstance.getCorrelationId(),
};
this.logger.error(errorToThrow.message);
throw errorToThrow;
}
}
};
exports.IntermediateSignalCatchEventHandler = IntermediateSignalCatchEventHandler;
exports.IntermediateSignalCatchEventHandler = IntermediateSignalCatchEventHandler = __decorate([
(0, inversify_1.injectable)(),
__metadata("design:paramtypes", [EventMiddlewareHandler_1.EventMiddlewareHandler,
FlowNodeHandlerFactory_1.FlowNodeHandlerFactory,
index_2.FlowNodeInstanceDatabaseAdapter, Object, ProcessInstance_1.ProcessInstance,
SignalEventService_1.SignalEventService])
], IntermediateSignalCatchEventHandler);
//# sourceMappingURL=IntermediateSignalCatchEventHandler.js.map