@steedos/moleculer-bullmq 3.0.0-beta.118
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +90 -0
- package/package.json +57 -0
- package/src/index.js +123 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 LuxChan S.A R.L.-S
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# moleculer-bullmq
|
|
2
|
+
|
|
3
|
+
[](https://coveralls.io/github/LuxChanLu/moleculer-bullmq?branch=master)
|
|
4
|
+
[](https://www.codacy.com/app/LuxChanLu/moleculer-bullmq?utm_source=github.com&utm_medium=referral&utm_content=LuxChanLu/moleculer-bullmq&utm_campaign=Badge_Grade)
|
|
5
|
+
[](https://codeclimate.com/github/LuxChanLu/moleculer-bullmq/maintainability)
|
|
6
|
+

|
|
7
|
+
[](https://www.npmjs.com/package/moleculer-bullmq)
|
|
8
|
+
|
|
9
|
+
## How to create job
|
|
10
|
+
You just need to add the `BullMqMixin` and add a `queue` attribute to you action.
|
|
11
|
+
This action will be call with the params & meta of the scheduler.
|
|
12
|
+
The return of the action will be the job result.
|
|
13
|
+
The mixin will add the BullMq `job` into `locals`
|
|
14
|
+
```js
|
|
15
|
+
module.exports = {
|
|
16
|
+
name: 'jobs',
|
|
17
|
+
mixins: [BullMqMixin],
|
|
18
|
+
settings: {
|
|
19
|
+
bullmq: {
|
|
20
|
+
worker: { concurrency: 50 }
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
actions: {
|
|
24
|
+
resize: {
|
|
25
|
+
queue: true,
|
|
26
|
+
params: { width: 'number', height: 'number' },
|
|
27
|
+
async handler(ctx) {
|
|
28
|
+
const { width, height } = ctx.params
|
|
29
|
+
const { user } = ctx.meta
|
|
30
|
+
ctx.locals.job.updateProgress(100)
|
|
31
|
+
return { user, size: width * height, job: ctx.locals.job.id }
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
By default it use the redis cacher, but you can specify a custom client :
|
|
38
|
+
```js
|
|
39
|
+
module.exports = {
|
|
40
|
+
name: 'jobs',
|
|
41
|
+
mixins: [BullMqMixin],
|
|
42
|
+
settings: {
|
|
43
|
+
bullmq: {
|
|
44
|
+
client: 'redis://:authpassword@127.0.0.1:6380/4'
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
The `client` option goes to the `IORedis` constructor.
|
|
49
|
+
```
|
|
50
|
+
## How to queue job
|
|
51
|
+
You can use the `queue` method, with five parameters : Current context, Queue name, Action name, Parameters, Job options
|
|
52
|
+
```js
|
|
53
|
+
module.exports = {
|
|
54
|
+
name: 'my.service',
|
|
55
|
+
mixins: [BullMqMixin],
|
|
56
|
+
actions: {
|
|
57
|
+
'resize.async': {
|
|
58
|
+
async handler(ctx) {
|
|
59
|
+
ctx.meta.user = 'Bob de glace'
|
|
60
|
+
const job = await this.queue(ctx, 'jobs', 'resize', { width: 42, height: 42 }, { priority: 10 })
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
If your in the same service as your scheduling action, you can omit the queue name with the `localQueue` method
|
|
67
|
+
```js
|
|
68
|
+
module.exports = {
|
|
69
|
+
name: 'my.service',
|
|
70
|
+
mixins: [BullMqMixin],
|
|
71
|
+
actions: {
|
|
72
|
+
resize: {
|
|
73
|
+
queue: true,
|
|
74
|
+
params: { width: 'number', height: 'number' },
|
|
75
|
+
async handler(ctx) {
|
|
76
|
+
const { width, height } = ctx.params
|
|
77
|
+
const { user } = ctx.meta
|
|
78
|
+
ctx.locals.job.updateProgress(100)
|
|
79
|
+
return { user, size: width * height, job: ctx.locals.job.id }
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
'resize.async': {
|
|
83
|
+
async handler(ctx) {
|
|
84
|
+
ctx.meta.user = 'Bob de glace'
|
|
85
|
+
const job = await this.localQueue(ctx, 'resize', { width: 42, height: 42 }, { priority: 10 })
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@steedos/moleculer-bullmq",
|
|
3
|
+
"version": "3.0.0-beta.118",
|
|
4
|
+
"description": "Moleculer service for bullmq",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"directories": {
|
|
7
|
+
"test": "test"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"ci": "jest --watch",
|
|
11
|
+
"test": "jest",
|
|
12
|
+
"lint": "eslint --ext=.js src test index.js"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/LuxChanLu/moleculer-bullmq.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"moleculer",
|
|
20
|
+
"queue",
|
|
21
|
+
"bullmq"
|
|
22
|
+
],
|
|
23
|
+
"author": "Hugo Meyronneinc <hugo@lxc.lu>",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/LuxChanLu/moleculer-bullmq/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/LuxChanLu/moleculer-bullmq#readme",
|
|
29
|
+
"jest": {
|
|
30
|
+
"testEnvironment": "node",
|
|
31
|
+
"coveragePathIgnorePatterns": [
|
|
32
|
+
"/node_modules/",
|
|
33
|
+
"/test/"
|
|
34
|
+
]
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">= 18.x.x"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"bullmq": "^5.62.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/jest": "^29.2.4",
|
|
47
|
+
"coveralls": "^3.1.1",
|
|
48
|
+
"eslint": "^8.30.0",
|
|
49
|
+
"eslint-plugin-jest": "^27.1.7",
|
|
50
|
+
"jest": "^29.3.1",
|
|
51
|
+
"jest-cli": "^29.3.1",
|
|
52
|
+
"moleculer": "^0.14.27",
|
|
53
|
+
"redis-mock": "^0.56.3",
|
|
54
|
+
"wait-for-expect": "^3.0.2"
|
|
55
|
+
},
|
|
56
|
+
"gitHead": "7511b2250673e97b6b7676aa46c93bd6cb49fd04"
|
|
57
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* moleculer-bullmq
|
|
3
|
+
* Copyright (c) 2022 LuxChan S.A R.L.-S (https://github.com/LuxChanLu/moleculer-bullmq)
|
|
4
|
+
* MIT Licensed
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict'
|
|
8
|
+
|
|
9
|
+
const { Worker, Queue, QueueEvents } = require('bullmq')
|
|
10
|
+
|
|
11
|
+
module.exports = {
|
|
12
|
+
settings: {
|
|
13
|
+
bullmq: {
|
|
14
|
+
worker: {}
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
hooks: {
|
|
18
|
+
before: {
|
|
19
|
+
'*': '$populateJob'
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
created() {
|
|
23
|
+
this.$queues = Object.entries(this.schema.actions || {}).filter(([, { queue }]) => queue).map(([name]) => name)
|
|
24
|
+
this.$queueResolved = {}
|
|
25
|
+
this.$connection = this.settings.bullmq.client ? this.settings.bullmq.client : process.env.B6_CLUSTER_TRANSPORTER
|
|
26
|
+
},
|
|
27
|
+
started() {
|
|
28
|
+
if (this.$queues.length > 0) {
|
|
29
|
+
this.$worker = new Worker(this.$queueName(), async job => {
|
|
30
|
+
const { params, meta, parentSpan } = job.data
|
|
31
|
+
meta.job = { id: job.id, queue: this.$queueName() }
|
|
32
|
+
return this.broker.call(`${this.$queueName()}.${job.name}`, params, { meta, timeout: 0, parentSpan })
|
|
33
|
+
}, { ...this.settings.bullmq.worker, connection: this.$connection })
|
|
34
|
+
this.$events = new QueueEvents(this.$queueName(), { connection: this.$connection })
|
|
35
|
+
this.$events.on('active', ({ jobId }) => this.$transformEvent(jobId, 'active'))
|
|
36
|
+
this.$events.on('removed', ({ jobId }) => this.$transformEvent(jobId, 'removed'))
|
|
37
|
+
this.$events.on('progress', ({ jobId, data }) => this.$transformEvent(jobId, 'progress', { progress: data }))
|
|
38
|
+
this.$events.on('failed', ({ jobId }) => this.$transformEvent(jobId, 'failed'))
|
|
39
|
+
this.$events.on('error', ({ jobId }) => this.$transformEvent(jobId, 'error'))
|
|
40
|
+
this.$events.on('completed', ({ jobId }) => this.$transformEvent(jobId, 'completed'))
|
|
41
|
+
this.$events.on('drained', () => this.$transformEvent('drained'))
|
|
42
|
+
this.$events.on('paused', () => this.$transformEvent('paused'))
|
|
43
|
+
this.$events.on('resumed', () => this.$transformEvent('resumed'))
|
|
44
|
+
this.$events.on('cleaned', (jobs, type) => this.$transformEvent(null, 'cleaned', { jobs: jobs.map(({ id }) => id), type }))
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
async stopped() {
|
|
48
|
+
if (this.$worker) {
|
|
49
|
+
await this.$worker.close()
|
|
50
|
+
}
|
|
51
|
+
if (this.$events) {
|
|
52
|
+
await this.$events.close()
|
|
53
|
+
}
|
|
54
|
+
await Promise.all(Object.values(this.$queueResolved).map(queue => queue.close()))
|
|
55
|
+
},
|
|
56
|
+
methods: {
|
|
57
|
+
$queueName() {
|
|
58
|
+
if (this.version != null && !(this.settings || {}).$noVersionPrefix) {
|
|
59
|
+
return `v${this.version}.${this.name}`
|
|
60
|
+
}
|
|
61
|
+
return this.name
|
|
62
|
+
},
|
|
63
|
+
$resolve(name) {
|
|
64
|
+
if (!this.$queueResolved[name]) {
|
|
65
|
+
this.$queueResolved[name] = new Queue(name, { connection: this.$connection })
|
|
66
|
+
}
|
|
67
|
+
return this.$queueResolved[name]
|
|
68
|
+
},
|
|
69
|
+
pause(name) {
|
|
70
|
+
if (name) {
|
|
71
|
+
return this.$resolve(name).pause()
|
|
72
|
+
} else if (this.$worker) {
|
|
73
|
+
return this.$worker.pause()
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
resume(name) {
|
|
77
|
+
if (name) {
|
|
78
|
+
return this.$resolve(name).resume()
|
|
79
|
+
} else if (this.$worker) {
|
|
80
|
+
return this.$worker.resume()
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
queue(ctx, name, action, params, options) {
|
|
84
|
+
return this.$resolve(name).add(action, {
|
|
85
|
+
params, meta: ctx.meta, parentSpan: {
|
|
86
|
+
id: ctx.parentID, traceID: ctx.requestID, sampled: ctx.tracing
|
|
87
|
+
}
|
|
88
|
+
}, options)
|
|
89
|
+
},
|
|
90
|
+
localQueue(ctx, action, params, options) {
|
|
91
|
+
return this.queue(ctx, this.$queueName(), action, params, options)
|
|
92
|
+
},
|
|
93
|
+
job(name, id) {
|
|
94
|
+
if (arguments.length === 1) {
|
|
95
|
+
id = name
|
|
96
|
+
name = this.$queueName()
|
|
97
|
+
}
|
|
98
|
+
return this.$resolve(name).getJob(id)
|
|
99
|
+
},
|
|
100
|
+
async $populateJob(ctx) {
|
|
101
|
+
ctx.locals.job = ctx.meta && ctx.meta.job ? await this.job(ctx.meta.job.queue, ctx.meta.job.id) : undefined
|
|
102
|
+
},
|
|
103
|
+
async $transformEvent(id, type, params) {
|
|
104
|
+
const event = arguments.length === 1 ? [id] : [type]
|
|
105
|
+
let emitOpts = { meta: { job: { queue: this.$queueName() } } }
|
|
106
|
+
if (arguments.length >= 2 && id) {
|
|
107
|
+
const job = await this.job(id)
|
|
108
|
+
if (job && job.name) {
|
|
109
|
+
event.unshift(job.name)
|
|
110
|
+
const { meta, parentSpan } = job.data
|
|
111
|
+
meta.job = { id: job.id, queue: this.$queueName() }
|
|
112
|
+
emitOpts = { meta, parentSpan }
|
|
113
|
+
}
|
|
114
|
+
params = params || {}
|
|
115
|
+
params.id = id
|
|
116
|
+
}
|
|
117
|
+
const name = event.join('.')
|
|
118
|
+
|
|
119
|
+
this.broker.emit(`${this.$queueName()}.${name}`, params, emitOpts)
|
|
120
|
+
this.broker.emit(name, params, this.$queueName(), { ...emitOpts, groups: this.$queueName() })
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|