@5minds/processcube_engine
Version:
The ProcessCube Engine. Stores and executes BPMNs.
193 lines • 10.8 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.MessageBoundaryEventHandler = void 0;
const inversify_1 = require("inversify");
const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk");
const Contracts_1 = require("../../../Contracts");
const index_1 = require("../../../Tools/DatabaseAdaptersSequelize/index");
const EventMiddlewareHandler_1 = require("../../../Tools/EventMiddlewareHandler");
const MessageEventService_1 = require("../../MessageEventService");
const ProcessInstance_1 = require("../../ProcessInstance");
const FlowNodeHandlerFactory_1 = require("../FlowNodeHandlerFactory");
const BoundaryEventHandler_1 = require("./BoundaryEventHandler");
let MessageBoundaryEventHandler = class MessageBoundaryEventHandler extends BoundaryEventHandler_1.BoundaryEventHandler {
messageEventService;
onMessageSubscriptionId;
acknowledgeEventReceivedId;
triggerValue;
constructor(eventMiddlewareHandler, flowNodeInstanceDatabaseAdapter, flowNodeHandlerFactory, boundaryEventModel, processInstance, processTokenOfAttachedFlowNode, messageEventService) {
super(eventMiddlewareHandler, flowNodeInstanceDatabaseAdapter, flowNodeHandlerFactory, boundaryEventModel, processInstance, processTokenOfAttachedFlowNode, 'message_boundary_event_handler');
this.messageEventService = messageEventService;
}
async execute(onTriggeredCallback, attachedFlowNodeInstanceId, decoratedFlowNodeRejectFunc) {
try {
this.logger.debug(`Initializing Message BoundaryEvent on FlowNodeInstance ${attachedFlowNodeInstanceId}`);
await this.parseTriggerValue();
this.validateMessage();
const typeSpecificData = {
type: Contracts_1.FlowNodeInstanceDataTypes.catchEvent,
eventName: this.boundaryEventModel.messageEventDefinition?.name,
triggerValue: this.triggerValue,
};
await this.initialize(attachedFlowNodeInstanceId, undefined, typeSpecificData);
await this.waitForMessage(onTriggeredCallback, this.boundaryEventModel.messageEventDefinition.name);
if (this.boundaryEventModel.cancelActivity) {
return this.handleNextFlowNodes();
}
}
catch (error) {
if (this.executionPromise?.controller.signal.aborted) {
return;
}
// persistOnError would fail, if the Boundary>Event Instance was not previously persisted.
// This can happen, if running the pre-script fails.
// Running "persistOnEnter" again is actually safe, because a repeated call doesn't do anything.
await this.persistOnEnter();
await this.persistOnError(error);
decoratedFlowNodeRejectFunc(error);
return;
}
}
async resume(boundaryEventInstance, onTriggeredCallback, attachedFlowNodeInstanceId, decoratedFlowNodeRejectFunc, flowNodeInstances) {
this.boundaryEventInstance = boundaryEventInstance;
this.triggerValue = boundaryEventInstance.triggerValue;
this.eventWasTriggered = this.boundaryEventWasTriggered(flowNodeInstances);
if (this.eventWasTriggered && this.boundaryEventModel.cancelActivity) {
return this.handleNextFlowNodes(flowNodeInstances);
}
try {
await this.initialize(attachedFlowNodeInstanceId, boundaryEventInstance);
if (this.boundaryEventInstance?.state === processcube_engine_sdk_1.FlowNodeInstanceState.canceled) {
return;
}
let resumingPromises = [];
const executionPromise = this.waitForMessage(onTriggeredCallback, this.boundaryEventModel.messageEventDefinition.name);
resumingPromises.push(executionPromise);
if (this.eventWasTriggered) {
resumingPromises.push(this.handleNextFlowNodes(flowNodeInstances));
}
await Promise.all(resumingPromises);
}
catch (error) {
if (this.executionPromise.controller.signal.aborted) {
return;
}
await this.persistOnError(error);
decoratedFlowNodeRejectFunc(error);
return;
}
if (this.boundaryEventModel.cancelActivity) {
return this.handleNextFlowNodes();
}
}
async finish() {
await super.finish();
this.messageEventService.acknowledge(this.acknowledgeEventReceivedId);
this.messageEventService.unsubscribe(this.onMessageSubscriptionId);
}
async cancel() {
this.messageEventService.acknowledge(this.acknowledgeEventReceivedId);
this.messageEventService.unsubscribe(this.onMessageSubscriptionId);
if (!this.boundaryEventModel.cancelActivity && this.eventWasTriggered) {
this.executionFinishCallback?.();
}
await super.cancel();
}
validateMessage() {
if (this.boundaryEventModel.messageEventDefinition?.name === undefined) {
const errorToThrow = new processcube_engine_sdk_1.BadRequestError('Message Boundary Event does not have a message name assigned.', 'process');
errorToThrow.additionalInformation = {
flowNodeInstanceId: this.boundaryEventInstanceId,
flowNodeId: this.boundaryEventModel.id,
flowNodeName: this.boundaryEventModel.name,
processInstanceId: this.processInstance.getProcessInstanceId(),
processModelId: this.processInstance.getProcessModelId(),
embeddedProcessModelId: this.processInstance.getEmbeddedProcessModelId(),
correlationId: this.processInstance.getCorrelationId(),
};
throw errorToThrow;
}
}
waitForMessage(onTriggeredCallback, messageName) {
this.logger.debug(`Waiting for a "${messageName}" Message`);
this.createExecutionPromise(async (resolve) => {
if (!this.boundaryEventModel.cancelActivity) {
this.executionFinishCallback = resolve;
}
const messageReceivedCallback = async (message) => {
this.logger.debug(`MessageBoundaryEvent instance ${this.boundaryEventInstanceId} received message:`, {
receivedMessage: message,
});
this.acknowledgeEventReceivedId = message.eventId;
this.eventWasTriggered = true;
this.logger.debug(`MessageBoundaryEvent triggered`);
const nextFlowNode = this.getNextFlowNode();
this.processToken = message?.currentToken ?? {};
await this.runPostScript();
const eventData = {
boundaryInstanceId: this.boundaryEventInstanceId,
nextFlowNode: nextFlowNode,
interruptHandler: this.boundaryEventModel.cancelActivity,
eventPayload: this.processToken,
canTriggerMultipleTimes: !this.boundaryEventModel.cancelActivity,
};
this.triggeredByFlowNodeInstanceId = message.flowNodeInstanceId;
await this.saveTokenInDataObjects(eventData.eventPayload, this.boundaryEventModel.id);
this.sendBoundaryEventTriggeredNotification(messageName);
onTriggeredCallback(eventData);
// An interrupting BoundaryEvent can only be triggered once.
// A non-interrupting BoundaryEvent can be triggerred repeatedly.
if (this.boundaryEventModel.cancelActivity) {
resolve();
}
else {
this.messageEventService.acknowledge(this.acknowledgeEventReceivedId);
this.handleNextFlowNodes();
}
};
this.onMessageSubscriptionId = this.messageEventService.onMessage(messageName, messageReceivedCallback, this.boundaryEventModel.cancelActivity, this.processInstance.getProcessInstanceId(), this.triggerValue, this.boundaryEventModel.messageChannel);
});
return this.executionPromise.promise;
}
async parseTriggerValue() {
if (!this.boundaryEventModel.triggerValueInToken) {
return;
}
try {
this.triggerValue = await this.processInstance.executeRuntimeExpression({
expression: `\`${this.boundaryEventModel.triggerValueInToken}\``,
currentFlowNode: this.boundaryEventModel,
currentToken: this.processToken,
previousFlowNode: this.processInstance.getProcessModelFacade().getPreviousFlowNodesFor(this.boundaryEventModel)?.pop(),
allowNonObjectResults: true,
});
}
catch (error) {
const errorToThrow = new processcube_engine_sdk_1.BadRequestError(`MessageBoundaryEvent configuration for setTriggerValueInToken is invalid!`, error.category);
errorToThrow.additionalInformation = {
setTriggerValueInToken: this.boundaryEventModel.triggerValueInToken,
processInstanceId: this.processInstance.getProcessInstanceId(),
correlationId: this.processInstance.getCorrelationId(),
};
this.logger.error(errorToThrow.message);
throw errorToThrow;
}
}
};
exports.MessageBoundaryEventHandler = MessageBoundaryEventHandler;
exports.MessageBoundaryEventHandler = MessageBoundaryEventHandler = __decorate([
(0, inversify_1.injectable)(),
__metadata("design:paramtypes", [EventMiddlewareHandler_1.EventMiddlewareHandler,
index_1.FlowNodeInstanceDatabaseAdapter,
FlowNodeHandlerFactory_1.FlowNodeHandlerFactory, Object, ProcessInstance_1.ProcessInstance, Object, MessageEventService_1.MessageEventService])
], MessageBoundaryEventHandler);
//# sourceMappingURL=MessageBoundaryEventHandler.js.map