-
Notifications
You must be signed in to change notification settings - Fork 65
/
core.js
127 lines (120 loc) · 3.58 KB
/
core.js
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
'use strict'
// Import Packages
const chalk = require('chalk')
const preStart = require('./src/prestart')
preStart.check() // Check Env
// Read Command
const commander = require('./src/commander')
const { opts } = commander.process()
// PreStart
preStart.loadAsync(opts.config_file || null, false, opts.dev).then(() => {
const { logger } = require('./src/logger')
if (opts.dev) {
logger.verbose(
chalk.yellowBright('[debug] you are running at Development mode.'),
)
}
// Load childProcesses
let childProcessList = []
async function registerProcesses() {
const {
processes: processesMap,
receivers,
} = require('./adapter/processes')
const processesToStart = []
const isDev = opts.dev
processesMap.forEach((process) => {
if (
(process.isDev && isDev) ||
(process.isProd && !isDev) ||
(!process.isDev && !process.isDev)
) {
processesToStart.push(process)
}
})
const { staticProcess, ProcessInteract } = require('./src/process')
processesToStart.forEach((v) =>
staticProcess().spawnProcess(v.path, v.name, v.messageListener),
)
childProcessList = staticProcess().ProcessList
// load receivers
const processInteract = new ProcessInteract(receivers)
processInteract.register()
}
// Run Task
// TODO: create a task tree file
const {
Task: updateSentencesTask,
} = require('./src/task/updateSentencesTask')
// Web Server Master
const { startWorkersPool, WorkersBridge } = require('./src/http/primary')
function notifyChildProcessesExit() {
childProcessList.forEach((child) => {
child.instance.kill('SIGTERM') // teng-koa exit signal code
})
for (const workerID in WorkersBridge.workers.workers) {
WorkersBridge.workers.workers[workerID].process.kill('SIGTERM') // force kill
}
}
// handle the process exit event
function handleProcessExitSignal(signal) {
logger.verbose(
'[core] received signal: ' +
chalk.yellow(signal) +
', start the exit produce.',
)
notifyChildProcessesExit()
logger.info('[core] Web server is shut down, Bye!')
process.exit(0)
}
process.on('uncaughtException', function (err) {
logger.error(`uncaughtException: ${err.stack}`)
const { Sentry, CaptureUncaughtException } = require('./src/tracing')
const nconf = require('nconf')
if (nconf.get('telemetry:error') && !nconf.get('dev')) {
CaptureUncaughtException(err)
Sentry.close()
.then(notifyChildProcessesExit)
.then(() => {
process.exit(1)
})
} else {
notifyChildProcessesExit()
process.exit(1)
}
})
process.on('SIGINT', handleProcessExitSignal) // Ctrl + C
process.on('SIGTERM', handleProcessExitSignal)
process.on('exit', (code) => {
// handle unexpected exit event
if (code) {
// ignore zero exit code
logger.error(
'[core] received exit code: ' + code + ', process will be destroyed.',
)
notifyChildProcessesExit()
logger.info('[core] Web server is shut down, Bye!')
}
})
// Start Server
async function start() {
try {
await preStart.checkProgramUpdates()
await updateSentencesTask()
await registerProcesses()
await startWorkersPool()
logger.verbose('[init] All init steps are exceeded.')
} catch (e) {
logger.error(chalk.red(e.stack))
logger.error(
'[init] error was thrown while initializing, processes exiting.',
)
// mail.error(e)
process.exit(1)
}
}
start().catch((e) => {
logger.error(e)
throw e
})
})