1 |
|
2 | import Emittery from 'emittery'
|
3 |
|
4 | import utils from '@midgar/utils'
|
5 | import Config from './libs/config'
|
6 | import Logger from './libs/logger'
|
7 | import PluginManager from './libs/plugin-manager'
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | class Midgar extends Emittery {
|
15 | constructor () {
|
16 | super()
|
17 |
|
18 | |
19 |
|
20 |
|
21 |
|
22 | this.config = null
|
23 |
|
24 | |
25 |
|
26 |
|
27 |
|
28 | this.logger = null
|
29 |
|
30 | |
31 |
|
32 |
|
33 |
|
34 | this.pm = null
|
35 |
|
36 | |
37 |
|
38 |
|
39 |
|
40 |
|
41 | this.cli = null
|
42 |
|
43 |
|
44 | this._watchProcessExit()
|
45 | }
|
46 |
|
47 | |
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | async start (configPath) {
|
55 | await this.loadConfig(configPath)
|
56 | await this.initLogger()
|
57 | await this.initPluginManager()
|
58 | }
|
59 |
|
60 | |
61 |
|
62 |
|
63 | async loadConfig (dirPath) {
|
64 |
|
65 | this.configPath = dirPath
|
66 | this.config = new Config(this)
|
67 |
|
68 |
|
69 | await this.config.loadConfigs(dirPath, 'config', true)
|
70 |
|
71 | process.env.TZ = this.config.tz || 'Europe/Paris'
|
72 |
|
73 | this.config.log.level = this.config.log && this.config.log.level ? this.config.log.level : 'warn'
|
74 |
|
75 | if (!this.config.pm) this.config.pm = {}
|
76 | if (this.config.pm.localPath && typeof this.config.pm.localPath !== 'string') throw new TypeError('@midgar/midgar: Invalid pm.localPath type in Midgar config !')
|
77 |
|
78 |
|
79 | this._isConfigLoaded = true
|
80 | }
|
81 |
|
82 | |
83 |
|
84 |
|
85 | async initLogger () {
|
86 |
|
87 | if (this.config === null) throw new Error('@midgar/midgar: Load config before init logger !')
|
88 |
|
89 | this.logger = this.config.logger ? this.config.logger(this.config.log) : new Logger(this.config.log)
|
90 | await this.logger.init()
|
91 |
|
92 |
|
93 | this._isLoggerInit = true
|
94 | }
|
95 |
|
96 | |
97 |
|
98 |
|
99 | async initPluginManager () {
|
100 |
|
101 | if (this.config === null) throw new Error('@midgar/midgar: Load config before init pm !')
|
102 | if (this.logger === null) throw new Error('@midgar/midgar: Init logger before init pm !')
|
103 |
|
104 | utils.timer.start('midgar-init')
|
105 | this.debug('@midgar/midgar: init PluginManager...')
|
106 |
|
107 |
|
108 | await this._initPluginManager()
|
109 |
|
110 | |
111 |
|
112 |
|
113 |
|
114 | await this.emit('@midgar/midgar:afterInit')
|
115 |
|
116 | const time = utils.timer.getTime('midgar-init')
|
117 | this.debug(`@midgar:midgar: PluginManager has init in ${time} ms.`)
|
118 | }
|
119 |
|
120 | |
121 |
|
122 |
|
123 |
|
124 | async _initPluginManager () {
|
125 | this.pm = new PluginManager(this)
|
126 | await this.pm.init()
|
127 | }
|
128 |
|
129 | |
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | addPlugin (name) {
|
138 |
|
139 | const pm = this.pm ? this.pm : new PluginManager(this)
|
140 | return pm.addPlugin(name)
|
141 | }
|
142 |
|
143 | |
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | async removePlugin (name) {
|
152 |
|
153 | const pm = this.pm ? this.pm : new PluginManager(this)
|
154 | return pm.removePlugin(name)
|
155 | }
|
156 |
|
157 | |
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | async enablePlugin (name) {
|
166 |
|
167 | const pm = this.pm ? this.pm : new PluginManager(this)
|
168 | return pm.enablePlugin(name)
|
169 | }
|
170 |
|
171 | |
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | async disablePlugin (name) {
|
180 |
|
181 | const pm = this.pm ? this.pm : new PluginManager(this)
|
182 | return pm.disablePlugin(name)
|
183 | }
|
184 |
|
185 | |
186 |
|
187 |
|
188 |
|
189 | getNodeEnv () {
|
190 | return process.env.NODE_ENV
|
191 | }
|
192 |
|
193 | |
194 |
|
195 |
|
196 |
|
197 | async exit () {
|
198 |
|
199 | if (this.pm === null) throw new Error('@midgar/midgar: Start Midgar before stop !')
|
200 |
|
201 |
|
202 | await this.stop()
|
203 |
|
204 | if (this.logger && this.logger.exit && !this._hasExitLogger) {
|
205 | this._hasExitLogger = true
|
206 | await this.logger.exit()
|
207 | }
|
208 |
|
209 |
|
210 | process.exit(0)
|
211 | }
|
212 |
|
213 | |
214 |
|
215 |
|
216 | async stop () {
|
217 | |
218 |
|
219 |
|
220 |
|
221 | await this.emit('@midgar/midgar:stop')
|
222 | }
|
223 |
|
224 | |
225 |
|
226 |
|
227 |
|
228 | _watchProcessExit () {
|
229 | process.stdin.resume()
|
230 |
|
231 |
|
232 | const exitHandler = () => {
|
233 |
|
234 | if (!this._exit) { this._exit = true } else { return }
|
235 |
|
236 | this.exit()
|
237 | }
|
238 |
|
239 |
|
240 | const uncaughtExceptionHandler = (error) => {
|
241 | if (this.logger) {
|
242 |
|
243 | this.logger.error('Uncaught Exception :(')
|
244 | this.logger.error(error)
|
245 | } else {
|
246 | console.error(error)
|
247 | }
|
248 | process.exit()
|
249 | }
|
250 |
|
251 | const uncaughtRejectionHandler = (error) => {
|
252 | if (this.logger) {
|
253 |
|
254 | this.logger.error('Uncaught Rejection :(')
|
255 | this.logger.error(error)
|
256 | } else {
|
257 | console.error(error)
|
258 | }
|
259 |
|
260 | process.exit()
|
261 | }
|
262 |
|
263 |
|
264 | process.on('exit', exitHandler.bind(null))
|
265 |
|
266 | process.on('SIGINT', exitHandler.bind(null))
|
267 |
|
268 | process.on('SIGUSR1', exitHandler.bind(null))
|
269 | process.on('SIGUSR2', exitHandler.bind(null))
|
270 |
|
271 | process.on('uncaughtException', uncaughtExceptionHandler.bind(null))
|
272 | process.on('unhandledRejection', uncaughtRejectionHandler.bind(null))
|
273 | }
|
274 |
|
275 | |
276 |
|
277 |
|
278 |
|
279 | error (...args) {
|
280 | this.logger.error(...args)
|
281 | }
|
282 |
|
283 | |
284 |
|
285 |
|
286 |
|
287 | warn (...args) {
|
288 | this.logger.warn(...args)
|
289 | }
|
290 |
|
291 | |
292 |
|
293 |
|
294 |
|
295 | info (...args) {
|
296 | this.logger.info(...args)
|
297 | }
|
298 |
|
299 | |
300 |
|
301 |
|
302 |
|
303 | verbose (...args) {
|
304 | this.logger.verbose(...args)
|
305 | }
|
306 |
|
307 | |
308 |
|
309 |
|
310 |
|
311 | debug (...args) {
|
312 | this.logger.debug(...args)
|
313 | }
|
314 |
|
315 | |
316 |
|
317 |
|
318 |
|
319 | silly (...args) {
|
320 | this.logger.silly(...args)
|
321 | }
|
322 | }
|
323 |
|
324 | export default Midgar
|