@@ -21,7 +21,7 @@ function errorHandler(err) {
2121}
2222
2323var connect = ( function connectWrapper ( ) {
24- var connectionPromise , connection , isConnected = false ;
24+ var connectionPromise , connection , isConnected = false , connectOpts = { } ;
2525
2626 function ready ( ) {
2727 isConnected = true ;
@@ -41,16 +41,29 @@ var connect = (function connectWrapper() {
4141 function checkConnected ( ) {
4242 if ( ! isConnected ) {
4343 connectionError ( new Error ( "Connection closed prematurely, please check your credentials" ) ) ;
44+ } else if ( connectOpts . reconnect ) {
45+ // monkey patch b/c node-amqp seems to be not maintained anymore based
46+ // upon number of open issues, comments within issues stating it is no
47+ // longer maintained and the age of open pull requests. node-amqp only
48+ // publishes an error on the "end" event if the "ready" event has not
49+ // yet been fired which means you probably have an issue authing with
50+ // Rabbit. They should also have an else block and look to see if the
51+ // socket is destroyed and if so, emit an error to which node-amqp
52+ // will attempt to reconnect using the specified strategy.
53+ connection . emit ( "error" , new Error ( "Connection closed, attempting to reconnect" ) ) ;
4454 }
4555 }
4656
4757 return function connect ( url , opts ) {
4858 if ( ! connectionPromise ) {
59+ connectOpts = opts ;
4960 connectionPromise = new Promise ( ) ;
5061 connection = amqp . createConnection ( url , opts ) ;
5162 connection . once ( 'ready' , ready ) ;
5263 connection . once ( 'error' , connectionError ) ;
53- connection . once ( "close" , checkConnected ) ;
64+ // use on instead of once for `close` so we can handle multiple
65+ // subsequent disconnects.
66+ connection . on ( "close" , checkConnected ) ;
5467 }
5568 return connectionPromise ;
5669 } ;
@@ -150,7 +163,8 @@ var Hare = comb.define(_Options, {
150163
151164 "static" : {
152165 OPTIONS : [ "defaultExchangeName" , "reconnect" , "reconnectBackoffStrategy" ,
153- "reconnectExponentialLimit" , "reconnectExponentialLimit" , "reconnectBackoffTime" ]
166+ "reconnectExponentialLimit" , "reconnectExponentialLimit" , "reconnectBackoffTime" ,
167+ "heartbeatForceReconnect" ]
154168 }
155169
156170} ) ;
0 commit comments