4
4
const Koa = require ( 'koa' ) // Koa v2
5
5
const winston = require ( 'winston' )
6
6
const nconf = require ( 'nconf' )
7
- const path = require ( 'path' )
7
+ // const path = require('path')
8
8
const colors = require ( 'colors/safe' )
9
9
// const pkg = require('./package.json')
10
10
// const mail = require('./src/mail')
@@ -27,50 +27,29 @@ if (program.dev) {
27
27
// Register Server
28
28
const app = new Koa ( )
29
29
30
- // Load CronJob
31
- const childProcess = require ( 'child_process' )
32
- function spawnCronProcess ( ) {
33
- const child = childProcess . fork ( path . join ( __dirname , './src/cron.js' ) , {
34
- env : {
35
- dev : program . dev
30
+ // Load childProcesses
31
+ let childProcessList = [ ]
32
+ async function registerProcesses ( ) {
33
+ const processesMap = require ( './processes' )
34
+ const processesToStart = [ ]
35
+ const isDev = program . dev
36
+ for ( const process of processesMap ) {
37
+ if ( ( process . isDev && isDev ) || ( process . isProd && ! isDev ) || ( ! process . isDev && ! process . isDev ) ) {
38
+ processesToStart . push ( process )
36
39
}
37
- } )
38
- child . on ( 'message' , ( message ) => {
39
- if ( message === 'loaded' ) {
40
- winston . verbose ( '[init] all cronJobs are loaded.' )
41
- } else if ( message . key ) {
42
- if ( message . key === 'error' ) {
43
- console . log ( colors . red ( message . data ) )
44
- winston . error ( '[init] error was thrown while loading cron jobs, process existing.' )
45
- }
46
- }
47
- } )
48
- // register master exit process
49
- let masterExitFlag = false
50
- process . on ( 'exit' , ( ) => {
51
- // kill child process
52
- masterExitFlag = true
53
- child . send ( {
54
- key : 'exit' ,
55
- data : ''
56
- } )
57
- } )
58
- child . on ( 'exit' , ( ) => {
59
- if ( ! masterExitFlag ) {
60
- winston . warn ( '[cronJob] cron job process is exited. Try to respawn it.' )
61
- spawnCronProcess ( )
62
- }
63
- } )
40
+ }
41
+ const { staticProcess } = require ( './src/process' )
42
+ processesToStart . forEach ( v => staticProcess ( ) . spawnProcess ( v . path , v . name , v . messageListener ) )
43
+ childProcessList = staticProcess ( ) . ProcessList
64
44
}
65
- spawnCronProcess ( )
66
45
67
46
// Register Middlewares (Plugins)
68
47
async function registerMiddlewares ( ) {
69
48
require ( './src/middleware' ) . register ( app , program . dev )
70
49
}
71
50
72
51
// Run Task
73
- // TODO: crate a task tree file
52
+ // TODO: create a task tree file
74
53
const { Task : updateSentencesTask } = require ( './src/task/updateSentencesTask' )
75
54
76
55
// Load Route
@@ -94,10 +73,33 @@ async function registerRoutes (routes) {
94
73
}
95
74
}
96
75
76
+ // handle the process exit event
77
+ function handleProcessExitSignal ( signal ) {
78
+ winston . verbose ( '[core] receive signal: ' + colors . yellow ( signal ) + ', starting the exit produre.' )
79
+ for ( const child of childProcessList ) {
80
+ child . instance . kill ( 'SIGTERM' ) // teng-koa exit signal code
81
+ }
82
+ winston . info ( '[core] Web server is shut down, Bye!' )
83
+ process . exit ( 0 )
84
+ }
85
+ process . on ( 'SIGINT' , handleProcessExitSignal ) // Ctrl + C
86
+ process . on ( 'SIGTERM' , handleProcessExitSignal )
87
+
88
+ process . on ( 'exit' , ( code ) => { // handle unexpected exit event
89
+ if ( code ) { // ignore zero exit code
90
+ winston . error ( '[core] receiving exit code: ' + code + ', process will be destoryed.' )
91
+ for ( const child of childProcessList ) {
92
+ child . instance . kill ( 'SIGTERM' ) // teng-koa exit signal code
93
+ }
94
+ winston . info ( '[core] Web server is shut down, Bye!' )
95
+ }
96
+ } )
97
+
97
98
// Start Server
98
99
async function start ( ) {
99
100
try {
100
101
await updateSentencesTask ( )
102
+ await registerProcesses ( )
101
103
await registerMiddlewares ( )
102
104
const Routes = require ( './src/route' )
103
105
await registerRoutes ( new Routes ( ) )
0 commit comments