Skip to content

Commit 6db13b8

Browse files
rahulbileTom Kirkpatrick
authored and
Tom Kirkpatrick
committed
fix: define stub consumer/producer methods before app boots (#7)
1 parent 243ab06 commit 6db13b8

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = function loopbackComponentMq(app, config) {
2222
// Get a reference to the created model and set up the topology
2323
const Queue = app.models.Queue
2424

25-
RabbitTopology.setupTopology(Queue)
25+
RabbitTopology.stubQueueMethod(Queue)
2626

2727
// Wait the app to be booted
2828
app.once('booted', () => {
@@ -36,6 +36,7 @@ module.exports = function loopbackComponentMq(app, config) {
3636
if (ds && dsOptions) {
3737
Queue.rabbit = new Rabbit(dsOptions)
3838
RabbitTopology.setupQueues(Queue)
39+
RabbitTopology.setupTopology(Queue)
3940
}
4041

4142
})

lib/rabbit-topology.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,45 @@ module.exports = function rabbitTopology() {
9090
})
9191
}
9292

93+
function stubQueueMethod(StubmodelModel) {
94+
// Loop through all the defined queues
95+
_.forEach(StubmodelModel.topology, handlers => {
96+
97+
98+
// Setup the consumer of this queue
99+
if (handlers.consumer) {
100+
const modelName = handlers.consumer.model
101+
const methodName = handlers.consumer.method
102+
const Model = StubmodelModel.app.models[modelName]
103+
104+
// Log if the method does not exists yet
105+
Model[methodName] = function stubQueueConsumer() {
106+
debug(`${modelName}.${methodName} is not initialised yet`)
107+
}
108+
}
109+
110+
// Setup the producers of this queue
111+
if (handlers.producer) {
112+
const modelName = handlers.producer.model
113+
const methodName = handlers.producer.method
114+
const Model = StubmodelModel.app.models[modelName]
115+
116+
// Log if the method does not exists yet
117+
Model[methodName] = function stubQueueProducer() {
118+
debug(`${modelName}.${methodName} is not initialised yet`)
119+
}
120+
}
121+
122+
})
123+
124+
}
125+
93126
return {
94127
setupQueue,
95128
setupQueues,
96129
setupTopology,
97130
setupQueueProducer,
98131
setupQueueConsumer,
132+
stubQueueMethod,
99133
}
100134
}

0 commit comments

Comments
 (0)