UNPKG

@villedemontreal/workit-stepfunction-client

Version:

Camunda BPM client for WorkIt that works with Camunda platform powered by TypeScript

160 lines 7.05 kB
"use strict"; /* * Copyright (c) 2025 Ville de Montreal. All rights reserved. * Licensed under the MIT license. * See LICENSE file in the project root for full license information. */ 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); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SFnSQSClient = void 0; const workit_core_1 = require("@villedemontreal/workit-core"); const identifiers_1 = require("./config/constants/identifiers"); const sfnMessage_1 = require("./sfnMessage"); const client_sqs_1 = require("@aws-sdk/client-sqs"); const sqsConsumer = require("sqs-consumer"); const crypto_1 = require("crypto"); const inversify_1 = require("inversify"); const stepFunctionRepository_1 = require("./repositories/stepFunctionRepository"); let SFnSQSClient = class SFnSQSClient { constructor(config, repo) { this._config = config; this._repo = repo || new stepFunctionRepository_1.StepFunctionRepository(this._config); const pluginLoader = new workit_core_1.PluginLoader(workit_core_1.IoC, this._getLogger()); if (config.plugins) { pluginLoader.load(config.plugins); } } subscribe(onMessageReceived) { // check if we have a specialized sqs config let config; if (workit_core_1.IoC.isServiceBound(identifiers_1.SERVICE_IDENTIFIER.sqs_config)) { config = workit_core_1.IoC.get(identifiers_1.SERVICE_IDENTIFIER.sqs_config); } else { config = this._config; } this._topicSubscription = sqsConsumer.Consumer.create({ queueUrl: this._config.queueUrl, alwaysAcknowledge: this._config.alwaysAcknowledge === undefined ? true : this._config.alwaysAcknowledge, waitTimeSeconds: this._config.waitTimeSeconds, handleMessage: async (message) => { const [msg, service] = sfnMessage_1.SfnMessage.wrap(message, this._repo); try { await onMessageReceived(msg, service); } catch (error) { this._getLogger().error("please check your code, this error shouldn't be catched here", error); } return message; }, sqs: new client_sqs_1.SQSClient(config), }); this._startSubscriber(); return Promise.resolve(); } unsubscribe() { try { if (this._topicSubscription) { this._topicSubscription.stop(); } return Promise.resolve(); } catch (error) { return Promise.reject(error); } } async deployWorkflow(absPath, override) { // TODO: specialize any // eslint-disable-next-line @typescript-eslint/no-unsafe-argument const result = await this._repo.deployWorkflow(absPath, override); if (!result.stateMachineArn) { throw new Error('Cannot deploy workflow'); } const workflows = [ { bpmnProcessId: result.stateMachineArn, workflowKey: result.stateMachineArn, resourceName: result.stateMachineArn, version: 1, }, ]; return { workflows, key: result.stateMachineArn, }; } async getWorkflows(options) { // use https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/sfn/command/ListStateMachinesCommand/ return Promise.reject(new Error('Not implemented exception')); } async getWorkflow(payload) { // https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/sfn/command/DescribeStateMachineCommand/ return Promise.reject(new Error('Not implemented exception')); } async updateVariables(model) { return Promise.reject(new Error('Not implemented exception ! Step function cannot update variable outside the pipeline')); } async updateJobRetries({ jobKey, retries }) { return Promise.reject(new Error('Not implemented exception ! This is only usefull for Camunda BPM/Zeebe, Step function handles everything on his side')); } publishMessage(payload) { // could be used but not with sqs service (perhaps with Amazon EventBridge or MQ) return Promise.reject(new Error('Not implemented exception, please use createWorkflowInstance method')); } async createWorkflowInstance(model) { const input = { stateMachineArn: model.bpmnProcessId, name: (0, crypto_1.randomUUID)(), input: JSON.stringify(model.variables), }; const response = await this._repo.startExecution(input); if (!response.executionArn) { throw new Error('Cannot create workflow instance'); } return { bpmnProcessId: model.bpmnProcessId, version: 1, workflowInstanceKey: response.executionArn, workflowKey: model.bpmnProcessId, }; } cancelWorkflowInstance(instanceId) { // use https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/sfn/command/StopExecutionCommand/ return Promise.reject(new Error('Not implemented exception')); } resolveIncident(incidentKey) { // perhaps we could use redrive command return Promise.reject(new Error('Not implemented exception')); } _startSubscriber() { this._topicSubscription.start(); } _getLogger() { try { return workit_core_1.IoC.get(workit_core_1.SERVICE_IDENTIFIER.logger); } catch (error) { return workit_core_1.NOOP_LOGGER; } } }; SFnSQSClient = __decorate([ (0, inversify_1.injectable)(), __param(0, (0, inversify_1.inject)(identifiers_1.SERVICE_IDENTIFIER.stepfunction_config)), __param(1, (0, inversify_1.inject)(identifiers_1.SERVICE_IDENTIFIER.stepfunction_repository)), __param(1, (0, inversify_1.optional)()), __metadata("design:paramtypes", [Object, stepFunctionRepository_1.StepFunctionRepository]) ], SFnSQSClient); exports.SFnSQSClient = SFnSQSClient; //# sourceMappingURL=sfnSQSClient.js.map