@5minds/processcube_engine
Version:
The ProcessCube Engine. Stores and executes BPMNs.
146 lines • 8.04 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.ExternalTaskServiceProxyConfiguration = exports.ExternalTaskService = void 0;
const async_lock_1 = __importDefault(require("async-lock"));
const dayjs_1 = __importDefault(require("dayjs"));
const inversify_1 = require("inversify");
const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk");
const IocRegistrations_1 = require("../../Contracts/IocRegistrations");
const index_1 = require("../../Core/index");
const Tools_1 = require("../../Tools");
const index_2 = require("../../Tools/DatabaseAdaptersSequelize/index");
const index_3 = require("../../Tools/Iam/index");
const asyncLocker = new async_lock_1.default({ maxPending: 100000 });
let ExternalTaskService = class ExternalTaskService {
externalTaskDatabaseAdapter;
iamService;
logger;
coreAccess;
processDefinitionMediator;
constructor(coreAccess, externalTaskDatabaseAdapter, iamService, processDefinitionMediator) {
this.coreAccess = coreAccess;
this.externalTaskDatabaseAdapter = externalTaskDatabaseAdapter;
this.iamService = iamService;
this.logger = new processcube_engine_sdk_1.Logger('api:external_task_service');
this.processDefinitionMediator = processDefinitionMediator;
}
async getAllDeployedTopics(identity) {
this.logger.trace('Getting all currently deployed External Task topics');
const processDefinitions = await this.processDefinitionMediator.getAll(identity, 0, 0);
const externalTaskTopics = processDefinitions
.flatMap((processDefinition) => processDefinition.getFlowNodesByType(processcube_engine_sdk_1.BpmnType.serviceTask))
.filter((serviceTask) => serviceTask.topic != undefined)
.map((serviceTask) => serviceTask.topic)
.filter((currentTopic, index, topicList) => topicList.indexOf(currentTopic) === index);
this.logger.info('Retrieved a list of available External Task topics', { topics: externalTaskTopics });
return externalTaskTopics;
}
async handleError(identity, workerId, externalTaskId, error) {
return asyncLocker.acquire(`${externalTaskId}-queue-update-request`, async () => {
this.logger.trace('Attempting to finish External Task with an Error', {
identity: identity,
workerId: workerId,
externalTaskId: externalTaskId,
externalTaskError: error,
});
await this.ensureExternalTaskCanBeAccessedByWorker(externalTaskId, workerId, identity);
const payload = {
error: this.createInternallyUsedExternalTaskError(error),
};
await this.coreAccess.finishExternalServiceTask(externalTaskId, payload);
this.logger.debug('Successfully finished External Task with an Error', {
identity: identity,
workerId: workerId,
externalTaskId: externalTaskId,
externalTaskError: error,
});
});
}
async finishExternalTask(identity, workerId, externalTaskId, payload) {
return asyncLocker.acquire(`${externalTaskId}-queue-update-request`, async () => {
this.logger.trace('Attempting to finish External Task', {
identity: identity,
workerId: workerId,
externalTaskId: externalTaskId,
result: payload,
});
await this.ensureExternalTaskCanBeAccessedByWorker(externalTaskId, workerId, identity);
const successNotificationPayload = {
result: payload,
};
await this.coreAccess.finishExternalServiceTask(externalTaskId, successNotificationPayload);
this.logger.debug('Successfully finished External Task', {
identity: identity,
workerId: workerId,
externalTaskId: externalTaskId,
});
});
}
async ensureExternalTaskCanBeAccessedByWorker(externalTaskId, workerId, identity) {
this.ensureHasClaim(identity, processcube_engine_sdk_1.claims.canAccessExternalTasks);
this.logger.trace(`Checking if worker can access the requested External Task.`, {
workerId: workerId,
externalTaskId: externalTaskId,
});
const externalTask = await this.externalTaskDatabaseAdapter.findByExternalTaskId(externalTaskId);
const externalTaskIsAlreadyFinished = externalTask.state === processcube_engine_sdk_1.ExternalTaskState.finished;
if (externalTaskIsAlreadyFinished) {
throw new processcube_engine_sdk_1.GoneError(`External Task with ID \`${externalTaskId}\` has been finished and is not accessible.`);
}
const now = (0, dayjs_1.default)();
const taskReleaseTime = (0, dayjs_1.default)(externalTask.lockExpirationTime);
const externalTaskIsLockedByOtherWorker = externalTask.workerId != undefined && externalTask.workerId !== workerId && now.isBefore(taskReleaseTime);
if (externalTaskIsLockedByOtherWorker) {
const msg = `External Task with ID \`${externalTaskId}\` is locked by another worker, until \`${taskReleaseTime.toISOString()}\`.`;
throw new processcube_engine_sdk_1.LockedError(msg);
}
}
createInternallyUsedExternalTaskError(error) {
return {
code: error.errorCode,
name: 'ExternalTaskWorkerError',
message: error.errorMessage,
errorDetails: error.errorDetails,
};
}
ensureHasClaim(identity, claimName) {
const isSuperAdmin = this.iamService.checkIfUserIsSuperAdmin(identity);
if (isSuperAdmin) {
return;
}
this.iamService.ensureHasClaim(identity, claimName);
}
};
exports.ExternalTaskService = ExternalTaskService;
exports.ExternalTaskService = ExternalTaskService = __decorate([
(0, inversify_1.injectable)(),
__param(0, (0, inversify_1.inject)(IocRegistrations_1.IocRegistrationKeys.core.services.CoreAccess)),
__param(1, (0, inversify_1.inject)(IocRegistrations_1.IocRegistrationKeys.internal.ExternalTaskDatabaseAdapter)),
__param(2, (0, inversify_1.inject)(IocRegistrations_1.IocRegistrationKeys.internal.IamService)),
__param(3, (0, inversify_1.inject)(IocRegistrations_1.IocRegistrationKeys.internal.ProcessDefinitionMediator)),
__metadata("design:paramtypes", [index_1.CoreAccessService,
index_2.ExternalTaskDatabaseAdapter,
index_3.IamService,
Tools_1.ProcessDefinitionMediator])
], ExternalTaskService);
exports.ExternalTaskServiceProxyConfiguration = {
proxyKey: IocRegistrations_1.IocRegistrationKeys.api.services.ExternalTaskServiceProxy,
targetKey: IocRegistrations_1.IocRegistrationKeys.api.services.ExternalTaskService,
target: ExternalTaskService,
};
//# sourceMappingURL=ExternalTaskService.js.map