-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathmoduleUtil.ts
More file actions
127 lines (115 loc) · 3.43 KB
/
Copy pathmoduleUtil.ts
File metadata and controls
127 lines (115 loc) · 3.43 KB
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
import * as os from 'os';
import * as admin from 'pinus-admin';
import * as utils from './utils';
import * as Constants from './constants';
import * as pathUtil from './pathUtil';
import * as starter from '../master/starter';
import { getLogger } from 'pinus-logger';import { Application } from '../application';
import { ConsoleService, IModule, IModuleFactory } from 'pinus-admin';
import { MasterWatcherModule } from '../modules/masterwatcher';
import { MonitorWatcherModule } from '../modules/monitorwatcher';
import { ConsoleModule } from '../modules/console';
let logger = getLogger('pinus', __filename);
export interface ModuleRecord
{
module : IModuleFactory | IModule;
moduleId : string;
opts : any;
}
/**
* Load admin modules
*/
export function loadModules(self : {app : Application , modules : Array<IModule>}, consoleService : ConsoleService)
{
// load app register modules
let _modules = self.app.get(Constants.KEYWORDS.MODULE);
if (!_modules)
{
return;
}
let modules = [];
for (let m in _modules)
{
modules.push(_modules[m]);
}
let record, moduleId, module;
for (let i = 0, l = modules.length; i < l; i++)
{
record = modules[i];
if (typeof record.module === 'function')
{
module = new record.module(record.opts, consoleService);
} else
{
module = record.module;
}
moduleId = record.moduleId || module.moduleId;
if (!moduleId)
{
logger.warn('ignore an unknown module.');
continue;
}
consoleService.register(moduleId, module);
self.modules.push(module);
}
};
export function startModules(modules : IModule[], cb : (err?:Error)=>void)
{
// invoke the start lifecycle method of modules
if (!modules)
{
return;
}
startModule(null, modules, 0, cb);
};
/**
* Append the default system admin modules
*/
export function registerDefaultModules(isMaster : boolean, app : Application, closeWatcher : boolean)
{
if (!closeWatcher)
{
if (isMaster)
{
app.registerAdmin(MasterWatcherModule, { app: app });
} else
{
app.registerAdmin(MonitorWatcherModule, { app: app });
}
}
app.registerAdmin(admin.modules.watchServer, { app: app });
app.registerAdmin(ConsoleModule, { app: app, starter: starter });
if (app.enabled('systemMonitor'))
{
if (os.platform() !== Constants.PLATFORM.WIN)
{
app.registerAdmin(admin.modules.systemInfo);
app.registerAdmin(admin.modules.nodeInfo);
}
app.registerAdmin(admin.modules.monitorLog, { path: pathUtil.getLogPath(app.getBase()) });
app.registerAdmin(admin.modules.scripts, { app: app, path: pathUtil.getScriptPath(app.getBase()) });
if (os.platform() !== Constants.PLATFORM.WIN)
{
app.registerAdmin(admin.modules.profiler);
}
}
};
let startModule = function (err : Error, modules : IModule[], index : number, cb : (err?:Error)=>void)
{
if (err || index >= modules.length)
{
utils.invokeCallback(cb, err);
return;
}
let module = modules[index];
if (module && typeof module.start === 'function')
{
module.start((err)=>
{
startModule(err, modules, index + 1, cb);
});
} else
{
startModule(err, modules, index + 1, cb);
}
};