1
+ import EventEmitter from "events" ;
1
2
import { RedisClientOptions } from "." ;
2
3
import RedisCommandsQueue from "./commands-queue" ;
3
4
import RedisSocket from "./socket" ;
4
5
5
- export default class EnterpriseMaintenanceManager {
6
- client : any ;
6
+ export default class EnterpriseMaintenanceManager extends EventEmitter {
7
7
commandsQueue : RedisCommandsQueue ;
8
8
options : RedisClientOptions ;
9
9
constructor (
10
- client : any ,
11
10
commandsQueue : RedisCommandsQueue ,
12
11
options : RedisClientOptions ,
13
12
) {
14
- this . client = client ;
13
+ super ( ) ;
15
14
this . commandsQueue = commandsQueue ;
16
15
this . options = options ;
17
16
18
17
this . commandsQueue . events . on ( "moving" , this . #onMoving) ;
19
18
}
20
19
21
- // Queue
20
+ // Queue:
22
21
// toWrite [ C D E ]
23
22
// waitingForReply [ A B ]
24
23
//
@@ -27,7 +26,7 @@ export default class EnterpriseMaintenanceManager {
27
26
// 1. [EVENT] MOVING PN received
28
27
// 2. [ACTION] Pause writing ( we need to wait for new socket to connect and for all in-flight commands to complete )
29
28
// 3. [EVENT] New socket connected
30
- // 4. [EVENT] In-flight commands completed
29
+ // 4. [EVENT] WaitingForReply commands completed
31
30
// 5. [ACTION] Destroy old socket
32
31
// 6. [ACTION] Resume writing -> we are going to write to the new socket from now on
33
32
#onMoving = async (
@@ -38,7 +37,7 @@ export default class EnterpriseMaintenanceManager {
38
37
// 1 [EVENT] MOVING PN received
39
38
console . log ( '[EnterpriseMaintenanceManager] Pausing client' ) ;
40
39
// 2 [ACTION] Pause writing
41
- this . client . pause ( ) ;
40
+ this . emit ( 'pause' )
42
41
43
42
console . log ( `[EnterpriseMaintenanceManager] Creating new socket for ${ host } :${ port } ` ) ;
44
43
const newSocket = new RedisSocket ( {
@@ -65,20 +64,10 @@ export default class EnterpriseMaintenanceManager {
65
64
} ) ;
66
65
}
67
66
} ) ;
68
- // 4 [EVENT] Reply queue now empty
67
+ // 4 [EVENT] WaitingForReply commands completed
69
68
70
- // 5 [ACTION] Destroy old socket
71
- // Switch to the new socket and clean up the old one
72
- console . log ( '[EnterpriseMaintenanceManager] Switching to new socket and cleaning up old one' ) ;
73
- const oldSocket = this . client . socket ;
74
- this . client . socket = newSocket ;
75
- oldSocket . removeAllListeners ( ) ;
76
- oldSocket . destroy ( ) ;
77
- console . log ( '[EnterpriseMaintenanceManager] Old socket destroyed' ) ;
69
+ // 5 + 6
70
+ this . emit ( 'resume' , newSocket ) ;
78
71
79
- // 6 [ACTION] Resume writing
80
- console . log ( '[EnterpriseMaintenanceManager] Resuming client' ) ;
81
- this . client . resume ( ) ;
82
- console . log ( '[EnterpriseMaintenanceManager] Socket migration complete' ) ;
83
72
} ;
84
73
}
0 commit comments