-
Notifications
You must be signed in to change notification settings - Fork 390
/
Copy pathmasterwatcher.ts
138 lines (118 loc) · 3.96 KB
/
masterwatcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { getLogger } from 'pinus-logger'; let logger = getLogger('pinus', __filename);
import * as utils from '../util/utils';
import * as Constants from '../util/constants';
import { Watchdog} from '../master/watchdog';
import { Application } from '../application';
import { IModule, ConsoleService } from 'pinus-admin';
import { MasterAgent, MasterCallback } from '../../index';
export class MasterWatcherModule implements IModule
{
app: Application;
service: ConsoleService;
id: string;
watchdog: Watchdog
static moduleId = Constants.KEYWORDS.MASTER_WATCHER;
constructor(opts : {app:Application}, consoleService : ConsoleService)
{
this.app = opts.app;
this.service = consoleService;
this.id = this.app.getServerId();
this.watchdog = new Watchdog(this.app, this.service);
this.service.on('register', this.onServerAdd.bind(this));
this.service.on('disconnect', this.onServerLeave.bind(this));
this.service.on('reconnect', this.onServerReconnect.bind(this));
};
// ----------------- bind methods -------------------------
onServerAdd(record : any)
{
logger.debug('masterwatcher receive add server event, with server: %j', record);
if (!record || record.type === 'client' || !record.serverType)
{
return;
}
this.watchdog.addServer(record);
};
onServerReconnect(record : any)
{
logger.debug('masterwatcher receive reconnect server event, with server: %j', record);
if (!record || record.type === 'client' || !record.serverType)
{
logger.warn('onServerReconnect receive wrong message: %j', record);
return;
}
this.watchdog.reconnectServer(record);
};
onServerLeave(id : string, type : string)
{
logger.debug('masterwatcher receive remove server event, with server: %s, type: %s', id, type);
if (!id)
{
logger.warn('onServerLeave receive server id is empty.');
return;
}
if (type !== 'client')
{
this.watchdog.removeServer(id);
}
};
// ----------------- module methods -------------------------
start(cb : ()=>void)
{
utils.invokeCallback(cb);
};
masterHandler(agent : MasterAgent, msg : any, cb : MasterCallback)
{
if (!msg)
{
logger.warn('masterwatcher receive empty message.');
return;
}
let func = (masterMethods as any)[msg.action];
if (!func)
{
logger.info('masterwatcher unknown action: %j', msg.action);
return;
}
func(this, agent, msg, cb);
};
}
// ----------------- monitor request methods -------------------------
let subscribe = function (module : MasterWatcherModule, agent : MasterAgent, msg : any, cb : MasterCallback)
{
if (!msg)
{
utils.invokeCallback(cb, new Error('masterwatcher subscribe empty message.'));
return;
}
module.watchdog.subscribe(msg.id);
utils.invokeCallback(cb, null, module.watchdog.query());
};
let unsubscribe = function (module : MasterWatcherModule, agent : MasterAgent, msg : any, cb : MasterCallback)
{
if (!msg)
{
utils.invokeCallback(cb, new Error('masterwatcher unsubscribe empty message.'));
return;
}
module.watchdog.unsubscribe(msg.id);
utils.invokeCallback(cb);
};
let query = function (module : MasterWatcherModule, agent : MasterAgent, msg : any, cb : MasterCallback)
{
utils.invokeCallback(cb, null, module.watchdog.query());
};
let record = function (module : MasterWatcherModule, agent : MasterAgent, msg : any, cb : MasterCallback)
{
if (!msg)
{
utils.invokeCallback(cb, new Error('masterwatcher record empty message.'));
return;
}
module.watchdog.record(msg.id);
};
let masterMethods = {
'subscribe': subscribe,
'unsubscribe': unsubscribe,
'query': query,
'record': record
};