Skip to content

Commit

Permalink
fallback to existing task schedule when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
gmmorris committed Oct 29, 2020
1 parent 118780a commit cd4f668
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 19 deletions.
82 changes: 67 additions & 15 deletions x-pack/plugins/alerts/server/task_runner/task_runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { eventLoggerMock } from '../../../event_log/server/event_logger.mock';
import { IEventLogger } from '../../../event_log/server';
import { SavedObjectsErrorHelpers } from '../../../../../src/core/server';
import { Alert } from '../../common';
import { omit } from 'lodash';
const alertType = {
id: 'test',
name: 'My test alert',
Expand All @@ -43,6 +44,7 @@ describe('Task Runner', () => {
status: TaskStatus.Running,
version: '123',
runAt: new Date(),
schedule: { interval: '10s' },
scheduledAt: new Date(),
startedAt: new Date(),
retryAt: new Date(Date.now() + 5 * 60 * 1000),
Expand Down Expand Up @@ -145,15 +147,17 @@ describe('Task Runner', () => {
});
const runnerResult = await taskRunner.run();
expect(runnerResult).toMatchInlineSnapshot(`
Object {
"runAt": 1970-01-01T00:00:10.000Z,
"state": Object {
"alertInstances": Object {},
"alertTypeState": undefined,
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
}
`);
Object {
"schedule": Object {
"interval": "10s",
},
"state": Object {
"alertInstances": Object {},
"alertTypeState": undefined,
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
}
`);
expect(alertType.executor).toHaveBeenCalledTimes(1);
const call = alertType.executor.mock.calls[0][0];
expect(call.params).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -620,7 +624,9 @@ describe('Task Runner', () => {
});
expect(await taskRunner.run()).toMatchInlineSnapshot(`
Object {
"runAt": 1970-01-01T00:00:10.000Z,
"schedule": Object {
"interval": "10s",
},
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
Expand Down Expand Up @@ -726,7 +732,9 @@ describe('Task Runner', () => {

expect(runnerResult).toMatchInlineSnapshot(`
Object {
"runAt": 1970-01-01T00:00:10.000Z,
"schedule": Object {
"interval": "10s",
},
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
Expand Down Expand Up @@ -780,7 +788,9 @@ describe('Task Runner', () => {

expect(runnerResult).toMatchInlineSnapshot(`
Object {
"runAt": 1970-01-01T00:05:00.000Z,
"schedule": Object {
"interval": "10s",
},
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
Expand Down Expand Up @@ -813,7 +823,9 @@ describe('Task Runner', () => {

expect(runnerResult).toMatchInlineSnapshot(`
Object {
"runAt": 1970-01-01T00:05:00.000Z,
"schedule": Object {
"interval": "10s",
},
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
Expand Down Expand Up @@ -845,7 +857,47 @@ describe('Task Runner', () => {

expect(runnerResult).toMatchInlineSnapshot(`
Object {
"runAt": 1970-01-01T00:05:00.000Z,
"schedule": Object {
"interval": "10s",
},
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
}
`);
});

test('recovers gracefully when the Runner of a legacy Alert task which has no schedule throws an exception when fetching attributes', async () => {
alertsClient.get.mockImplementation(() => {
throw new Error('OMG');
});

// legacy alerts used to run by returning a new `runAt` instead of using a schedule
// ensure we return a fallback schedule when this happens, otherwise the task might be deleted
const legacyTaskInstance = omit(mockedTaskInstance, 'schedule');

const taskRunner = new TaskRunner(
alertType,
legacyTaskInstance,
taskRunnerFactoryInitializerParams
);

encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValueOnce({
id: '1',
type: 'alert',
attributes: {
apiKey: Buffer.from('123:abc').toString('base64'),
},
references: [],
});

const runnerResult = await taskRunner.run();

expect(runnerResult).toMatchInlineSnapshot(`
Object {
"schedule": Object {
"interval": "5m",
},
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
Expand Down Expand Up @@ -877,7 +929,7 @@ describe('Task Runner', () => {

expect(runnerResult).toMatchInlineSnapshot(`
Object {
"runAt": undefined,
"schedule": undefined,
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
Expand Down
10 changes: 6 additions & 4 deletions x-pack/plugins/alerts/server/task_runner/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { TaskRunnerContext } from './task_runner_factory';
import { ConcreteTaskInstance } from '../../../task_manager/server';
import { createExecutionHandler } from './create_execution_handler';
import { AlertInstance, createAlertInstanceFactory } from '../alert_instance';
import { getNextRunAt } from './get_next_run_at';
import {
validateAlertTypeParams,
executionStatusFromState,
Expand Down Expand Up @@ -38,7 +37,7 @@ import { isAlertSavedObjectNotFoundError } from '../lib/is_alert_not_found_error
import { AlertsClient } from '../alerts_client';
import { partiallyUpdateAlert } from '../saved_objects';

const FALLBACK_RETRY_INTERVAL: IntervalSchedule = { interval: '5m' };
const FALLBACK_RETRY_INTERVAL = '5m';

interface AlertTaskRunResult {
state: AlertTaskState;
Expand Down Expand Up @@ -316,6 +315,7 @@ export class TaskRunner {
params: { alertId, spaceId },
startedAt: previousStartedAt,
state: originalState,
schedule: taskSchedule,
} = this.taskInstance;

const { state, schedule } = await errorAsAlertTaskRunResult(this.loadAlertAttributesAndRun());
Expand Down Expand Up @@ -368,8 +368,10 @@ export class TaskRunner {
};
}
),
schedule: resolveErr<IntervalSchedule | undefined, Error>(schedule, (err) => {
return isAlertSavedObjectNotFoundError(err, alertId) ? undefined : FALLBACK_RETRY_INTERVAL;
schedule: resolveErr<IntervalSchedule | undefined, Error>(schedule, (error) => {
return isAlertSavedObjectNotFoundError(error, alertId)
? undefined
: { interval: taskSchedule?.interval ?? FALLBACK_RETRY_INTERVAL };
}),
};
}
Expand Down

0 comments on commit cd4f668

Please sign in to comment.