-
Notifications
You must be signed in to change notification settings - Fork 6
/
task.ts
331 lines (276 loc) · 8.93 KB
/
task.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
import { DispatchableEvent, emit, hasListeners } from "../events";
import AwaitLock from "await-lock";
import { Logger, getLogger } from "../utils/logger";
import { TaskChain, TaskResultStatus } from "./task-chain";
import { now } from "../utils/time";
import { flatten } from "../utils/serialization";
export abstract class Task {
get name(): string {
return this._name;
}
get outputEventNames(): Array<string> {
return this._taskConfig.outputEventNames;
}
private _taskConfig: TaskConfig;
private _taskParams: TaskParams;
private _invocationEvent: DispatchableEvent;
private readonly _name: string;
private readonly _inQueue: Set<string>;
private readonly _executionHistory: Set<string>;
private readonly _cancelFunctions: Map<string, CancelFunction>;
private readonly _executionLock: AwaitLock;
private _logger: Logger;
constructor(name: string, taskConfig: TaskConfig = {}) {
this._name = name;
this._inQueue = new Set();
this._executionHistory = new Set();
this._cancelFunctions = new Map();
this._executionLock = new AwaitLock();
this.configureTask(taskConfig);
}
/**
* To be called by the task runner. Performs pre-execution checks and runs the task.
* @param taskParams The runtime parameters of the task
* @param invocationEvent The event causing the task to be executed
*/
async run(
taskParams: TaskParams,
invocationEvent: DispatchableEvent
): Promise<void> {
this._inQueue.add(invocationEvent.id);
await this._executionLock.acquireAsync();
this._taskParams = taskParams;
this._invocationEvent = invocationEvent;
if (this.isDone()) {
this._executionLock.release();
return;
}
this.log(
`Run triggered by ${invocationEvent.name} event ${
invocationEvent.expirationTimestamp !== -1
? `with ${invocationEvent.expirationTimestamp} expirationTimestamp`
: ``
}`
);
let executionError: Error;
try {
await this.checkIfCanRun();
const outcome = await this.onRun(taskParams, invocationEvent);
if (!this.isDone()) {
this.processTaskOutcome(outcome);
}
} catch (err) {
this.getLogger().error(
`Execution failed with params ${JSON.stringify(
taskParams
)} and invocation event ${JSON.stringify(invocationEvent)}: ${
err.stack ? err.stack : err
}`
);
this.emitEndEvent(TaskResultStatus.Error, err);
executionError = err;
}
this.removeCancelFunction();
this._inQueue.delete(this._invocationEvent.id);
this._executionLock.release();
if (executionError) throw executionError;
}
/**
* Indicates if a task runs in background (true by default). Can be configured at instantiation time.
*/
runsInBackground(): boolean {
return !this._taskConfig.foreground;
}
/**
* Method to be called by the task runner if the task takes longer than expected to be
* executed. Finishes the task chain and gracefully cancels the task through onCancel.
*/
cancel(): void {
for (const id of this._inQueue) {
this.emitEndEvent(TaskResultStatus.Cancelled, undefined, id);
}
this._inQueue.clear();
if (
this._invocationEvent &&
this._cancelFunctions.has(this._invocationEvent.id)
) {
const cancelFunction = this._cancelFunctions.get(
this._invocationEvent.id
);
cancelFunction();
this.removeCancelFunction();
}
this.log("Cancelled");
}
/**
* Runs task pre run checks (does nothing by default). Should be overridden if certain
* conditions must be met. Exceptions are expected in case certain task precondition is not met.
*/
checkIfCanRun(): Promise<void> {
return Promise.resolve();
}
/**
* Performs actions in order to prepare the task for execution. Should be overridden if certain
* custom actions have to be done.
*/
prepare(): Promise<void> {
return Promise.resolve();
}
/**
* Run the task again within a certain amount of time. WARNING: it won't work
* if the amount of time is exactly the same than for the current run. In that
* situation use a recurrent task instead.
* @param seconds amount of seconds in which the task will be executed again
* @param params optional parameters that will be passed to the task
*/
protected runAgainIn(seconds: number, params?: TaskParams) {
defer(this.name, seconds, params ? params : this._taskParams);
this.log(`Will run again in ${seconds} s`);
}
/**
* The content of the task to be executed
*/
protected abstract onRun(
taskParams: TaskParams,
invocationEvent: DispatchableEvent
): Promise<void | TaskOutcome>;
/**
* Method to be called to inject a function in charge of cleaning up
* resources on task cancellation
*/
protected setCancelFunction(f: CancelFunction) {
if (this.isDone()) {
f();
return;
}
this._cancelFunctions.set(this._invocationEvent.id, f);
}
/**
* Meant to be used by the task itself. Logs should be printed through here.
* @param message The message to be printed
*/
protected log(message: any) {
this.getLogger().info(
`${message} (invocationId=${
this._invocationEvent ? this._invocationEvent.id : "??"
})`
);
}
/**
* Meant to be used by the task itself. Provides the amount of time until the timeout will fire.
*/
protected remainingTime(): number {
if (this._invocationEvent.expirationTimestamp === -1) {
return -1;
}
let timeForExpiration = this._invocationEvent.expirationTimestamp - now();
if (this.outputEventNames.some(hasListeners)) {
timeForExpiration *= 0.9;
}
return Math.floor(timeForExpiration);
}
private configureTask(taskConfig: TaskConfig) {
this._taskConfig = {
foreground: taskConfig.foreground,
outputEventNames:
taskConfig.outputEventNames && taskConfig.outputEventNames.length > 0
? taskConfig.outputEventNames
: [`${this.name}Finished`],
};
}
private processTaskOutcome(outcome: void | TaskOutcome) {
if (this.outputEventNames.length > 1 && (!outcome || !outcome.eventName)) {
this.getLogger().error(
`Task was declared to have more than one possible output event, but outcome did not specify any.
Please, check if you forgot to fill eventName field in any of the outputs of your conditional branches.
Received outcome: ${JSON.stringify(outcome)}`
);
const reason = new Error("Cannot choose which event to emit");
this.emitEndEvent(TaskResultStatus.Error, reason);
return;
}
const outputEvent =
!outcome || !outcome.eventName
? this.outputEventNames[0]
: outcome.eventName;
if (!outcome || !outcome.result) {
this.done(outputEvent);
} else {
this.done(outputEvent, outcome.result);
}
}
private done(eventName: string, result?: any) {
if (this.isDone()) {
return;
}
this.markAsDone();
if (!this.outputEventNames.find((name) => name === eventName)) {
this.getLogger().warn(
`About to emit an event (${eventName}) not declared in the list of possible output events of this task.
This can lead to unintended behaviors. Perhaps you have forgot to include it in the task configuration or there is a typo`
);
}
if (!hasListeners(eventName)) {
this.emitEndEvent(TaskResultStatus.Ok);
return;
}
const data = result
? typeof result === "object"
? flatten(result)
: { result }
: {};
emit({
name: eventName,
id: this._invocationEvent.id,
expirationTimestamp: this._invocationEvent.expirationTimestamp,
data,
});
this.log(`Finished running with ${eventName} event`);
}
private isDone(): boolean {
return this._executionHistory.has(this._invocationEvent.id);
}
private markAsDone(invocationId?: string) {
const id = invocationId ? invocationId : this._invocationEvent.id;
this._executionHistory.add(id);
}
private emitEndEvent(
status: TaskResultStatus,
err?: Error,
invocationId?: string
): void {
const id = invocationId ? invocationId : this._invocationEvent.id;
this.markAsDone(id);
TaskChain.finalize(id, status, err);
}
private removeCancelFunction() {
this._cancelFunctions.delete(this._invocationEvent.id);
}
private getLogger() {
if (!this._logger) {
this._logger = getLogger(`Task (${this._name})`);
}
return this._logger;
}
}
export interface TaskConfig {
foreground?: boolean;
outputEventNames?: Array<string>;
}
export interface TaskParams {
[key: string]: any;
}
export interface TaskOutcome {
eventName?: string;
result?: any;
}
export type CancelFunction = () => void;
export type TaskDeferrer = (
taskName: string,
seconds: number,
taskParams: TaskParams
) => void;
let defer: TaskDeferrer;
export function setTaskDeferrer(taskDeferrer: TaskDeferrer) {
defer = taskDeferrer;
}