-
Notifications
You must be signed in to change notification settings - Fork 375
/
monitor.ts
112 lines (104 loc) · 3.18 KB
/
monitor.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
/**
* Component for monitor.
* Load and start monitor client.
*/
import { getLogger } from 'pinus-logger'; let logger = getLogger('pinus', __filename);
import * as admin from 'pinus-admin';
import * as moduleUtil from '../util/moduleUtil';
import * as utils from '../util/utils';
import * as Constants from '../util/constants';
import { Application } from '../application';
import { ConsoleService } from 'pinus-admin';
import { IModule, MasterInfo } from '../../index';
import { ServerInfo } from '../util/constants';
import { ServerStartArgs } from '../util/appUtil';
export interface MonitorOptions
{
closeWatcher ?: boolean;
}
export class Monitor
{
app: Application;
serverInfo: ServerInfo;
masterInfo: ServerStartArgs;
modules : IModule[] = [];
closeWatcher: any;
monitorConsole: ConsoleService;
constructor(app : Application, opts ?: MonitorOptions)
{
opts = opts || {};
this.app = app;
this.serverInfo = app.getCurServer();
this.masterInfo = app.getMaster();
this.closeWatcher = opts.closeWatcher;
this.monitorConsole = admin.createMonitorConsole({
id: this.serverInfo.id,
type: this.app.getServerType(),
host: this.masterInfo.host,
port: this.masterInfo.port,
info: this.serverInfo,
env: this.app.get(Constants.RESERVED.ENV),
authServer: app.get('adminAuthServerMonitor') // auth server function
});
};
start(cb : (err?:Error)=>void)
{
moduleUtil.registerDefaultModules(false, this.app, this.closeWatcher);
this.startConsole(cb);
};
startConsole(cb : (err?:Error)=>void)
{
moduleUtil.loadModules(this, this.monitorConsole);
let self = this;
this.monitorConsole.start(function (err)
{
if (err)
{
utils.invokeCallback(cb, err);
return;
}
moduleUtil.startModules(self.modules, function (err)
{
utils.invokeCallback(cb, err);
return;
});
});
this.monitorConsole.on('error', function (err)
{
if (!!err)
{
logger.error('monitorConsole encounters with error: %j', err.stack);
return;
}
});
};
stop(cb:()=>void)
{
this.monitorConsole.stop();
this.modules = [];
process.nextTick(function ()
{
utils.invokeCallback(cb);
});
};
// monitor reconnect to master
reconnect(masterInfo : MasterInfo)
{
let self = this;
this.stop(function ()
{
self.monitorConsole = admin.createMonitorConsole({
id: self.serverInfo.id,
type: self.app.getServerType(),
host: masterInfo.host,
port: masterInfo.port,
info: self.serverInfo,
env: self.app.get(Constants.RESERVED.ENV)
});
self.startConsole(function ()
{
logger.info('restart modules for server : %j finish.', self.app.serverId);
});
});
};
}