Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion x-pack/plugins/task_manager/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export interface TaskDictionary<T extends TaskDefinition> {
[taskType: string]: T;
}

export type TaskStatus = 'idle' | 'running';
export type TaskStatus = 'idle' | 'running' | 'failed';

/*
* A task instance represents all of the data required to store, fetch,
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/task_manager/task_runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ describe('TaskManagerRunner', () => {
const store = {
update: sinon.stub(),
remove: sinon.stub(),
getMaxAttempts: sinon.stub().returns(5),
};
const runner = new TaskManagerRunner({
kbnServer: sinon.stub(),
Expand Down
24 changes: 20 additions & 4 deletions x-pack/plugins/task_manager/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface TaskRunner {
interface Updatable {
update(doc: ConcreteTaskInstance): Promise<ConcreteTaskInstance>;
remove(id: string): Promise<RemoveResult>;
getMaxAttempts(): number;
}

interface Opts {
Expand Down Expand Up @@ -197,17 +198,32 @@ export class TaskManagerRunner implements TaskRunner {
}

private async processResult(result: RunResult): Promise<RunResult> {
const runAt = result.runAt || intervalFromNow(this.instance.interval);
const state = result.state || this.instance.state || {};
const recurring = result.runAt || this.instance.interval || result.error;
if (recurring) {
// recurring task: update the task instance
const state = result.state || this.instance.state || {};
const status = this.instance.attempts < this.store.getMaxAttempts() ? 'idle' : 'failed';

let runAt;
if (status === 'failed') {
// task run errored, keep the same runAt
runAt = this.instance.runAt;
} else {
runAt =
result.runAt ||
intervalFromNow(this.instance.interval) ||
minutesFromNow((this.instance.attempts + 1) * 5);
}

if (runAt || result.error) {
await this.store.update({
...this.instance,
runAt: runAt || minutesFromNow((this.instance.attempts + 1) * 5),
runAt,
state,
status,
attempts: result.error ? this.instance.attempts + 1 : 0,
});
} else {
// not a recurring task: clean up by removing the task instance from store
try {
await this.store.remove(this.instance.id);
} catch (err) {
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/task_manager/task_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ export class TaskStore {
return docs;
};

/**
* getter for maxAttempts
*
* @returns {number} maxAttempts
*/
public getMaxAttempts(): number {
return this.maxAttempts;
}

/**
* Updates the specified doc in the index, returning the doc
* with its version up to date.
Expand Down