Skip to content

Commit 90f8dae

Browse files
rahulbileTom Kirkpatrick
authored and
Tom Kirkpatrick
committed
fix: setup consumer and producer methods early
* Create method after setting up queue model. * Setup queues after app boot event. * log the request to queue before its been initialised * code cleanup
1 parent 7870634 commit 90f8dae

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

lib/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,23 @@ module.exports = function loopbackComponentMq(app, config) {
1919
// Independent of app boot, add model and ACL
2020
setupModel(app)
2121

22+
// Get a reference to the created model and set up the topology
23+
const Queue = app.models.Queue
24+
25+
RabbitTopology.setupTopology(Queue)
26+
2227
// Wait the app to be booted
2328
app.once('booted', () => {
2429

25-
// Get a reference to the created model
26-
const Queue = app.models.Queue
27-
2830
// Get a reference to the configured datasource
2931
const dsName = _.get(app, 'settings.loopback-component-mq.options.dataSource', 'rabbit')
3032
const ds = app.dataSources[dsName]
3133
const dsOptions = _.get(ds, 'settings.options')
3234

33-
// If we have a datasource, wire up Rabbit and set up the topology
35+
// If we have a datasource, wire up Rabbit and setup Queues
3436
if (ds && dsOptions) {
3537
Queue.rabbit = new Rabbit(dsOptions)
36-
RabbitTopology.setupTopology(Queue)
38+
RabbitTopology.setupQueues(Queue)
3739
}
3840

3941
})

lib/rabbit-topology.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ module.exports = function rabbitTopology() {
1212
msqQueue = QueueModel.rabbit.exchange.queue({ name, durable: true })
1313
}
1414

15+
function setupQueues(Model) {
16+
QueueModel = Model
17+
18+
// Loop through all the defined queues
19+
_.forEach(QueueModel.topology, (handlers, queue) => {
20+
// Setup the actual queue on RabbitMQ
21+
setupQueue(queue)
22+
})
23+
}
24+
1525
function setupQueueConsumer(app, queue, definition) {
1626
debug('setupQueueConsumer')
1727
const modelName = definition.model
@@ -31,7 +41,10 @@ module.exports = function rabbitTopology() {
3141
}
3242

3343
// Start consuming the queue
34-
msqQueue.consume(Method)
44+
if (msqQueue) {
45+
msqQueue.consume(Method)
46+
}
47+
3548

3649
debug('setupQueueConsumer: queue: %s, model: %s, method: %s', queue, modelName, methodName)
3750
}
@@ -49,7 +62,12 @@ module.exports = function rabbitTopology() {
4962

5063
Model[methodName] = function queueProducer(params) {
5164
debug(`${modelName}.${methodName}(%o)`, params)
52-
QueueModel.rabbit.exchange.publish(params, { key: queue })
65+
if (QueueModel.rabbit && QueueModel.rabbit.exchange) {
66+
QueueModel.rabbit.exchange.publish(params, { key: queue })
67+
}
68+
else {
69+
debug('setupQueueProducer: queue %s is not yet initialised', queue)
70+
}
5371
}
5472
}
5573

@@ -59,9 +77,6 @@ module.exports = function rabbitTopology() {
5977
// Loop through all the defined queues
6078
_.forEach(QueueModel.topology, (handlers, queue) => {
6179

62-
// Setup the actual queue on RabbitMQ
63-
setupQueue(queue)
64-
6580
// Setup the consumer of this queue
6681
if (handlers.consumer) {
6782
setupQueueConsumer(QueueModel.app, queue, handlers.consumer)
@@ -77,6 +92,7 @@ module.exports = function rabbitTopology() {
7792

7893
return {
7994
setupQueue,
95+
setupQueues,
8096
setupTopology,
8197
setupQueueProducer,
8298
setupQueueConsumer,

0 commit comments

Comments
 (0)