@5minds/processcube_engine
Version:
The ProcessCube Engine. Stores and executes BPMNs.
180 lines • 10.7 kB
JavaScript
;
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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoreAccessService = void 0;
const inversify_1 = require("inversify");
const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk");
const index_1 = require("../Contracts/InternalMessages/index");
const index_2 = require("../Contracts/index");
const EventAggregator_1 = __importDefault(require("../Tools/EventAggregator"));
const ProcessInstanceStore_1 = __importDefault(require("./Facades/ProcessInstanceStore"));
/**
* Gatekeeper Service for Core Access.
*/
let CoreAccessService = class CoreAccessService {
executeProcessService;
flowNodeInstanceDatabaseAdapter;
messageEventService;
processInstanceDatabaseAdapter;
signalEventService;
logger;
constructor(executeProcessService, flowNodeInstanceDatabaseAdapter, messageEventService, processInstanceDatabaseAdapter, signalEventService) {
this.executeProcessService = executeProcessService;
this.flowNodeInstanceDatabaseAdapter = flowNodeInstanceDatabaseAdapter;
this.messageEventService = messageEventService;
this.processInstanceDatabaseAdapter = processInstanceDatabaseAdapter;
this.signalEventService = signalEventService;
this.logger = new processcube_engine_sdk_1.Logger('core_access_service');
}
async transferOwnership(processInstanceId, newOwner) {
await new Promise((resolve) => {
const transferOwnershipMessage = index_1.eventAggregatorSettings.messagePaths.changeProcessInstanceOwner.replace(index_1.eventAggregatorSettings.messageParams.processInstanceId, processInstanceId);
EventAggregator_1.default.publish(transferOwnershipMessage, { newOwner: newOwner }, resolve);
});
}
async finishExternalServiceTask(externalTaskId, externalTaskResult) {
await new Promise((resolve) => {
const externalTaskFinishedEventName = index_1.eventAggregatorSettings.messagePaths.externalTaskFinished.replace(index_1.eventAggregatorSettings.messageParams.externalTaskId, externalTaskId);
EventAggregator_1.default.publish(externalTaskFinishedEventName, externalTaskResult, resolve);
});
}
async finishUntypedTask(identity, taskInstanceId) {
return new Promise((resolve) => {
const finishUntypedTaskMesage = {
flowNodeInstanceId: taskInstanceId,
processInstanceOwner: identity,
};
const finishUntypedTaskEvent = index_1.eventAggregatorSettings.messagePaths.finishUntypedTask.replace(index_1.eventAggregatorSettings.messageParams.flowNodeInstanceId, taskInstanceId);
EventAggregator_1.default.publish(finishUntypedTaskEvent, finishUntypedTaskMesage, resolve);
});
}
async finishManualTask(identity, manualTaskInstanceId) {
return new Promise((resolve) => {
const finishManualTaskMessage = {
finishedBy: identity,
flowNodeInstanceId: manualTaskInstanceId,
processInstanceOwner: identity,
currentToken: {},
};
const finishManualTaskEvent = index_1.eventAggregatorSettings.messagePaths.finishManualTask.replace(index_1.eventAggregatorSettings.messageParams.flowNodeInstanceId, manualTaskInstanceId);
EventAggregator_1.default.publish(finishManualTaskEvent, finishManualTaskMessage, resolve);
});
}
async finishUserTask(identity, userTaskInstanceId, userTaskResult) {
return new Promise((resolve) => {
const finishUserTaskMessage = {
userTaskResult: userTaskResult,
finishedBy: identity,
flowNodeInstanceId: userTaskInstanceId,
};
const finishUserTaskEvent = index_1.eventAggregatorSettings.messagePaths.finishUserTask.replace(index_1.eventAggregatorSettings.messageParams.flowNodeInstanceId, userTaskInstanceId);
EventAggregator_1.default.publish(finishUserTaskEvent, finishUserTaskMessage, resolve);
});
}
async triggerMessageEvent(identity, messageName, options) {
this.messageEventService.throwMessage({
messageReference: messageName,
customCorrelationId: options?.customCorrelationId ?? options?.payload?.customCorrelationId,
correlationId: options?.payload?.correlationId,
currentToken: options?.payload,
identity: identity,
targetProcessInstanceId: options?.processInstanceId,
triggerValue: options?.triggerValue,
messageChannel: options?.messageChannel,
});
}
async triggerSignalEvent(identity, signalName, options) {
this.signalEventService.throwSignal({
signalReference: signalName,
customCorrelationId: options?.customCorrelationId ?? options?.payload?.customCorrelationId,
correlationId: options?.payload?.correlationId,
currentToken: options?.payload,
identity: identity,
targetProcessInstanceId: options?.processInstanceId,
triggerValue: options?.triggerValue,
signalChannel: options?.signalChannel,
});
}
async retryProcessInstance(identity, processInstanceId, processDefinitionHash, flowNodeInstanceId, newStartToken) {
await this.executeProcessService.retryProcessInstance(identity, processInstanceId, flowNodeInstanceId, newStartToken, processDefinitionHash);
}
async killProcessInstance(identity, processInstanceId) {
if (ProcessInstanceStore_1.default.isRegistered(processInstanceId)) {
ProcessInstanceStore_1.default.kill(identity, processInstanceId);
return;
}
const processInstance = (await this.processInstanceDatabaseAdapter.getProcessInstances([processInstanceId]))[0];
this.logger.warn(`Cannot terminate Process Instance ${processInstanceId} regularly, because it is no longer actively handled by the Engine. Termination will be done manually.\n
Note that this will NOT affect the entire process tree but only this specific instance.`, {
processInstanceId: processInstanceId,
});
await this.killProcessInstanceManually(identity, processInstance);
}
/**
* CAUTION:
* This is only for those rare cases, when the engine stops processing a ProcessInstance and therefore no longer holds it in active memory.
* Then, and ONLY then, we must handle process instance termination manually. Consider this an emergency circuit breaker.
* Killing a process should only ever be done through the Process Instance class.
*/
async killProcessInstanceManually(identity, processInstance) {
if (processInstance.state !== processcube_engine_sdk_1.ProcessInstanceState.running) {
this.logger.info(`Process Instance ${processInstance.processInstanceId} is already finished. Nothing to do here.`);
return;
}
const terminationError = new processcube_engine_sdk_1.GoneError(`Process was killed by user \`${identity.userId}\`.`);
terminationError.additionalInformation = {
processInstanceWasTerminated: true,
killedBy: identity,
};
try {
await this.processInstanceDatabaseAdapter.terminate(processInstance.processInstanceId, terminationError, undefined, true);
this.sendProcessTerminatedNotification(processInstance);
}
catch (error) { }
}
async sendProcessTerminatedNotification(processInstance) {
const processInstanceTerminatedNotification = index_1.eventAggregatorSettings.messagePaths.processInstanceWithIdKilled.replace(index_1.eventAggregatorSettings.messageParams.processInstanceId, processInstance.processInstanceId);
const processTerminatedMessage = {
correlationId: processInstance.correlationId,
processDefinitionId: processInstance.processDefinitionId,
processModelId: processInstance.processModelId,
embeddedProcessModelId: processInstance.embeddedProcessModelId,
processModelName: processInstance.processModelName,
processInstanceId: processInstance.processInstanceId,
parentProcessInstanceId: processInstance.processInstanceId,
processInstanceOwner: {
userId: processInstance.ownerId,
},
currentToken: processInstance.endToken ?? processInstance.startToken,
};
// Instance specific Event
EventAggregator_1.default.publish(processInstanceTerminatedNotification, processTerminatedMessage);
// Generic event
EventAggregator_1.default.publish(index_1.eventAggregatorSettings.messagePaths.processTerminated, processTerminatedMessage);
}
};
exports.CoreAccessService = CoreAccessService;
exports.CoreAccessService = CoreAccessService = __decorate([
(0, inversify_1.injectable)(),
__param(0, (0, inversify_1.inject)(index_2.IocRegistrationKeys.core.services.ExecuteProcessService)),
__param(1, (0, inversify_1.inject)(index_2.IocRegistrationKeys.internal.FlowNodeInstanceDatabaseAdapter)),
__param(2, (0, inversify_1.inject)(index_2.IocRegistrationKeys.core.services.MessageEventService)),
__param(3, (0, inversify_1.inject)(index_2.IocRegistrationKeys.internal.ProcessInstanceDatabaseAdapter)),
__param(4, (0, inversify_1.inject)(index_2.IocRegistrationKeys.core.services.SignalEventService)),
__metadata("design:paramtypes", [Function, Function, Function, Function, Function])
], CoreAccessService);
//# sourceMappingURL=CoreAccessService.js.map