-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
task_runner.ts
456 lines (408 loc) · 13.6 KB
/
task_runner.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
/*
* This module contains the core logic for running an individual task.
* It handles the full lifecycle of a task run, including error handling,
* rescheduling, middleware application, etc.
*/
import apm from 'elastic-apm-node';
import { performance } from 'perf_hooks';
import Joi from 'joi';
import { identity, defaults, flow } from 'lodash';
import { asOk, asErr, mapErr, eitherAsync, unwrap, mapOk, Result } from './lib/result_type';
import { TaskRun, TaskMarkRunning, asTaskRunEvent, asTaskMarkRunningEvent } from './task_events';
import { intervalFromDate, intervalFromNow } from './lib/intervals';
import { Logger } from './types';
import { BeforeRunFunction, BeforeMarkRunningFunction } from './lib/middleware';
import {
CancelFunction,
CancellableTask,
ConcreteTaskInstance,
RunResult,
SuccessfulRunResult,
FailedRunResult,
FailedTaskResult,
TaskDefinition,
TaskDictionary,
validateRunResult,
TaskStatus,
} from './task';
const defaultBackoffPerFailure = 5 * 60 * 1000;
const EMPTY_RUN_RESULT: SuccessfulRunResult = {};
export interface TaskRunner {
isExpired: boolean;
expiration: Date;
startedAt: Date | null;
definition: TaskDefinition;
cancel: CancelFunction;
markTaskAsRunning: () => Promise<boolean>;
run: () => Promise<Result<SuccessfulRunResult, FailedRunResult>>;
id: string;
toString: () => string;
}
interface Updatable {
readonly maxAttempts: number;
update(doc: ConcreteTaskInstance): Promise<ConcreteTaskInstance>;
remove(id: string): Promise<void>;
}
interface Opts {
logger: Logger;
definitions: TaskDictionary<TaskDefinition>;
instance: ConcreteTaskInstance;
store: Updatable;
beforeRun: BeforeRunFunction;
beforeMarkRunning: BeforeMarkRunningFunction;
onTaskEvent?: (event: TaskRun | TaskMarkRunning) => void;
}
/**
* Runs a background task, ensures that errors are properly handled,
* allows for cancellation.
*
* @export
* @class TaskManagerRunner
* @implements {TaskRunner}
*/
export class TaskManagerRunner implements TaskRunner {
private task?: CancellableTask;
private instance: ConcreteTaskInstance;
private definitions: TaskDictionary<TaskDefinition>;
private logger: Logger;
private bufferedTaskStore: Updatable;
private beforeRun: BeforeRunFunction;
private beforeMarkRunning: BeforeMarkRunningFunction;
private onTaskEvent: (event: TaskRun | TaskMarkRunning) => void;
/**
* Creates an instance of TaskManagerRunner.
* @param {Opts} opts
* @prop {Logger} logger - The task manager logger
* @prop {TaskDefinition} definition - The definition of the task being run
* @prop {ConcreteTaskInstance} instance - The record describing this particular task instance
* @prop {Updatable} store - The store used to read / write tasks instance info
* @prop {BeforeRunFunction} beforeRun - A function that adjusts the run context prior to running the task
* @memberof TaskManagerRunner
*/
constructor({
instance,
definitions,
logger,
store,
beforeRun,
beforeMarkRunning,
onTaskEvent = identity,
}: Opts) {
this.instance = sanitizeInstance(instance);
this.definitions = definitions;
this.logger = logger;
this.bufferedTaskStore = store;
this.beforeRun = beforeRun;
this.beforeMarkRunning = beforeMarkRunning;
this.onTaskEvent = onTaskEvent;
}
/**
* Gets the id of this task instance.
*/
public get id() {
return this.instance.id;
}
/**
* Gets the task type of this task instance.
*/
public get taskType() {
return this.instance.taskType;
}
/**
* Gets the task defintion from the dictionary.
*/
public get definition() {
return this.definitions[this.taskType];
}
/**
* Gets the time at which this task will expire.
*/
public get expiration() {
return intervalFromDate(this.instance.startedAt!, this.definition.timeout)!;
}
/**
* Gets the duration of the current task run
*/
public get startedAt() {
return this.instance.startedAt;
}
/**
* Gets whether or not this task has run longer than its expiration setting allows.
*/
public get isExpired() {
return this.expiration < new Date();
}
/**
* Returns a log-friendly representation of this task.
*/
public toString() {
return `${this.taskType} "${this.id}"`;
}
/**
* Runs the task, handling the task result, errors, etc, rescheduling if need
* be. NOTE: the time of applying the middleware's beforeRun is incorporated
* into the total timeout time the task in configured with. We may decide to
* start the timer after beforeRun resolves
*
* @returns {Promise<Result<SuccessfulRunResult, FailedRunResult>>}
*/
public async run(): Promise<Result<SuccessfulRunResult, FailedRunResult>> {
this.logger.debug(`Running task ${this}`);
const modifiedContext = await this.beforeRun({
taskInstance: this.instance,
});
const apmTrans = apm.startTransaction(
`taskManager run ${this.instance.taskType}`,
'taskManager'
);
try {
this.task = this.definition.createTaskRunner(modifiedContext);
const result = await this.task.run();
const validatedResult = this.validateResult(result);
if (apmTrans) apmTrans.end('success');
return this.processResult(validatedResult);
} catch (err) {
this.logger.error(`Task ${this} failed: ${err}`);
// in error scenario, we can not get the RunResult
// re-use modifiedContext's state, which is correct as of beforeRun
if (apmTrans) apmTrans.end('error');
return this.processResult(asErr({ error: err, state: modifiedContext.taskInstance.state }));
}
}
/**
* Attempts to claim exclusive rights to run the task. If the attempt fails
* with a 409 (http conflict), we assume another Kibana instance beat us to the punch.
*
* @returns {Promise<boolean>}
*/
public async markTaskAsRunning(): Promise<boolean> {
performance.mark('markTaskAsRunning_start');
const apmTrans = apm.startTransaction(
`taskManager markTaskAsRunning ${this.instance.taskType}`,
'taskManager'
);
const VERSION_CONFLICT_STATUS = 409;
const now = new Date();
const { taskInstance } = await this.beforeMarkRunning({
taskInstance: this.instance,
});
const attempts = taskInstance.attempts + 1;
const ownershipClaimedUntil = taskInstance.retryAt;
try {
const { id } = taskInstance;
const timeUntilClaimExpires = howManyMsUntilOwnershipClaimExpires(ownershipClaimedUntil);
if (timeUntilClaimExpires < 0) {
this.logger.debug(
`[Task Runner] Task ${id} started after ownership expired (${Math.abs(
timeUntilClaimExpires
)}ms after expiry)`
);
}
this.instance = await this.bufferedTaskStore.update({
...taskInstance,
status: TaskStatus.Running,
startedAt: now,
attempts,
retryAt: this.instance.schedule
? intervalFromNow(this.definition.timeout)!
: this.getRetryDelay({
attempts,
// Fake an error. This allows retry logic when tasks keep timing out
// and lets us set a proper "retryAt" value each time.
error: new Error('Task timeout'),
addDuration: this.definition.timeout,
}),
});
const timeUntilClaimExpiresAfterUpdate = howManyMsUntilOwnershipClaimExpires(
ownershipClaimedUntil
);
if (timeUntilClaimExpiresAfterUpdate < 0) {
this.logger.debug(
`[Task Runner] Task ${id} ran after ownership expired (${Math.abs(
timeUntilClaimExpiresAfterUpdate
)}ms after expiry)`
);
}
if (apmTrans) apmTrans.end('success');
performanceStopMarkingTaskAsRunning();
this.onTaskEvent(asTaskMarkRunningEvent(this.id, asOk(this.instance)));
return true;
} catch (error) {
if (apmTrans) apmTrans.end('failure');
performanceStopMarkingTaskAsRunning();
this.onTaskEvent(asTaskMarkRunningEvent(this.id, asErr(error)));
if (error.statusCode !== VERSION_CONFLICT_STATUS) {
throw error;
}
}
return false;
}
/**
* Attempts to cancel the task.
*
* @returns {Promise<void>}
*/
public async cancel() {
const { task } = this;
if (task?.cancel) {
this.task = undefined;
return task.cancel();
}
this.logger.debug(`The task ${this} is not cancellable.`);
}
private validateResult(result?: RunResult | void): Result<SuccessfulRunResult, FailedRunResult> {
const { error } = Joi.validate(result, validateRunResult);
if (error) {
this.logger.warn(`Invalid task result for ${this}: ${error.message}`);
return asErr({
error: new Error(`Invalid task result for ${this}: ${error.message}`),
state: {},
});
}
if (!result) {
return asOk(EMPTY_RUN_RESULT);
}
return result.error ? asErr({ ...result, error: result.error as Error }) : asOk(result);
}
private shouldTryToScheduleRetry(): boolean {
if (this.instance.schedule) {
return true;
}
const maxAttempts = this.definition.maxAttempts || this.bufferedTaskStore.maxAttempts;
return this.instance.attempts < maxAttempts;
}
private rescheduleFailedRun = (
failureResult: FailedRunResult
): Result<SuccessfulRunResult, FailedTaskResult> => {
if (this.shouldTryToScheduleRetry()) {
const { runAt, state, error } = failureResult;
// if we're retrying, keep the number of attempts
const { schedule, attempts } = this.instance;
if (runAt || schedule) {
return asOk({ state, attempts, runAt });
} else {
// when result.error is truthy, then we're retrying because it failed
const newRunAt = this.getRetryDelay({
attempts,
error,
});
if (newRunAt) {
return asOk({ state, attempts, runAt: newRunAt });
}
}
}
// scheduling a retry isn't possible,mark task as failed
return asErr({ status: TaskStatus.Failed });
};
private async processResultForRecurringTask(
result: Result<SuccessfulRunResult, FailedRunResult>
): Promise<void> {
const fieldUpdates = flow(
// if running the task has failed ,try to correct by scheduling a retry in the near future
mapErr(this.rescheduleFailedRun),
// if retrying is possible (new runAt) or this is an recurring task - reschedule
mapOk(({ runAt, state, attempts = 0 }: Partial<ConcreteTaskInstance>) => {
const { startedAt, schedule: { interval = undefined } = {} } = this.instance;
return asOk({
runAt: runAt || intervalFromDate(startedAt!, interval)!,
state,
attempts,
status: TaskStatus.Idle,
});
}),
unwrap
)(result);
await this.bufferedTaskStore.update(
defaults(
{
...fieldUpdates,
// reset fields that track the lifecycle of the concluded `task run`
startedAt: null,
retryAt: null,
ownerId: null,
},
this.instance
)
);
}
private async processResultWhenDone(): Promise<void> {
// not a recurring task: clean up by removing the task instance from store
try {
await this.bufferedTaskStore.remove(this.instance.id);
} catch (err) {
if (err.statusCode === 404) {
this.logger.warn(`Task cleanup of ${this} failed in processing. Was remove called twice?`);
} else {
throw err;
}
}
}
private async processResult(
result: Result<SuccessfulRunResult, FailedRunResult>
): Promise<Result<SuccessfulRunResult, FailedRunResult>> {
await eitherAsync(
result,
async ({ runAt }: SuccessfulRunResult) => {
if (runAt || this.instance.schedule) {
await this.processResultForRecurringTask(result);
} else {
await this.processResultWhenDone();
}
this.onTaskEvent(asTaskRunEvent(this.id, asOk(this.instance)));
},
async ({ error }: FailedRunResult) => {
await this.processResultForRecurringTask(result);
this.onTaskEvent(asTaskRunEvent(this.id, asErr(error)));
}
);
return result;
}
private getRetryDelay({
error,
attempts,
addDuration,
}: {
error: any;
attempts: number;
addDuration?: string;
}): Date | null {
let result = null;
// Use custom retry logic, if any, otherwise we'll use the default logic
const retry: boolean | Date = this.definition.getRetry
? this.definition.getRetry(attempts, error)
: true;
if (retry instanceof Date) {
result = retry;
} else if (retry === true) {
result = new Date(Date.now() + attempts * defaultBackoffPerFailure);
}
// Add a duration to the result
if (addDuration && result) {
result = intervalFromDate(result, addDuration)!;
}
return result;
}
}
function sanitizeInstance(instance: ConcreteTaskInstance): ConcreteTaskInstance {
return {
...instance,
params: instance.params || {},
state: instance.state || {},
};
}
function howManyMsUntilOwnershipClaimExpires(ownershipClaimedUntil: Date | null): number {
return ownershipClaimedUntil ? ownershipClaimedUntil.getTime() - Date.now() : 0;
}
function performanceStopMarkingTaskAsRunning() {
performance.mark('markTaskAsRunning_stop');
performance.measure(
'taskRunner.markTaskAsRunning',
'markTaskAsRunning_start',
'markTaskAsRunning_stop'
);
}