@@ -18,11 +18,13 @@ import * as Db from '@/Db';
1818import * as ResponseHelper from '@/ResponseHelper' ;
1919import type {
2020 IExecutionFlattedDb ,
21+ IExecutionResponse ,
2122 IExecutionsStopData ,
2223 IWorkflowExecutionDataProcess ,
2324} from '@/Interfaces' ;
2425import { WorkflowRunner } from '@/WorkflowRunner' ;
2526import { getWorkflowOwner } from '@/UserManagement/UserManagementHelper' ;
27+ import { recoverExecutionDataFromEventLogMessages } from './eventbus/MessageEventBus/recoverEvents' ;
2628
2729@Service ( )
2830export class WaitTracker {
@@ -106,12 +108,29 @@ export class WaitTracker {
106108 // Also check in database
107109 const execution = await Db . collections . Execution . findOneBy ( { id : executionId } ) ;
108110
109- if ( execution === null || ! execution . waitTill ) {
111+ if ( execution === null ) {
110112 throw new Error ( `The execution ID "${ executionId } " could not be found.` ) ;
111113 }
112114
113- const fullExecutionData = ResponseHelper . unflattenExecutionData ( execution ) ;
114-
115+ if ( ! [ 'new' , 'unknown' , 'waiting' , 'running' ] . includes ( execution . status ) ) {
116+ throw new Error (
117+ `Only running or waiting executions can be stopped and ${ executionId } is currently ${ execution . status } .` ,
118+ ) ;
119+ }
120+ let fullExecutionData : IExecutionResponse ;
121+ try {
122+ fullExecutionData = ResponseHelper . unflattenExecutionData ( execution ) ;
123+ } catch ( error ) {
124+ // if the execution ended in an unforseen, non-cancelable state, try to recover it
125+ await recoverExecutionDataFromEventLogMessages ( executionId , [ ] , true ) ;
126+ // find recovered data
127+ const recoveredExecution = await Db . collections . Execution . findOneBy ( { id : executionId } ) ;
128+ if ( recoveredExecution ) {
129+ fullExecutionData = ResponseHelper . unflattenExecutionData ( recoveredExecution ) ;
130+ } else {
131+ throw new Error ( `Execution ${ executionId } could not be recovered or canceled.` ) ;
132+ }
133+ }
115134 // Set in execution in DB as failed and remove waitTill time
116135 const error = new WorkflowOperationError ( 'Workflow-Execution has been canceled!' ) ;
117136
0 commit comments