Skip to content

Commit b67e28d

Browse files
authored
fix(amqp): remove event listener when channel is closed (#203)
1 parent b778f03 commit b67e28d

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

lib/adapters/amqp.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,37 @@ module.exports = (config) => {
3030
queue,
3131
(message) => {
3232
if (message !== null) {
33-
debug(`Got ${key} event from APMQ channel`)
33+
debug(`Got ${key} event from AMQP channel`)
3434
app.emit('sync-in', message.content)
3535
}
3636
},
3737
{ noAck: true }
3838
)
39-
// Publish the received message to the queue
40-
app.on('sync-out', (data) => {
41-
try {
39+
40+
function publishToQueue(data) {
41+
try {
4242
const publishResponse = channel.publish(
4343
key,
4444
queue,
4545
Buffer.from(data)
4646
)
47-
debug(`Publish success: |${publishResponse}| APMQ channel`)
47+
debug(`Publish success: |${publishResponse}| AMQP channel`)
4848
} catch (error) {
49-
debug(`Publish fail: |${error.message}| APMQ channel`)
49+
debug(`Publish fail: |${error.message}| AMQP channel`)
5050
}
51+
}
52+
53+
// Publish the received message to the queue
54+
app.on('sync-out', publishToQueue)
55+
56+
channel.on('close', () => {
57+
debug('Channel closed')
58+
app.off('sync-out', publishToQueue)
5159
})
60+
5261
return channel
5362
} catch (error) {
54-
debug(`Publish fail: |${error.message}| APMQ channel`)
63+
debug(`Publish fail: |${error.message}| AMQP channel`)
5564
}
5665
}
5766
})

0 commit comments

Comments
 (0)