1 | const Queue = require("bull");
|
2 | const _ = require("lodash");
|
3 |
|
4 | /**
|
5 | * Base class for all Jobs
|
6 | *
|
7 | * Generate a new job
|
8 | *
|
9 | * ```
|
10 | * $ yarn run micro-queue generate {job_name}
|
11 | * ```
|
12 | *
|
13 | * Creates a job inside the /app/jobs/{job_name}.job.js
|
14 | *
|
15 | * @name JobBase
|
16 | */
|
17 | class JobBase {
|
18 | /**
|
19 | * Job concurrency limit
|
20 | */
|
21 | constructor(App) {
|
22 | this.app = App;
|
23 | }
|
24 |
|
25 | /**
|
26 | * The perform function is the target for all job processing. It should be
|
27 | * re-implemented in the JobBase subclass and will be called each time a job
|
28 | * instance is processed
|
29 | * */
|
30 |
|
31 |
|
32 | /**
|
33 | * Job options defaults
|
34 | */
|
35 | static perform(App, params, progress) {
|
36 | throw new Error('Must be implemented in a subclass');
|
37 | }
|
38 |
|
39 | /**
|
40 | * Called to queue a job instance
|
41 | *
|
42 | * @param params data that will be passed into the perform function
|
43 | * @param options Bull Queue JobOpts: https://github.com/OptimalBits/bull/blob/master/REFERENCE.md#queueadd
|
44 | */
|
45 | perform_later(params, options) {
|
46 | const { app_id, token, request_id } = this.app;
|
47 | const data = { app: { app_id, token, request_id }, params };
|
48 |
|
49 | const { queue, name, job_default_options } = this.constructor;
|
50 | const job_options = _.merge(job_default_options, options);
|
51 |
|
52 | return queue.add(name, data, job_options);
|
53 | }
|
54 | }
|
55 |
|
56 | module.exports = JobBase;
|
57 | module.exports.concurrency = 5;
|
58 | module.exports.job_default_options = {}; |
\ | No newline at end of file |