@@ -4,7 +4,7 @@ import { EventEmitter } from 'events';
4
4
import { hostname } from 'os' ;
5
5
import { AwaitableSender , Connection , ConnectionEvents , Container , EventContext , Receiver , ReceiverEvents , SenderEvents } from 'rhea-promise' ;
6
6
7
- import { AMQP_CONNECTION_RECONNECT } from '../constants' ;
7
+ import { AMQP_CONNECTION_DISCONNECTED , AMQP_CONNECTION_RECONNECT } from '../constants' ;
8
8
import { AMQPModuleOptions , CreateReceiverOptions , CreateSenderOptions } from '../interfaces' ;
9
9
import { ErrorMessage , getConnectionToken , getLogger , parseURL } from '../utils' ;
10
10
@@ -46,6 +46,10 @@ export class AMQPService {
46
46
connection . on ( ConnectionEvents . disconnected , ( context : EventContext ) => {
47
47
const error = [ `Connection closed by peer: ${ connectionToken } ` , ErrorMessage . fromContext ( context ) ] ;
48
48
this . logger . warn ( ...error . filter ( e => e ) ) ;
49
+ const emitted = AMQPService . eventEmitter . emit ( AMQP_CONNECTION_DISCONNECTED ) ;
50
+ if ( ! emitted ) {
51
+ this . logger . warn ( 'disconnect event not emitted' ) ;
52
+ }
49
53
} ) ;
50
54
connection . on ( ConnectionEvents . connectionClose , ( context : EventContext ) => {
51
55
const error = `Connection closed: ${ connectionToken } ` ;
@@ -54,6 +58,22 @@ export class AMQPService {
54
58
} else {
55
59
this . logger . warn ( error ) ;
56
60
}
61
+ const timeoutHandler = setTimeout ( async ( ) => {
62
+ ( context . connection as any ) . _connection . dispatch ( ConnectionEvents . disconnected , void 0 ) ;
63
+ await context . connection
64
+ . open ( )
65
+ . then ( ( ) => {
66
+ this . logger . silly ( 'connection successfully reopened' ) ;
67
+ const emitted = AMQPService . eventEmitter . emit ( AMQP_CONNECTION_RECONNECT ) ;
68
+ if ( ! emitted ) {
69
+ this . logger . warn ( 'reconnect event not emitted' ) ;
70
+ }
71
+ } )
72
+ . catch ( error => {
73
+ this . logger . error ( `reopening connection failed with error: ${ error . message } ` , error ) ;
74
+ } ) ;
75
+ clearTimeout ( timeoutHandler ) ;
76
+ } , 1000 ) ;
57
77
} ) ;
58
78
try {
59
79
await connection . open ( ) ;
0 commit comments