UNPKG

2.41 kBMarkdownView Raw
1# Knetik Micro Queue
2
3This package adds redis queue support using Bull Queue to any
4[micro-core](https://www.npmjs.com/package/@knetik/micro-core) application
5as a multi tenant adaptor.
6
7When a customer connects to a micro-core application, the required app_id
8is passed into the adaptor initializer and used as the context for any jobs
9created using the micro queue Job class interface.
10
11### Setup
12
13Install the `@knetik/micro-queue` package
14
15```
16yarn add @knetik/micro-queue
17```
18
19Add the Adaptor to your config/environments/{env}.json
20
21```
22"ADAPTORS": [
23 "@knetik/micro-queue"
24]
25```
26
27Add 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
39A file is generated in `app/jobs/{job_name}.job.js`
40
41if the `{job_name}` id `some_long_process`, the contents of `app/jobs/{job_name}.job.js` are:
42
43```
44const { JobBase } = require('@knetik/micro-queue');
45
46module.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.
78module.exports.concurrency = 50;
79```
80
81### Usage
82
83The 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```
87const Job = App.Queue.get('SomeLongProcessJob');
88```
89
90Call `perform_later` on the Job class passing in the job data and the job
91options
92
93```
94const data = { hi: 'there' };
95const options = {};
96
97Job.perform_later(data, options);
98```
99
100### Queue UI
101
102if 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
107More info is available in the [DOCS](./DOCS.md)