UNPKG

nestjs-temporal-core

Version:

Complete NestJS integration for Temporal.io with auto-discovery, declarative scheduling, enhanced monitoring, and enterprise-ready features

187 lines 7.64 kB
"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); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var TemporalClientService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.TemporalClientService = void 0; const common_1 = require("@nestjs/common"); const constants_1 = require("../constants"); const logger_1 = require("../utils/logger"); let TemporalClientService = TemporalClientService_1 = class TemporalClientService { constructor(client, options) { this.client = client; this.options = options; this.workflowClient = this.client?.workflow || null; this.logger = (0, logger_1.createLogger)(TemporalClientService_1.name); } async onModuleInit() { if (!this.client) { this.logger.warn('Temporal client not initialized - some features may be unavailable'); } else { this.logger.log('Temporal client initialized successfully'); } } async startWorkflow(workflowType, args, options) { this.ensureClientInitialized(); const { taskQueue, workflowId = this.generateWorkflowId(workflowType), signal, ...restOptions } = options; try { const startOptions = { taskQueue, workflowId, ...restOptions, }; const handle = await this.workflowClient.start(workflowType, { taskQueue: startOptions.taskQueue, workflowId: startOptions.workflowId, args, ...restOptions, }); if (signal) { await handle.signal(signal.name, ...(signal.args || [])); } this.logger.debug(`Started workflow: ${workflowType} (${workflowId})`); return { result: handle.result(), workflowId: handle.workflowId, firstExecutionRunId: handle.firstExecutionRunId, handle, }; } catch (error) { const errorMsg = `Failed to start workflow '${workflowType}': ${error.message}`; this.logger.error(errorMsg); throw new Error(errorMsg); } } async signalWorkflow(workflowId, signalName, args = []) { this.ensureClientInitialized(); try { const handle = await this.workflowClient.getHandle(workflowId); await handle.signal(signalName, ...args); this.logger.debug(`Sent signal '${signalName}' to workflow ${workflowId}`); } catch (error) { const errorMsg = `Failed to send signal '${signalName}' to workflow ${workflowId}: ${error.message}`; this.logger.error(errorMsg); throw new Error(errorMsg); } } async queryWorkflow(workflowId, queryName, args = []) { this.ensureClientInitialized(); try { const handle = await this.workflowClient.getHandle(workflowId); const result = await handle.query(queryName, ...args); this.logger.debug(`Queried '${queryName}' on workflow ${workflowId}`); return result; } catch (error) { const errorMsg = `Failed to query '${queryName}' on workflow ${workflowId}: ${error.message}`; this.logger.error(errorMsg); throw new Error(errorMsg); } } async terminateWorkflow(workflowId, reason) { this.ensureClientInitialized(); try { const handle = await this.workflowClient.getHandle(workflowId); await handle.terminate(reason); this.logger.log(`Terminated workflow ${workflowId}${reason ? `: ${reason}` : ''}`); } catch (error) { const errorMsg = `Failed to terminate workflow ${workflowId}: ${error.message}`; this.logger.error(errorMsg); throw new Error(errorMsg); } } async cancelWorkflow(workflowId) { this.ensureClientInitialized(); try { const handle = await this.workflowClient.getHandle(workflowId); await handle.cancel(); this.logger.log(`Cancelled workflow ${workflowId}`); } catch (error) { const errorMsg = `Failed to cancel workflow ${workflowId}: ${error.message}`; this.logger.error(errorMsg); throw new Error(errorMsg); } } async getWorkflowHandle(workflowId, runId) { this.ensureClientInitialized(); try { return await this.workflowClient.getHandle(workflowId, runId); } catch (error) { const errorMsg = `Failed to get workflow handle for ${workflowId}: ${error.message}`; this.logger.error(errorMsg); throw new Error(errorMsg); } } async describeWorkflow(workflowId, runId) { this.ensureClientInitialized(); try { const handle = await this.workflowClient.getHandle(workflowId, runId); return await handle.describe(); } catch (error) { const errorMsg = `Failed to describe workflow ${workflowId}: ${error.message}`; this.logger.error(errorMsg); throw new Error(errorMsg); } } listWorkflows(query, pageSize = 100) { this.ensureClientInitialized(); try { return this.workflowClient.list({ query, pageSize }); } catch (error) { const errorMsg = `Failed to list workflows with query '${query}': ${error.message}`; this.logger.error(errorMsg); throw new Error(errorMsg); } } getWorkflowClient() { return this.workflowClient; } getRawClient() { return this.client; } isHealthy() { return Boolean(this.client && this.workflowClient); } getStatus() { return { available: Boolean(this.client), healthy: this.isHealthy(), }; } ensureClientInitialized() { if (!this.workflowClient) { throw new Error(constants_1.ERRORS.CLIENT_NOT_INITIALIZED); } } generateWorkflowId(workflowType) { const timestamp = Date.now(); const random = Math.random().toString(36).slice(2); return `${workflowType}-${timestamp}-${random}`; } }; exports.TemporalClientService = TemporalClientService; exports.TemporalClientService = TemporalClientService = TemporalClientService_1 = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(constants_1.TEMPORAL_CLIENT)), __param(1, (0, common_1.Inject)(constants_1.TEMPORAL_MODULE_OPTIONS)), __metadata("design:paramtypes", [Object, Object]) ], TemporalClientService); //# sourceMappingURL=temporal-client.service.js.map