Skip to content

Commit

Permalink
Split schedule between cleanup and idle
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Apr 19, 2021
1 parent 8aa2d06 commit 25a5e6b
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 18 deletions.
3 changes: 2 additions & 1 deletion x-pack/plugins/actions/server/actions_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ describe('create()', () => {
responseTimeout: moment.duration('60s'),
cleanupFailedExecutionsTask: {
enabled: true,
interval: schema.duration().validate('15s'),
cleanupInterval: schema.duration().validate('5m'),
idleInterval: schema.duration().validate('1h'),
pageSize: 100,
},
});
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/actions/server/actions_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const defaultActionsConfig: ActionsConfig = {
responseTimeout: moment.duration(60000),
cleanupFailedExecutionsTask: {
enabled: true,
interval: schema.duration().validate('15s'),
cleanupInterval: schema.duration().validate('5m'),
idleInterval: schema.duration().validate('1h'),
pageSize: 100,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ describe('ensureScheduled', () => {

const config: ActionsConfig['cleanupFailedExecutionsTask'] = {
enabled: true,
interval: schema.duration().validate('5m'),
cleanupInterval: schema.duration().validate('5m'),
idleInterval: schema.duration().validate('1h'),
pageSize: 100,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import { TaskManagerStartContract, asInterval } from '../../../task_manager/serv
export async function ensureScheduled(
taskManager: TaskManagerStartContract,
logger: Logger,
{ interval }: ActionsConfig['cleanupFailedExecutionsTask']
{ cleanupInterval }: ActionsConfig['cleanupFailedExecutionsTask']
) {
try {
await taskManager.ensureScheduled({
id: TASK_ID,
taskType: TASK_TYPE,
schedule: {
interval: asInterval(interval.asMilliseconds()),
interval: asInterval(cleanupInterval.asMilliseconds()),
},
state: {
runs: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ describe('findAndCleanupTasks', () => {

const config: ActionsConfig['cleanupFailedExecutionsTask'] = {
enabled: true,
interval: schema.duration().validate('5m'),
cleanupInterval: schema.duration().validate('5m'),
idleInterval: schema.duration().validate('1h'),
pageSize: 100,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ export interface FindAndCleanupTasksOpts {
taskManagerIndex: string;
}

export interface FindAndCleanupTasksResult extends CleanupTasksResult {
remaining: number;
}

export async function findAndCleanupTasks({
logger,
actionTypeRegistry,
coreStartServices,
config,
kibanaIndex,
taskManagerIndex,
}: FindAndCleanupTasksOpts): Promise<CleanupTasksResult> {
}: FindAndCleanupTasksOpts): Promise<FindAndCleanupTasksResult> {
logger.debug('Starting cleanup of failed executions');
const [{ savedObjects, elasticsearch }, { spaces }] = await coreStartServices;
const esClient = elasticsearch.client.asInternalUser;
Expand Down Expand Up @@ -69,5 +73,8 @@ export async function findAndCleanupTasks({
logger.debug(
`Finished cleanup of failed executions. [success=${cleanupResult.successCount}, failures=${cleanupResult.failureCount}]`
);
return cleanupResult;
return {
...cleanupResult,
remaining: result.total - cleanupResult.successCount,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ describe('registerTaskDefinition', () => {

const config: ActionsConfig['cleanupFailedExecutionsTask'] = {
enabled: true,
interval: schema.duration().validate('5m'),
cleanupInterval: schema.duration().validate('5m'),
idleInterval: schema.duration().validate('1h'),
pageSize: 100,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ describe('taskRunner', () => {

const config: ActionsConfig['cleanupFailedExecutionsTask'] = {
enabled: true,
interval: schema.duration().validate('5m'),
cleanupInterval: schema.duration().validate('5m'),
idleInterval: schema.duration().validate('1h'),
pageSize: 100,
};

Expand Down Expand Up @@ -60,6 +61,7 @@ describe('taskRunner', () => {
success: true,
successCount: 1,
failureCount: 1,
remaining: 0,
});
});

Expand All @@ -81,7 +83,21 @@ describe('taskRunner', () => {
});
});

it('should return latest schedule', async () => {
it('should return idle schedule when no remaining tasks to cleanup', async () => {
const runner = taskRunner(taskRunnerOpts)({ taskInstance });
const { schedule } = await runner.run();
expect(schedule).toEqual({
interval: '60m',
});
});

it('should return cleanup schedule when there are some remaining tasks to cleanup', async () => {
jest.requireMock('./find_and_cleanup_tasks').findAndCleanupTasks.mockResolvedValue({
success: true,
successCount: 1,
failureCount: 1,
remaining: 1,
});
const runner = taskRunner(taskRunnerOpts)({ taskInstance });
const { schedule } = await runner.run();
expect(schedule).toEqual({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export function taskRunner(opts: TaskRunnerOpts) {
total_cleaned_up: state.total_cleaned_up + cleanupResult.successCount,
},
schedule: {
interval: asInterval(opts.config.interval.asMilliseconds()),
interval:
cleanupResult.remaining > 0
? asInterval(opts.config.cleanupInterval.asMilliseconds())
: asInterval(opts.config.idleInterval.asMilliseconds()),
},
};
},
Expand Down
6 changes: 4 additions & 2 deletions x-pack/plugins/actions/server/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ describe('config validation', () => {
"*",
],
"cleanupFailedExecutionsTask": Object {
"cleanupInterval": "PT5M",
"enabled": true,
"interval": "PT5M",
"idleInterval": "PT1H",
"pageSize": 100,
},
"enabled": true,
Expand Down Expand Up @@ -64,8 +65,9 @@ describe('config validation', () => {
"*",
],
"cleanupFailedExecutionsTask": Object {
"cleanupInterval": "PT5M",
"enabled": true,
"interval": "PT5M",
"idleInterval": "PT1H",
"pageSize": 100,
},
"enabled": true,
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/actions/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export const configSchema = schema.object({
responseTimeout: schema.duration({ defaultValue: '60s' }),
cleanupFailedExecutionsTask: schema.object({
enabled: schema.boolean({ defaultValue: true }),
interval: schema.duration({ defaultValue: '5m' }),
cleanupInterval: schema.duration({ defaultValue: '5m' }),
idleInterval: schema.duration({ defaultValue: '1h' }),
pageSize: schema.number({ defaultValue: 100 }),
}),
});
Expand Down
9 changes: 6 additions & 3 deletions x-pack/plugins/actions/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ describe('Actions Plugin', () => {
responseTimeout: moment.duration(60000),
cleanupFailedExecutionsTask: {
enabled: true,
interval: schema.duration().validate('15s'),
cleanupInterval: schema.duration().validate('5m'),
idleInterval: schema.duration().validate('1h'),
pageSize: 100,
},
});
Expand Down Expand Up @@ -214,7 +215,8 @@ describe('Actions Plugin', () => {
responseTimeout: moment.duration(60000),
cleanupFailedExecutionsTask: {
enabled: true,
interval: schema.duration().validate('15s'),
cleanupInterval: schema.duration().validate('5m'),
idleInterval: schema.duration().validate('1h'),
pageSize: 100,
},
});
Expand Down Expand Up @@ -286,7 +288,8 @@ describe('Actions Plugin', () => {
responseTimeout: moment.duration('60s'),
cleanupFailedExecutionsTask: {
enabled: true,
interval: schema.duration().validate('15s'),
cleanupInterval: schema.duration().validate('5m'),
idleInterval: schema.duration().validate('1h'),
pageSize: 100,
},
...overrides,
Expand Down

0 comments on commit 25a5e6b

Please sign in to comment.