10
10
*
11
11
* amqp.connection: RabbitMQ Connection eg. amqp://guest:guest@localhost:5672
12
12
* amqp.queue: RabbitMQ Exchange eg. metrics
13
+ * ampq.durable: Whether the queue will survive a broker restart
13
14
*
14
15
*/
15
- var util = require ( 'util' ) ;
16
16
17
17
18
18
function RabbitmqBackend ( startupTime , config , emitter ) {
@@ -21,8 +21,6 @@ function RabbitmqBackend(startupTime, config, emitter) {
21
21
this . lastException = startupTime ;
22
22
this . config = config ;
23
23
24
- this . connection = require ( 'amqplib' ) . connect ( this . config . amqp . connection ) ;
25
-
26
24
// attach
27
25
emitter . on ( 'flush' , function ( timestamp , metrics ) {
28
26
self . flush ( timestamp , metrics ) ;
@@ -73,21 +71,21 @@ RabbitmqBackend.prototype.flush = function(timestamp, metrics) {
73
71
74
72
var queue = this . config . amqp . queue ;
75
73
var msg = JSON . stringify ( metric ) ;
74
+ var durable = this . config . amqp . durable ;
76
75
77
- console . log ( 'Sending metrics ' , msg ) ;
76
+ console . log ( 'Attempt to send metrics ' , msg ) ;
78
77
79
78
// Publish
80
- this . connection . then ( function ( conn ) {
81
- if ( this . ch ) {
82
- return this . ch ;
83
- }
84
- this . ch = conn . createChannel ( ) ;
85
- return this . ch ;
86
- } ) . then ( function ( ch ) {
87
- return ch . assertQueue ( queue ) . then ( function ( ok ) {
88
- console . log ( 'Sent ' , msg ) ;
89
- return ch . sendToQueue ( queue , Buffer . from ( msg ) ) ;
90
- } ) ;
79
+ require ( 'amqplib' ) . connect ( this . config . amqp . connection ) . then ( function ( conn ) {
80
+ return conn . createChannel ( ) . then ( function ( ch ) {
81
+ var ok = ch . assertQueue ( queue , { durable : durable } ) ;
82
+
83
+ return ok . then ( function ( _qok ) {
84
+ ch . sendToQueue ( queue , Buffer . from ( msg ) ) ;
85
+ console . log ( "Sent " , msg ) ;
86
+ return ch . close ( ) ;
87
+ } ) ;
88
+ } ) . finally ( function ( ) { conn . close ( ) ; } ) ;
91
89
} ) . catch ( console . warn ) ;
92
90
} ;
93
91
0 commit comments