-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
task_scheduling.ts
312 lines (288 loc) · 9.25 KB
/
task_scheduling.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import pMap from 'p-map';
import { chunk, flatten } from 'lodash';
import agent from 'elastic-apm-node';
import { Logger } from '@kbn/core/server';
import { Middleware } from './lib/middleware';
import { parseIntervalAsMillisecond } from './lib/intervals';
import {
ConcreteTaskInstance,
IntervalSchedule,
TaskInstanceWithDeprecatedFields,
TaskInstanceWithId,
TaskStatus,
} from './task';
import { TaskStore } from './task_store';
import { ensureDeprecatedFieldsAreCorrected } from './lib/correct_deprecated_fields';
import { retryableBulkUpdate } from './lib/retryable_bulk_update';
import { ErrorOutput } from './lib/bulk_operation_buffer';
const VERSION_CONFLICT_STATUS = 409;
const BULK_ACTION_SIZE = 100;
export interface TaskSchedulingOpts {
logger: Logger;
taskStore: TaskStore;
middleware: Middleware;
taskManagerId: string;
}
/**
* return type of TaskScheduling.bulkUpdateSchedules method
*/
export interface BulkUpdateTaskResult {
/**
* list of successfully updated tasks
*/
tasks: ConcreteTaskInstance[];
/**
* list of failed tasks and errors caused failure
*/
errors: ErrorOutput[];
}
export interface RunSoonResult {
id: ConcreteTaskInstance['id'];
}
export interface RunNowResult {
id: ConcreteTaskInstance['id'];
state?: ConcreteTaskInstance['state'];
}
export class TaskScheduling {
private store: TaskStore;
private logger: Logger;
private middleware: Middleware;
/**
* Initializes the task manager, preventing any further addition of middleware,
* enabling the task manipulation methods, and beginning the background polling
* mechanism.
*/
constructor(opts: TaskSchedulingOpts) {
this.logger = opts.logger;
this.middleware = opts.middleware;
this.store = opts.taskStore;
}
/**
* Schedules a task.
*
* @param task - The task being scheduled.
* @returns {Promise<ConcreteTaskInstance>}
*/
public async schedule(
taskInstance: TaskInstanceWithDeprecatedFields,
options?: Record<string, unknown>
): Promise<ConcreteTaskInstance> {
const { taskInstance: modifiedTask } = await this.middleware.beforeSave({
...options,
taskInstance: ensureDeprecatedFieldsAreCorrected(taskInstance, this.logger),
});
const traceparent =
agent.currentTransaction && agent.currentTransaction.type !== 'request'
? agent.currentTraceparent
: '';
return await this.store.schedule({
...modifiedTask,
traceparent: traceparent || '',
enabled: modifiedTask.enabled ?? true,
});
}
/**
* Bulk schedules a task.
*
* @param tasks - The tasks being scheduled.
* @returns {Promise<ConcreteTaskInstance>}
*/
public async bulkSchedule(
taskInstances: TaskInstanceWithDeprecatedFields[],
options?: Record<string, unknown>
): Promise<ConcreteTaskInstance[]> {
const traceparent =
agent.currentTransaction && agent.currentTransaction.type !== 'request'
? agent.currentTraceparent
: '';
const modifiedTasks = await Promise.all(
taskInstances.map(async (taskInstance) => {
const { taskInstance: modifiedTask } = await this.middleware.beforeSave({
...options,
taskInstance: ensureDeprecatedFieldsAreCorrected(taskInstance, this.logger),
});
return {
...modifiedTask,
traceparent: traceparent || '',
enabled: modifiedTask.enabled ?? true,
};
})
);
return await this.store.bulkSchedule(modifiedTasks);
}
public async bulkDisable(taskIds: string[], clearStateIdsOrBoolean?: string[] | boolean) {
return await retryableBulkUpdate({
taskIds,
store: this.store,
getTasks: async (ids) => await this.bulkGetTasksHelper(ids),
filter: (task) => !!task.enabled,
map: (task) => ({
...task,
enabled: false,
...((Array.isArray(clearStateIdsOrBoolean) && clearStateIdsOrBoolean.includes(task.id)) ||
clearStateIdsOrBoolean === true
? { state: {} }
: {}),
}),
validate: false,
});
}
public async bulkEnable(taskIds: string[], runSoon: boolean = true) {
return await retryableBulkUpdate({
taskIds,
store: this.store,
getTasks: async (ids) => await this.bulkGetTasksHelper(ids),
filter: (task) => !task.enabled,
map: (task, i) => {
if (runSoon) {
// Run the first task now. Run all other tasks a random number of ms in the future,
// with a maximum of 5 minutes or the task interval, whichever is smaller.
const taskToRun =
i === 0
? { ...task, runAt: new Date(), scheduledAt: new Date() }
: randomlyOffsetRunTimestamp(task);
return { ...taskToRun, enabled: true };
}
return { ...task, enabled: true };
},
validate: false,
});
}
public async bulkUpdateState(
taskIds: string[],
stateMapFn: (s: ConcreteTaskInstance['state'], id: string) => ConcreteTaskInstance['state']
) {
return await retryableBulkUpdate({
taskIds,
store: this.store,
getTasks: async (ids) => await this.bulkGetTasksHelper(ids),
filter: () => true,
map: (task) => ({
...task,
state: stateMapFn(task.state, task.id),
}),
validate: false,
});
}
/**
* Bulk updates schedules for tasks by ids.
* Only tasks with `idle` status will be updated, as for the tasks which have `running` status,
* `schedule` and `runAt` will be recalculated after task run finishes
*
* @param {string[]} taskIds - list of task ids
* @param {IntervalSchedule} schedule - new schedule
* @returns {Promise<BulkUpdateTaskResult>}
*/
public async bulkUpdateSchedules(
taskIds: string[],
schedule: IntervalSchedule
): Promise<BulkUpdateTaskResult> {
return retryableBulkUpdate({
taskIds,
store: this.store,
getTasks: async (ids) => await this.bulkGetTasksHelper(ids),
filter: (task) =>
task.status === TaskStatus.Idle && task.schedule?.interval !== schedule.interval,
map: (task) => {
const oldIntervalInMs = parseIntervalAsMillisecond(task.schedule?.interval ?? '0s');
// computing new runAt using formula:
// newRunAt = oldRunAt - oldInterval + newInterval
const newRunAtInMs = Math.max(
Date.now(),
task.runAt.getTime() - oldIntervalInMs + parseIntervalAsMillisecond(schedule.interval)
);
return { ...task, schedule, runAt: new Date(newRunAtInMs) };
},
validate: false,
});
}
private async bulkGetTasksHelper(taskIds: string[]) {
const batches = await pMap(
chunk(taskIds, BULK_ACTION_SIZE),
async (taskIdsChunk) => this.store.bulkGet(taskIdsChunk),
{ concurrency: 10 }
);
return flatten(batches);
}
/**
* Run task.
*
* @param taskId - The task being scheduled.
* @returns {Promise<RunSoonResult>}
*/
public async runSoon(taskId: string): Promise<RunSoonResult> {
const task = await this.getNonRunningTask(taskId);
try {
await this.store.update(
{
...task,
status: TaskStatus.Idle,
scheduledAt: new Date(),
runAt: new Date(),
},
{ validate: false }
);
} catch (e) {
if (e.statusCode === 409) {
this.logger.debug(
`Failed to update the task (${taskId}) for runSoon due to conflict (409)`
);
} else {
this.logger.error(`Failed to update the task (${taskId}) for runSoon`);
throw e;
}
}
return { id: task.id };
}
/**
* Schedules a task with an Id
*
* @param task - The task being scheduled.
* @returns {Promise<TaskInstanceWithId>}
*/
public async ensureScheduled(
taskInstance: TaskInstanceWithId,
options?: Record<string, unknown>
): Promise<TaskInstanceWithId> {
try {
return await this.schedule(taskInstance, options);
} catch (err) {
if (err.statusCode === VERSION_CONFLICT_STATUS) {
return taskInstance;
}
throw err;
}
}
private async getNonRunningTask(taskId: string) {
const task = await this.store.get(taskId);
switch (task.status) {
case TaskStatus.Claiming:
case TaskStatus.Running:
throw Error(`Failed to run task "${taskId}" as it is currently running`);
case TaskStatus.Unrecognized:
throw Error(`Failed to run task "${taskId}" with status ${task.status}`);
case TaskStatus.Failed:
default:
return task;
}
}
}
const randomlyOffsetRunTimestamp: (task: ConcreteTaskInstance) => ConcreteTaskInstance = (task) => {
const now = Date.now();
const maximumOffsetTimestamp = now + 1000 * 60 * 5; // now + 5 minutes
const taskIntervalInMs = parseIntervalAsMillisecond(task.schedule?.interval ?? '0s');
const maximumRunAt = Math.min(now + taskIntervalInMs, maximumOffsetTimestamp);
// Offset between 1 and maximumRunAt ms
const runAt = new Date(now + Math.floor(Math.random() * (maximumRunAt - now) + 1));
return {
...task,
runAt,
scheduledAt: runAt,
};
};