1 | # Knetik Micro Queue
|
2 |
|
3 | This package adds redis queue support using Bull Queue to any
|
4 | [micro-core](https://www.npmjs.com/package/@knetik/micro-core) application
|
5 | as a multi tenant adaptor.
|
6 |
|
7 | When a customer connects to a micro-core application, the required app_id
|
8 | is passed into the adaptor initializer and used as the context for any jobs
|
9 | created using the micro queue Job class interface.
|
10 |
|
11 | ### Setup
|
12 |
|
13 | Install the `@knetik/micro-queue` package
|
14 |
|
15 | ```
|
16 | yarn add @knetik/micro-queue
|
17 | ```
|
18 |
|
19 | Add the Adaptor to your config/environments/{env}.json
|
20 |
|
21 | ```
|
22 | "ADAPTORS": [
|
23 | "@knetik/micro-queue"
|
24 | ]
|
25 | ```
|
26 |
|
27 | Add the `REDIS_HOST` param as well
|
28 |
|
29 | ```
|
30 | "REDIS_HOST": "127.0.0.1"
|
31 | ```
|
32 |
|
33 | ## Job Generator
|
34 |
|
35 | ```
|
36 | $ yarn run micro-queue generate {job_name}
|
37 | ```
|
38 |
|
39 | A file is generated in `app/jobs/{job_name}.job.js`
|
40 |
|
41 | if the `{job_name}` id `some_long_process`, the contents of `app/jobs/{job_name}.job.js` are:
|
42 |
|
43 | ```
|
44 | const { JobBase } = require('@knetik/micro-queue');
|
45 |
|
46 | module.exports = class SomeLongProcessJob extends JobBase {
|
47 | static perform(App, params, progress) {
|
48 | App.Logger.info('performing SomeLongProcessJob');
|
49 |
|
50 | /* ====== EXAMPLE USAGE ========= */
|
51 | App.Logger.info('SomeLongProcessJob', params);
|
52 |
|
53 | let pending = 100;
|
54 | let total = 0;
|
55 |
|
56 | // Jobs must return a Promise
|
57 | return new Promise((resolve, reject) => {
|
58 | const interval = setInterval(() => {
|
59 | pending -= 1;
|
60 | total += 1;
|
61 |
|
62 | App.Logger.info('SomeLongProcessJob progress', pending);
|
63 |
|
64 | // use progress to increment the jobs progress status
|
65 | progress(total);
|
66 |
|
67 | if (!pending) {
|
68 | resolve('done');
|
69 | clearInterval(interval);
|
70 | }
|
71 | }, 1000);
|
72 | });
|
73 | /* ====== END EXAMPLE USAGE ========= */
|
74 | }
|
75 | };
|
76 |
|
77 | // Set the max concurrency value for this job.
|
78 | module.exports.concurrency = 50;
|
79 | ```
|
80 |
|
81 | ### Usage
|
82 |
|
83 | The Adaptor is added to the Micro Core application at `App.Queue`. Use
|
84 | `App.Queue#get` to load job classes form the App's initialized jobs.
|
85 |
|
86 | ```
|
87 | const Job = App.Queue.get('SomeLongProcessJob');
|
88 | ```
|
89 |
|
90 | Call `perform_later` on the Job class passing in the job data and the job
|
91 | options
|
92 |
|
93 | ```
|
94 | const data = { hi: 'there' };
|
95 | const options = {};
|
96 |
|
97 | Job.perform_later(data, options);
|
98 | ```
|
99 |
|
100 | ### Queue UI
|
101 |
|
102 | if the Micro Express Adaptor is installed the Queue management UI is mounted at
|
103 | `/arena`. Visit it in the browser for handy job management.
|
104 |
|
105 | ### Documentation
|
106 |
|
107 | More info is available in the [DOCS](./DOCS.md)
|