Skip to content

Commit

Permalink
ensure scheduled tasks dont get wiped
Browse files Browse the repository at this point in the history
  • Loading branch information
gmmorris committed Oct 27, 2020
1 parent 1611932 commit 118780a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/task_manager/server/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export type FailedRunResult = SuccessfulRunResult & {
error: Error;
};

type RunResult = FailedRunResult | SuccessfulRunResult;
export type RunResult = FailedRunResult | SuccessfulRunResult;

export const isFailedRunResult = (result: unknown): result is FailedRunResult =>
!!((result as FailedRunResult)?.error ?? false);
Expand Down
28 changes: 28 additions & 0 deletions x-pack/plugins/task_manager/server/task_runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,34 @@ describe('TaskManagerRunner', () => {
sinon.assert.calledWithMatch(store.update, { runAt });
});

test('reschedules tasks that return a schedule', async () => {
const runAt = minutesFromNow(1);
const schedule = {
interval: '1m',
};
const { runner, store } = testOpts({
instance: {
status: TaskStatus.Running,
startedAt: new Date(),
},
definitions: {
bar: {
title: 'Bar!',
createTaskRunner: () => ({
async run() {
return { schedule, state: {} };
},
}),
},
},
});

await runner.run();

sinon.assert.calledOnce(store.update);
sinon.assert.calledWithMatch(store.update, { runAt });
});

test('tasks that return runAt override the schedule', async () => {
const runAt = minutesFromNow(_.random(5));
const { runner, store } = testOpts({
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/task_manager/server/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ export class TaskManagerRunner implements TaskRunner {
): Promise<Result<SuccessfulRunResult, FailedRunResult>> {
await eitherAsync(
result,
async ({ runAt }: SuccessfulRunResult) => {
if (runAt || this.instance.schedule) {
async ({ runAt, schedule }: SuccessfulRunResult) => {
if (runAt || schedule || this.instance.schedule) {
await this.processResultForRecurringTask(result);
} else {
await this.processResultWhenDone();
Expand Down

0 comments on commit 118780a

Please sign in to comment.