Skip to content

Commit

Permalink
Skip updating task document if task is cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
ymao1 committed Sep 9, 2021
1 parent 334f129 commit a365b57
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions x-pack/plugins/task_manager/server/task_running/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export class TaskManagerRunner implements TaskRunner {
private beforeMarkRunning: Middleware['beforeMarkRunning'];
private onTaskEvent: (event: TaskRun | TaskMarkRunning) => void;
private defaultMaxAttempts: number;
private cancelled: boolean;
private readonly executionContext: ExecutionContextStart;

/**
Expand Down Expand Up @@ -174,6 +175,7 @@ export class TaskManagerRunner implements TaskRunner {
this.onTaskEvent = onTaskEvent;
this.defaultMaxAttempts = defaultMaxAttempts;
this.executionContext = executionContext;
this.cancelled = false;
}

/**
Expand Down Expand Up @@ -279,6 +281,7 @@ export class TaskManagerRunner implements TaskRunner {
withSpan({ name: 'run', type: 'task manager' }, () => this.task!.run())
);
const validatedResult = this.validateResult(result);

const processedResult = await withSpan({ name: 'process result', type: 'task manager' }, () =>
this.processResult(validatedResult, stopTaskTimer())
);
Expand Down Expand Up @@ -407,7 +410,7 @@ export class TaskManagerRunner implements TaskRunner {
this.task = undefined;
return task.cancel();
}

this.cancelled = true;
this.logger.debug(`The task ${this} is not cancellable.`);
}

Expand Down Expand Up @@ -499,20 +502,26 @@ export class TaskManagerRunner implements TaskRunner {
unwrap
)(result);

this.instance = asRan(
await this.bufferedTaskStore.update(
defaults(
{
...fieldUpdates,
// reset fields that track the lifecycle of the concluded `task run`
startedAt: null,
retryAt: null,
ownerId: null,
},
this.instance.task
if (!this.cancelled) {
this.instance = asRan(
await this.bufferedTaskStore.update(
defaults(
{
...fieldUpdates,
// reset fields that track the lifecycle of the concluded `task run`
startedAt: null,
retryAt: null,
ownerId: null,
},
this.instance.task
)
)
)
);
);
} else {
this.logger.warn(
`Recurring task completed but doc will not be updated because task was cancelled`
);
}

return fieldUpdates.status === TaskStatus.Failed
? TaskRunResult.Failed
Expand Down

0 comments on commit a365b57

Please sign in to comment.