onjs 2.3.5 → 2.4.1
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/README.md +33 -12
- package/bin/prepare-test-environment +1 -1
- package/lib/on.js +17 -4
- package/package.json +3 -3
- package/tests/tests/queues.js +3 -1
- package/tests/tests/rpcs.js +4 -1
- package/tests/tests/test.js +4 -1
- package/tests/tests/topics.js +4 -1
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ Processing means that the task will be added to the queue and processed as soon
|
|
|
28
28
|
|
|
29
29
|
Dispatching means that the task will be added to the queue but instead of being processed, it will be sent to MQ with another name.
|
|
30
30
|
|
|
31
|
-
```
|
|
31
|
+
```javascript
|
|
32
32
|
on
|
|
33
33
|
.eventReceived(eventName: string)
|
|
34
34
|
.withProperties(properties: array)
|
|
@@ -38,7 +38,7 @@ on
|
|
|
38
38
|
|
|
39
39
|
Broadcasting means that the task will immediately be executed by ALL the listeners without using redis, the tasks will be "duplicated" across all listeners.
|
|
40
40
|
|
|
41
|
-
```
|
|
41
|
+
```javascript
|
|
42
42
|
on
|
|
43
43
|
.broadcastReceived(eventName: string)
|
|
44
44
|
.do(callback: function)
|
|
@@ -48,7 +48,7 @@ on
|
|
|
48
48
|
|
|
49
49
|
#### Processing
|
|
50
50
|
|
|
51
|
-
```
|
|
51
|
+
```javascript
|
|
52
52
|
on
|
|
53
53
|
.eventReceived(eventName: string)
|
|
54
54
|
.withProperties(properties: array)
|
|
@@ -59,7 +59,7 @@ Any event received with the name registered on ```eventReceived``` will be added
|
|
|
59
59
|
|
|
60
60
|
#### Dispatching
|
|
61
61
|
|
|
62
|
-
```
|
|
62
|
+
```javascript
|
|
63
63
|
on
|
|
64
64
|
.eventReceived(eventName: string)
|
|
65
65
|
.withProperties(properties: array)
|
|
@@ -74,7 +74,7 @@ The pattern of listening to work queues is described at https://www.rabbitmq.com
|
|
|
74
74
|
|
|
75
75
|
This are tasks that will be executed by a queue
|
|
76
76
|
|
|
77
|
-
```
|
|
77
|
+
```javascript
|
|
78
78
|
on
|
|
79
79
|
.taskReceived(eventName: string)
|
|
80
80
|
.withProperties(properties: array)
|
|
@@ -87,7 +87,7 @@ This type of queues require the rabbitmq-delayed-message-exchange plugin
|
|
|
87
87
|
|
|
88
88
|
The task will arrive at the queue with a set delay
|
|
89
89
|
|
|
90
|
-
```
|
|
90
|
+
```javascript
|
|
91
91
|
on
|
|
92
92
|
.taskReceived(eventName: string, options: { isDelayed: boolean})
|
|
93
93
|
.withProperties(properties: array)
|
|
@@ -99,7 +99,7 @@ The pattern of listening to RPC requests described at https://www.rabbitmq.com/t
|
|
|
99
99
|
|
|
100
100
|
On.js will order MQ to start listening for events registered and the callback registered will be executed by MQ and the results sent to the client who requested the data.
|
|
101
101
|
|
|
102
|
-
```
|
|
102
|
+
```javascript
|
|
103
103
|
on
|
|
104
104
|
.eventReceived(eventName: string)
|
|
105
105
|
.withProperties(properties: array)
|
|
@@ -113,7 +113,7 @@ The response will be a JSON string so you will have to parse it. The type of res
|
|
|
113
113
|
|
|
114
114
|
To initialize initialize a new instance and then call .init()
|
|
115
115
|
|
|
116
|
-
```
|
|
116
|
+
```javascript
|
|
117
117
|
var options = {
|
|
118
118
|
redis: {
|
|
119
119
|
host: REDIS_HOST,
|
|
@@ -132,9 +132,30 @@ var on = new On(options)
|
|
|
132
132
|
on.init()
|
|
133
133
|
```
|
|
134
134
|
|
|
135
|
-
|
|
135
|
+
### reconnecting
|
|
136
136
|
|
|
137
|
+
By default onjs will try to reconnect to rabbitmq if the connection is closed, including when using the MQ library to close a connection directly, if you need to tear down use onjs.tearDown() function, or you can disable this behavior by setting the config option `reconnectOnClose` to false
|
|
138
|
+
|
|
139
|
+
```javascript
|
|
140
|
+
const options = {
|
|
141
|
+
redis: {
|
|
142
|
+
host: REDIS_HOST,
|
|
143
|
+
port: REDIS_PORT
|
|
144
|
+
},
|
|
145
|
+
mq: {
|
|
146
|
+
url: RABBITMQ_URL,
|
|
147
|
+
exchange_name: EXCHANGE_NAME
|
|
148
|
+
},
|
|
149
|
+
reconnectOnClose: false,
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const on = new On(options)
|
|
153
|
+
on.init()
|
|
137
154
|
```
|
|
155
|
+
|
|
156
|
+
## Usage Example
|
|
157
|
+
|
|
158
|
+
```javascript
|
|
138
159
|
var RabbitMQ = require('rabbitmq-lib')
|
|
139
160
|
var On = require('./lib/on/on.js')
|
|
140
161
|
|
|
@@ -203,13 +224,13 @@ on.init()
|
|
|
203
224
|
# Debugging and Logging
|
|
204
225
|
Running
|
|
205
226
|
|
|
206
|
-
```
|
|
227
|
+
```javascript
|
|
207
228
|
on.debug()
|
|
208
229
|
```
|
|
209
230
|
|
|
210
231
|
will enable all logging. If you only want to enable one particular event to have logging, you can do it as well by calling the .log method on a particular event:
|
|
211
232
|
|
|
212
|
-
```
|
|
233
|
+
```javascript
|
|
213
234
|
on
|
|
214
235
|
.requestReceived('someRequest')
|
|
215
236
|
.withProperties(['userId'])
|
|
@@ -222,7 +243,7 @@ on
|
|
|
222
243
|
# MQ Dependency
|
|
223
244
|
The module has a dependency on a MQ class that handles the communication with MQ. In theory any kind of service with MQ should work as long as the communication with MQ is abstracted. The methods needed in the constructor are:
|
|
224
245
|
|
|
225
|
-
```
|
|
246
|
+
```javascript
|
|
226
247
|
connect()
|
|
227
248
|
disconnect()
|
|
228
249
|
publish(eventName, data)
|
|
@@ -9,7 +9,7 @@ REDIS_CONTAINER_PORT=19379
|
|
|
9
9
|
|
|
10
10
|
function createRabbitContainer() {
|
|
11
11
|
id=$(docker run -d --name ${RABBIT_CONTAINER_NAME} \
|
|
12
|
-
-p 5672 \
|
|
12
|
+
-p 5672:5272 \
|
|
13
13
|
-e RABBITMQ_ERLANG_COOKIE=SWQOKODSQALRPCLNMEQG \
|
|
14
14
|
-e RABBITMQ_DEFAULT_USER=rabbitmq \
|
|
15
15
|
-e RABBITMQ_DEFAULT_PASS=rabbitmq \
|
package/lib/on.js
CHANGED
|
@@ -27,6 +27,7 @@ class On extends EventEmitter {
|
|
|
27
27
|
this.currentEventTypeToRegister = null
|
|
28
28
|
this.debugEnabled = false
|
|
29
29
|
this.delayedExchangeNameIsEmpty = !options.mq.delayed_exchange_name && options.mq.delayed_exchange_name !== ''
|
|
30
|
+
this.reconnectOnClose = options.reconnectOnClose ?? true
|
|
30
31
|
|
|
31
32
|
return this
|
|
32
33
|
}
|
|
@@ -66,6 +67,10 @@ class On extends EventEmitter {
|
|
|
66
67
|
this._log('info', 'Connected to MQ, initializing topics')
|
|
67
68
|
})
|
|
68
69
|
|
|
70
|
+
this.mqInstance.on('connectionAttempt', async (attempt) => {
|
|
71
|
+
this._log('info', `MQ Connection attempt number ${attempt}`)
|
|
72
|
+
})
|
|
73
|
+
|
|
69
74
|
this.mqInstance.on('reconnected', async (connection) => {
|
|
70
75
|
this.setConnection(connection)
|
|
71
76
|
this._log('info', 'ReConnected to MQ, initializing topics')
|
|
@@ -77,6 +82,13 @@ class On extends EventEmitter {
|
|
|
77
82
|
throw err
|
|
78
83
|
})
|
|
79
84
|
|
|
85
|
+
this.mqInstance.on('close', async (err) => {
|
|
86
|
+
if (this.reconnectOnClose) {
|
|
87
|
+
this._log('error', 'MQ Connection closed attempting to reconnect', err)
|
|
88
|
+
await this.mqInstance.reconnect()
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
|
|
80
92
|
this._log('info', 'Connecting to RabbitMQ')
|
|
81
93
|
var connection = await this.mqInstance.connect()
|
|
82
94
|
this.setConnection(connection)
|
|
@@ -145,6 +157,7 @@ class On extends EventEmitter {
|
|
|
145
157
|
async tearDown () {
|
|
146
158
|
try {
|
|
147
159
|
this._log('info', 'Tearing down on.js. Closing connection and stopped listening events')
|
|
160
|
+
this.reconnectOnClose = false
|
|
148
161
|
this.removeAllListeners()
|
|
149
162
|
var connection = this.getConnection()
|
|
150
163
|
return await connection.close()
|
|
@@ -270,13 +283,13 @@ class On extends EventEmitter {
|
|
|
270
283
|
|
|
271
284
|
async _initRPCRequests (requests) {
|
|
272
285
|
try {
|
|
273
|
-
this._log('info', 'Listening for rpc requests'
|
|
286
|
+
this._log('info', 'Listening for rpc requests')
|
|
274
287
|
|
|
275
288
|
for (let request of requests) {
|
|
276
289
|
let rpcInstance = new RPC()
|
|
277
290
|
let channel = await this.mqInstance.createChannel()
|
|
278
291
|
rpcInstance.setChannel(channel)
|
|
279
|
-
this._log('info', `Listening rpc request ${request.eventName}
|
|
292
|
+
this._log('info', `Listening rpc request ${request.eventName}`)
|
|
280
293
|
await rpcInstance.listen(request.eventName, request.actions[0])
|
|
281
294
|
}
|
|
282
295
|
|
|
@@ -561,9 +574,9 @@ class On extends EventEmitter {
|
|
|
561
574
|
}
|
|
562
575
|
}
|
|
563
576
|
|
|
564
|
-
_log (type, message) {
|
|
577
|
+
_log (type, message, metadata) {
|
|
565
578
|
if (this.debugEnabled) {
|
|
566
|
-
return this.logger[type](message)
|
|
579
|
+
return this.logger[type](message, metadata)
|
|
567
580
|
}
|
|
568
581
|
}
|
|
569
582
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "onjs",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.1",
|
|
4
4
|
"description": "On.js is an event library based on RabbitMQ that helps you control events on a microservices arquitecture",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"mocha": "^5.2.0"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"rabbitmq-lib": "^2.2
|
|
32
|
-
"amqplib": "^0.10.
|
|
31
|
+
"rabbitmq-lib": "^2.3.2",
|
|
32
|
+
"amqplib": "^0.10.5",
|
|
33
33
|
"bull": "^3.29.3"
|
|
34
34
|
}
|
|
35
35
|
}
|
package/tests/tests/queues.js
CHANGED
|
@@ -15,11 +15,13 @@ describe('Queue', function () {
|
|
|
15
15
|
mq: {
|
|
16
16
|
exchange_name: 'onjs_test',
|
|
17
17
|
url: 'amqp://rabbitmq:rabbitmq@rabbitmq:5672/',
|
|
18
|
+
connectMaxAttempts: 1,
|
|
19
|
+
delayMS: 10,
|
|
18
20
|
},
|
|
19
21
|
redis: {
|
|
20
22
|
port: REDIS_PORT,
|
|
21
23
|
host: 'redis'
|
|
22
|
-
}
|
|
24
|
+
},
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
var mq1 = new MQ(config.mq)
|
package/tests/tests/rpcs.js
CHANGED
|
@@ -14,11 +14,14 @@ describe('RPC', function () {
|
|
|
14
14
|
mq: {
|
|
15
15
|
exchange_name: 'onjs_test',
|
|
16
16
|
url: 'amqp://rabbitmq:rabbitmq@rabbitmq:5672/',
|
|
17
|
+
connectMaxAttempts: 1,
|
|
18
|
+
delayMS: 10,
|
|
17
19
|
},
|
|
18
20
|
redis: {
|
|
19
21
|
port: REDIS_PORT,
|
|
20
22
|
host: 'redis'
|
|
21
|
-
}
|
|
23
|
+
},
|
|
24
|
+
reconnectOnClose: false,
|
|
22
25
|
}
|
|
23
26
|
|
|
24
27
|
var mq = new MQ(config.mq)
|
package/tests/tests/test.js
CHANGED
|
@@ -18,11 +18,14 @@ describe('On.js', function () {
|
|
|
18
18
|
mq: {
|
|
19
19
|
exchange_name: 'onjs_test',
|
|
20
20
|
url: 'amqp://rabbitmq:rabbitmq@rabbitmq:5672/',
|
|
21
|
+
connectMaxAttempts: 1,
|
|
22
|
+
delayMS: 10,
|
|
21
23
|
},
|
|
22
24
|
redis: {
|
|
23
25
|
port: REDIS_PORT,
|
|
24
26
|
host: 'redis'
|
|
25
|
-
}
|
|
27
|
+
},
|
|
28
|
+
reconnectOnClose: false,
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
describe('Topics registration', function () {
|
package/tests/tests/topics.js
CHANGED
|
@@ -13,11 +13,14 @@ describe('Topics', function () {
|
|
|
13
13
|
mq: {
|
|
14
14
|
exchange_name: 'onjs_test',
|
|
15
15
|
url: 'amqp://rabbitmq:rabbitmq@rabbitmq:5672/',
|
|
16
|
+
connectMaxAttempts: 1,
|
|
17
|
+
delayMS: 10,
|
|
16
18
|
},
|
|
17
19
|
redis: {
|
|
18
20
|
port: REDIS_PORT,
|
|
19
21
|
host: 'redis'
|
|
20
|
-
}
|
|
22
|
+
},
|
|
23
|
+
reconnectOnClose: false,
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
var mq1 = new MQ(config.mq)
|