Skip to content

Commit

Permalink
handle cancel correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Nov 24, 2023
1 parent 4b9a419 commit b7dfa0d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
5 changes: 3 additions & 2 deletions packages/cli/src/ActiveExecutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ export class ActiveExecutions {
return;
}

const postExecutePromise = this.getPostExecutePromise(executionId);

// In case something goes wrong make sure that promise gets first
// returned that it gets then also resolved correctly.
if (this.activeExecutions[executionId].process !== undefined) {
Expand All @@ -177,10 +179,9 @@ export class ActiveExecutions {
} else {
// Workflow is running in current process
this.activeExecutions[executionId].workflowExecution!.cancel();
setTimeout(() => this.remove(executionId), 10);
}

return this.getPostExecutePromise(executionId);
return postExecutePromise;
}

/**
Expand Down
22 changes: 7 additions & 15 deletions packages/core/src/WorkflowExecute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,21 +820,16 @@ export class WorkflowExecute {
let closeFunction: Promise<void> | undefined;

return new PCancelable(async (resolve, reject, onCancel) => {
let gotCancel = false;

// Let as many nodes listen to the abort signal, without getting the MaxListenersExceededWarning
setMaxListeners(Infinity, this.abortController.signal);

onCancel.shouldReject = false;
onCancel(() => {
gotCancel = true;
this.status = 'canceled';
this.abortController.abort();
void this.processSuccessExecution(
startedAt,
workflow,
new WorkflowOperationError('Workflow has been canceled!'),
closeFunction,
);
const fullRunData = this.getFullRunData(startedAt);
void this.executeHook('workflowExecuteAfter', [fullRunData]);
setTimeout(() => resolve(fullRunData), 10);
});

const returnPromise = (async () => {
Expand Down Expand Up @@ -881,10 +876,10 @@ export class WorkflowExecute {
this.additionalData.executionTimeoutTimestamp !== undefined &&
Date.now() >= this.additionalData.executionTimeoutTimestamp
) {
gotCancel = true;
this.status = 'canceled';
}

if (gotCancel) {
if (this.status === 'canceled') {
return;
}

Expand Down Expand Up @@ -1014,9 +1009,6 @@ export class WorkflowExecute {
}

for (let tryIndex = 0; tryIndex < maxTries; tryIndex++) {
if (gotCancel) {
return;
}
try {
if (tryIndex !== 0) {
// Reset executionError from previous error try
Expand Down Expand Up @@ -1646,7 +1638,7 @@ export class WorkflowExecute {
return;
})()
.then(async () => {
if (gotCancel && executionError === undefined) {
if (this.status === 'canceled' && executionError === undefined) {
return this.processSuccessExecution(
startedAt,
workflow,
Expand Down
20 changes: 16 additions & 4 deletions packages/editor-ui/src/mixins/pushConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ export const pushConnection = defineComponent({
return false;
}

if (this.workflowsStore.activeExecutionId !== pushData.executionId) {
const { activeExecutionId } = this.workflowsStore;
if (activeExecutionId !== pushData.executionId) {
// The workflow which did finish execution did either not get started
// by this session or we do not have the execution id yet.
if (isRetry !== true) {
Expand All @@ -285,10 +286,17 @@ export const pushConnection = defineComponent({

let runDataExecutedErrorMessage = this.getExecutionError(runDataExecuted.data);

if (pushData.data.status === 'crashed') {
if (runDataExecuted.status === 'crashed') {
runDataExecutedErrorMessage = this.$locale.baseText(
'pushConnection.executionFailed.message',
);
} else if (runDataExecuted.status === 'canceled') {
runDataExecutedErrorMessage = this.$locale.baseText(
'executionsList.showMessage.stopExecution.message',
{
interpolate: { activeExecutionId },
},
);
}

const lineNumber = runDataExecuted?.data?.resultData?.error?.lineNumber;
Expand Down Expand Up @@ -389,7 +397,11 @@ export const pushConnection = defineComponent({
});
} else {
let title: string;
if (runDataExecuted.data.resultData.lastNodeExecuted) {
let type = 'error';
if (runDataExecuted.status === 'canceled') {
title = this.$locale.baseText('nodeView.showMessage.stopExecutionTry.title');
type = 'warning';
} else if (runDataExecuted.data.resultData.lastNodeExecuted) {
title = `Problem in node ‘${runDataExecuted.data.resultData.lastNodeExecuted}‘`;
} else {
title = 'Problem executing workflow';
Expand All @@ -398,7 +410,7 @@ export const pushConnection = defineComponent({
this.showMessage({
title,
message: runDataExecutedErrorMessage,
type: 'error',
type,
duration: 0,
dangerouslyUseHTMLString: true,
});
Expand Down
4 changes: 0 additions & 4 deletions packages/editor-ui/src/views/NodeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1562,10 +1562,6 @@ export default defineComponent({
try {
this.stopExecutionInProgress = true;
await this.workflowsStore.stopCurrentExecution(executionId);
this.showMessage({
title: this.$locale.baseText('nodeView.showMessage.stopExecutionTry.title'),
type: 'success',
});
} catch (error) {
// Execution stop might fail when the execution has already finished. Let's treat this here.
const execution = await this.workflowsStore.getExecution(executionId);
Expand Down

0 comments on commit b7dfa0d

Please sign in to comment.