@5minds/processcube_engine
Version:
The ProcessCube Engine. Stores and executes BPMNs.
140 lines • 7.84 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.EscalationBoundaryEventHandler = 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 EscalationEventService_1 = require("../../EscalationEventService");
const ProcessInstance_1 = require("../../ProcessInstance");
const FlowNodeHandlerFactory_1 = require("../FlowNodeHandlerFactory");
const BoundaryEventHandler_1 = require("./BoundaryEventHandler");
let EscalationBoundaryEventHandler = class EscalationBoundaryEventHandler extends BoundaryEventHandler_1.BoundaryEventHandler {
escalationEventService;
onEscalationSubscriptionId;
constructor(eventMiddlewareHandler, flowNodeInstanceDatabaseAdapter, flowNodeHandlerFactory, boundaryEventModel, processInstance, processTokenOfAttachedFlowNode, escalationEventService) {
super(eventMiddlewareHandler, flowNodeInstanceDatabaseAdapter, flowNodeHandlerFactory, boundaryEventModel, processInstance, processTokenOfAttachedFlowNode, 'escalation_boundary_event_handler');
this.escalationEventService = escalationEventService;
}
async execute(onTriggeredCallback, attachedFlowNodeInstanceId, decoratedFlowNodeRejectFunc) {
try {
this.logger.debug(`Initializing Escalation BoundaryEvent on FlowNodeInstance ${attachedFlowNodeInstanceId}`);
const typeSpecificData = {
type: Contracts_1.FlowNodeInstanceDataTypes.catchEvent,
eventName: this.boundaryEventModel.escalationEventDefinition?.escalationCode ?? '',
};
await this.initialize(attachedFlowNodeInstanceId, undefined, typeSpecificData);
await this.waitForEscalation(onTriggeredCallback, this.boundaryEventModel.escalationEventDefinition?.escalationCode ?? '');
if (this.boundaryEventModel.cancelActivity) {
return this.handleNextFlowNodes();
}
}
catch (error) {
if (this.executionPromise.controller.signal.aborted) {
return;
}
await this.persistOnEnter();
await this.persistOnError(error);
decoratedFlowNodeRejectFunc(error);
return;
}
}
async resume(boundaryEventInstance, onTriggeredCallback, attachedFlowNodeInstanceId, decoratedFlowNodeRejectFunc, flowNodeInstances) {
this.boundaryEventInstance = boundaryEventInstance;
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.waitForEscalation(onTriggeredCallback, this.boundaryEventModel.escalationEventDefinition?.escalationCode ?? '');
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();
}
async cancel() {
this.escalationEventService.unsubscribe(this.onEscalationSubscriptionId);
if (!this.boundaryEventModel.cancelActivity && this.eventWasTriggered) {
this.executionFinishCallback?.();
}
await super.cancel();
}
waitForEscalation(onTriggeredCallback, code) {
this.logger.debug(`Waiting for a "${code}" Escalation`);
this.createExecutionPromise(async (resolve) => {
if (!this.boundaryEventModel.cancelActivity) {
this.executionFinishCallback = resolve;
}
const escalationReceivedCallback = async (escalation) => {
this.logger.debug(`Escalation BoundaryEvent instance ${this.boundaryEventInstanceId} received escalation:`, {
receivedEscalation: escalation,
});
this.eventWasTriggered = true;
this.logger.debug(`EscalationBoundaryEvent triggered`);
const nextFlowNode = this.getNextFlowNode();
this.processToken = escalation?.currentToken ?? {};
await this.runPostScript();
const eventData = {
boundaryInstanceId: this.boundaryEventInstanceId,
nextFlowNode: nextFlowNode,
interruptHandler: this.boundaryEventModel.cancelActivity,
eventPayload: this.processToken,
canTriggerMultipleTimes: !this.boundaryEventModel.cancelActivity,
};
this.triggeredByFlowNodeInstanceId = escalation.flowNodeInstanceId;
await this.saveTokenInDataObjects(eventData.eventPayload, this.boundaryEventModel.id);
this.sendBoundaryEventTriggeredNotification(code);
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.handleNextFlowNodes();
}
};
this.onEscalationSubscriptionId = this.escalationEventService.onEscalation(code, this.processInstance, escalationReceivedCallback, this.boundaryEventModel.cancelActivity);
});
return this.executionPromise.promise;
}
};
exports.EscalationBoundaryEventHandler = EscalationBoundaryEventHandler;
exports.EscalationBoundaryEventHandler = EscalationBoundaryEventHandler = __decorate([
(0, inversify_1.injectable)(),
__metadata("design:paramtypes", [EventMiddlewareHandler_1.EventMiddlewareHandler,
index_1.FlowNodeInstanceDatabaseAdapter,
FlowNodeHandlerFactory_1.FlowNodeHandlerFactory, Object, ProcessInstance_1.ProcessInstance, Object, EscalationEventService_1.EscalationEventService])
], EscalationBoundaryEventHandler);
//# sourceMappingURL=EscalationBoundaryEventHandler.js.map