From 047b1d13c0b1ef9600130253fcd97635597291c2 Mon Sep 17 00:00:00 2001 From: Danny Martini Date: Thu, 19 Sep 2024 11:01:35 +0200 Subject: [PATCH] `getExecution` can now return undefined if there is no active execution for that id --- packages/cli/src/active-executions.ts | 41 +++++++++++++++++++-------- packages/cli/src/workflow-runner.ts | 5 +++- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/packages/cli/src/active-executions.ts b/packages/cli/src/active-executions.ts index aed55c9c84..7025a477c6 100644 --- a/packages/cli/src/active-executions.ts +++ b/packages/cli/src/active-executions.ts @@ -127,14 +127,20 @@ export class ActiveExecutions { */ attachWorkflowExecution(executionId: string, workflowExecution: PCancelable) { - this.getExecution(executionId).workflowExecution = workflowExecution; + const execution = this.getExecution(executionId); + if (execution) { + execution.workflowExecution = workflowExecution; + } } attachResponsePromise( executionId: string, responsePromise: IDeferredPromise, ): void { - this.getExecution(executionId).responsePromise = responsePromise; + const execution = this.getExecution(executionId); + if (execution) { + execution.responsePromise = responsePromise; + } } resolveResponsePromise(executionId: string, response: IExecuteResponsePromiseData): void { @@ -145,23 +151,31 @@ export class ActiveExecutions { /** Cancel the execution promise and reject its post-execution promise. */ stopExecution(executionId: string): void { const execution = this.getExecution(executionId); - execution.workflowExecution?.cancel(); - execution.postExecutePromise.reject(new ExecutionCancelledError(executionId)); - this.logger.debug('Execution cancelled', { executionId }); + if (execution) { + execution.workflowExecution?.cancel(); + execution.postExecutePromise.reject(new ExecutionCancelledError(executionId)); + this.logger.debug('Execution cancelled', { executionId }); + } } /** Resolve the post-execution promise in an execution. */ finalizeExecution(executionId: string, fullRunData?: IRun) { const execution = this.getExecution(executionId); - execution.postExecutePromise.resolve(fullRunData); - this.logger.debug('Execution finalized', { executionId }); + if (execution) { + execution.postExecutePromise.resolve(fullRunData); + this.logger.debug('Execution finalized', { executionId }); + } } /** * Returns a promise which will resolve with the data of the execution with the given id */ async getPostExecutePromise(executionId: string): Promise { - return await this.getExecution(executionId).postExecutePromise.promise; + const execution = this.getExecution(executionId); + if (execution) { + return await execution.postExecutePromise.promise; + } + return undefined; } /** @@ -188,11 +202,14 @@ export class ActiveExecutions { } setStatus(executionId: string, status: ExecutionStatus) { - this.getExecution(executionId).status = status; + const execution = this.getExecution(executionId); + if (execution) { + execution.status = status; + } } - getStatus(executionId: string): ExecutionStatus { - return this.getExecution(executionId).status; + getStatus(executionId: string): ExecutionStatus | undefined { + return this.getExecution(executionId)?.status; } /** Wait for all active executions to finish */ @@ -224,7 +241,7 @@ export class ActiveExecutions { } } - private getExecution(executionId: string): IExecutingWorkflowData { + private getExecution(executionId: string): IExecutingWorkflowData | undefined { const execution = this.activeExecutions[executionId]; if (!execution) { ErrorReporterProxy.error(new ExecutionNotFoundError(executionId)); diff --git a/packages/cli/src/workflow-runner.ts b/packages/cli/src/workflow-runner.ts index a4dd344b62..602304ff63 100644 --- a/packages/cli/src/workflow-runner.ts +++ b/packages/cli/src/workflow-runner.ts @@ -334,7 +334,10 @@ export class WorkflowRunner { if (workflowExecution.isCanceled) { fullRunData.finished = false; } - fullRunData.status = this.activeExecutions.getStatus(executionId); + const status = this.activeExecutions.getStatus(executionId); + if (status) { + fullRunData.status = status; + } this.activeExecutions.finalizeExecution(executionId, fullRunData); }) .catch(