-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathJobManager.js
More file actions
427 lines (375 loc) · 15.9 KB
/
JobManager.js
File metadata and controls
427 lines (375 loc) · 15.9 KB
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
const path = require('path');
const util = require('util');
const setTimeoutPromise = util.promisify(setTimeout);
const fastq = require('fastq');
const later = require('@breejs/later');
const Bree = require('bree');
const pWaitFor = require('p-wait-for');
const {UnhandledJobError, IncorrectUsageError} = require('@tryghost/errors');
const logging = require('@tryghost/logging');
const isCronExpression = require('./is-cron-expression');
const assembleBreeJob = require('./assemble-bree-job');
const JobsRepository = require('./JobsRepository');
const worker = async (task, callback) => {
try {
let result = await task();
await callback(null, result);
} catch (error) {
await callback(error);
}
};
const ALL_STATUSES = {
started: 'started',
finished: 'finished',
failed: 'failed',
queued: 'queued'
};
/**
* @typedef {Object} ScheduledJob
* @property {Function | string} job - Function or path to a module defining a job
* @property {string} [name] - Unique job name, if not provided takes function name or job script filename
* @property {string | Date} [at] - Date, cron or human readable schedule format
* @property {Object} [data] - Data to be passed into the job
* @property {boolean} [offloaded=true] - If true, creates an "offloaded" job running in a worker thread. If false, runs an "inline" job on the same event loop
*/
class JobManager {
#domainEvents;
#completionPromises = new Map();
#config;
#JobModel;
#events;
/**
* @param {Object} options
* @param {Function} [options.errorHandler] - custom job error handler
* @param {Function} [options.workerMessageHandler] - custom message handler coming from workers
* @param {Object} [options.JobModel] - a model which can persist job data in the storage
* @param {Object} [options.domainEvents] - domain events emitter
* @param {Object} [options.config] - config
* @param {Object} [options.events] - events instance (for testing)
*/
constructor({errorHandler, workerMessageHandler, JobModel, domainEvents, config, events = null}) {
this.inlineQueue = fastq(this, worker, 3);
this._jobMessageHandler = this._jobMessageHandler.bind(this);
this._jobErrorHandler = this._jobErrorHandler.bind(this);
this.#domainEvents = domainEvents;
this.#config = config;
this.#JobModel = JobModel;
this.#events = events;
const combinedMessageHandler = workerMessageHandler
? ({name, message}) => {
workerMessageHandler({name, message});
this._jobMessageHandler({name, message});
}
: this._jobMessageHandler;
const combinedErrorHandler = errorHandler
? (error, workerMeta) => {
errorHandler(error, workerMeta);
this._jobErrorHandler(error, workerMeta);
}
: this._jobErrorHandler;
this.bree = new Bree({
root: false, // set this to `false` to prevent requiring a root directory of jobs
hasSeconds: true, // precision is needed to avoid task overlaps after immediate execution
outputWorkerMetadata: true,
logger: logging,
errorHandler: combinedErrorHandler,
workerMessageHandler: combinedMessageHandler
});
this.bree.on('worker created', (name) => {
this._jobMessageHandler({name, message: ALL_STATUSES.started});
});
if (JobModel) {
this._jobsRepository = new JobsRepository({JobModel});
}
}
inlineJobHandler(jobName) {
return async (error, result) => {
if (error) {
await this._jobErrorHandler(error, {
name: jobName
});
} else {
await this._jobMessageHandler({
name: jobName,
message: 'done'
});
}
// Can potentially standardize the result here
return result;
};
}
async _jobMessageHandler({name, message}) {
if (name) {
if (message === ALL_STATUSES.started) {
if (this._jobsRepository) {
const job = await this._jobsRepository.read(name);
if (job) {
await this._jobsRepository.update(job.id, {
status: ALL_STATUSES.started,
started_at: new Date()
});
}
}
} else if (message === 'done') {
if (this._jobsRepository) {
const job = await this._jobsRepository.read(name);
if (job) {
await this._jobsRepository.update(job.id, {
status: ALL_STATUSES.finished,
finished_at: new Date()
});
}
}
// Check completion listeners
if (this.#completionPromises.has(name)) {
for (const listeners of this.#completionPromises.get(name)) {
listeners.resolve();
}
// Clear the listeners
this.#completionPromises.delete(name);
}
if (this.inlineQueue.length() <= 1) {
if (this.#completionPromises.has('all')) {
for (const listeners of this.#completionPromises.get('all')) {
listeners.resolve();
}
// Clear the listeners
this.#completionPromises.delete('all');
}
}
} else {
if (typeof message === 'object' && this.#domainEvents) {
// Is this an event?
if (message.event) {
this.#domainEvents.dispatchRaw(message.event.type, message.event.data);
}
}
}
}
}
async _jobErrorHandler(error, jobMeta) {
if (this._jobsRepository && jobMeta.name) {
const job = await this._jobsRepository.read(jobMeta.name);
if (job) {
await this._jobsRepository.update(job.id, {
status: ALL_STATUSES.failed
});
}
}
// Check completion listeners and call them with error
if (this.#completionPromises.has(jobMeta.name)) {
for (const listeners of this.#completionPromises.get(jobMeta.name)) {
listeners.reject(error);
}
// Clear the listeners
this.#completionPromises.delete(jobMeta.name);
}
if (this.inlineQueue.length() <= 1) {
if (this.#completionPromises.has('all')) {
for (const listeners of this.#completionPromises.get('all')) {
listeners.reject(error);
}
// Clear the listeners
this.#completionPromises.delete('all');
}
}
}
/**
* By default schedules an "offloaded" job. If `offloaded: true` parameter is set,
* puts an "inline" immediate job into the inlineQueue.
*
* @param {Object} GhostJob - job options
* @prop {Function | String} GhostJob.job - function or path to a module defining a job
* @prop {String} [GhostJob.name] - unique job name, if not provided takes function name or job script filename
* @prop {String | Date} [GhostJob.at] - Date, cron or human readable schedule format. Manage will do immediate execution if not specified. Not supported for "inline" jobs
* @prop {Object} [GhostJob.data] - data to be passed into the job
* @prop {Boolean} [GhostJob.offloaded] - creates an "offloaded" job running in a worker thread by default. If set to "false" runs an "inline" job on the same event loop
*/
async addJob({name, at, job, data, offloaded = true}) {
if (offloaded) {
logging.info('Adding offloaded job to the inline job queue');
let schedule;
if (!name) {
if (typeof job === 'string') {
name = path.parse(job).name;
} else {
throw new IncorrectUsageError({
message: 'Name parameter should be present if job is a function'
});
}
}
if (at && !(at instanceof Date)) {
if (isCronExpression(at)) {
schedule = later.parse.cron(at, true);
} else {
schedule = later.parse.text(at);
}
if ((schedule.error && schedule.error !== -1) || schedule.schedules.length === 0) {
throw new IncorrectUsageError({
message: 'Invalid schedule format'
});
}
logging.info(`Scheduling job ${name} at ${at}. Next run on: ${later.schedule(schedule).next()}`);
} else if (at !== undefined) {
logging.info(`Scheduling job ${name} at ${at}`);
} else {
logging.info(`Scheduling job ${name} to run immediately`);
}
const breeJob = assembleBreeJob(at, job, data, name);
await this.bree.add(breeJob);
return this.bree.start(name);
} else {
logging.info(`Adding one-off job to inlineQueue with current length = ${this.inlineQueue.length()} called '${name || 'anonymous'}'`);
this.inlineQueue.push(async () => {
try {
// NOTE: setting the status here otherwise it is impossible to
// distinguish between states when the job fails immediately
await this._jobMessageHandler({
name: name,
message: ALL_STATUSES.started
});
if (typeof job === 'function') {
await job(data);
} else {
await require(job)(data);
}
} catch (err) {
// NOTE: each job should be written in a safe way and handle all errors internally
// if the error is caught here jobs implementation should be changed
logging.error(new UnhandledJobError({
context: (typeof job === 'function') ? 'function' : job,
err
}));
throw err;
}
}, this.inlineJobHandler(name));
}
}
/**
* Adds a job that could ever be executed once. In case the job fails
* can be "added" again, effectively restarting the failed job.
*
* @param {Object} GhostJob - job options
* @prop {Function | String} GhostJob.job - function or path to a module defining a job
* @prop {String} GhostJob.name - unique job name, if not provided takes function name or job script filename
* @prop {String | Date} [GhostJob.at] - Date, cron or human readable schedule format. Manage will do immediate execution if not specified. Not supported for "inline" jobs
* @prop {Object} [GhostJob.data] - data to be passed into the job
* @prop {Boolean} [GhostJob.offloaded] - creates an "offloaded" job running in a worker thread by default. If set to "false" runs an "inline" job on the same event loop
*/
async addOneOffJob({name, job, data, offloaded = true}) {
if (!name) {
throw new IncorrectUsageError({
message: `The name parameter is required for a one off job.`
});
}
const persistedJob = await this._jobsRepository.read(name);
if (persistedJob && (persistedJob.get('status') !== ALL_STATUSES.failed)) {
throw new IncorrectUsageError({
message: `A "${name}" one off job has already been executed.`
});
}
if (persistedJob && (persistedJob.get('status') === ALL_STATUSES.failed)) {
await this._jobsRepository.update(persistedJob.id, {
status: ALL_STATUSES.queued
});
} else {
await this._jobsRepository.add({
name,
status: ALL_STATUSES.queued
});
}
// NOTE: there's a assumption the job with the same name failed while
// running under different instance of job manager (bree).
// For example, it failed and the process was restarted.
// If we want to be able to restart within the same instance,
// we'd need to handle job restart/removal in Bree first
this.addJob({name, job, data, offloaded});
}
/**
* Checks if the one-off job has ever been executed successfully.
* @param {String} name one-off job name
*/
async hasExecutedSuccessfully(name) {
if (this._jobsRepository) {
const persistedJob = await this._jobsRepository.read(name);
if (!persistedJob) {
return false;
} else {
return (persistedJob.get('status') !== ALL_STATUSES.failed);
}
} else {
return false;
}
}
/**
* Awaits completion of the offloaded one-off job.
* CAUTION: it might take a long time to resolve!
* @param {string} name one-off job name
* @returns resolves with a Job model at current state
*/
async awaitOneOffCompletion(name) {
const persistedJob = await this._jobsRepository.read(name);
if (!persistedJob || ![ALL_STATUSES.finished, ALL_STATUSES.failed].includes(persistedJob.get('status'))) {
// NOTE: can implement exponential backoff here if that's ever needed
await setTimeoutPromise(500);
return this.awaitOneOffCompletion(name);
}
return persistedJob;
}
/***
* Create this promise before you add the job you want to listen for. Then await the returned promise.
* Resolves if the job has been executed successfully.
* Throws an error if the job has failed execution.
*/
async awaitCompletion(name) {
const promise = new Promise((resolve, reject) => {
this.#completionPromises.set(name, [
...(this.#completionPromises.get(name) ?? []),
{resolve, reject}
]);
});
return promise;
}
/**
* Wait for all inline jobs to be completed.
*/
async allSettled() {
const name = 'all';
return new Promise((resolve, reject) => {
if (this.inlineQueue.idle()) {
resolve();
return;
}
this.#completionPromises.set(name, [
...(this.#completionPromises.get(name) ?? []),
{resolve, reject}
]);
});
}
/**
* Removes an "offloaded" job from scheduled jobs inlineQueue.
* It's NOT yet possible to remove "inline" jobs (will be possible when scheduling is added https://github.com/breejs/bree/issues/68).
* The method will throw an Error if job with provided name does not exist.
*
* NOTE: current implementation does not guarante running job termination
* for details see https://github.com/breejs/bree/pull/64
*
* @param {String} name - job name
*/
async removeJob(name) {
await this.bree.remove(name);
}
/**
* @param {import('p-wait-for').Options} [options]
*/
async shutdown(options) {
await this.bree.stop();
if (this.inlineQueue.idle()) {
return;
}
logging.warn('Waiting for busy job in inline job queue');
await pWaitFor(() => this.inlineQueue.idle() === true, options);
logging.warn('Inline job queue finished');
}
}
module.exports = JobManager;