-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.js
158 lines (129 loc) · 3.86 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'use strict'
const chokidar = require('chokidar')
const cluster = require('cluster')
const config = require('./config')
const debug = require('debug')('web:cluster')
const fs = require('fs')
const path = require('path')
const log = require('@dadi/logger')
log.init(config.get('logging'))
require('console-stamp')(console, 'yyyy-mm-dd HH:MM:ss.l')
// Console start message
const dadiBoot = require('@dadi/boot')
dadiBoot.start(require('./package.json'))
let app
function createApp (options) {
return new Promise((resolve, reject) => {
options = options || {}
if (config.get('cluster')) {
if (cluster.isMaster) {
let numWorkers = require('os').cpus().length
log.info(
'Starting DADI Web in cluster mode, using ' + numWorkers + ' workers.'
)
log.info('Master cluster setting up ' + numWorkers + ' workers...')
// Start new workers
for (let i = 0; i < numWorkers; i++) {
cluster.fork()
}
// New worker alive
cluster.on('online', function (worker) {
log.info('Worker ' + worker.process.pid + ' is online')
})
// Handle a thread exit, start a new worker
cluster.on('exit', function (worker, code, signal) {
log.info(
'Worker ' +
worker.process.pid +
' died with code: ' +
code +
', and signal: ' +
signal
)
log.info('Starting a new worker')
cluster.fork()
})
// Watch the current directory for a "restart.web" file
let watcher = chokidar.watch(process.cwd(), {
depth: 0,
ignored: /(^|[/\\])\../, // ignores dotfiles, see https://regex101.com/r/7VuO4e/1
ignoreInitial: true
})
watcher.on('add', function (filePath) {
if (path.basename(filePath) === 'restart.web') {
log.info('Shutdown requested')
fs.unlinkSync(filePath)
restartWorkers()
}
})
} else {
if (app) {
return resolve({
App: app,
Components: app.components
})
}
// Start Workers
app = require(path.join(__dirname, '/index.js'))(options)
app.start(function () {
debug('process %s is listening for incoming requests', process.pid)
process.on('message', function (message) {
if (message.type === 'shutdown') {
log.info('Process ' + process.pid + ' is shutting down...')
process.exit(0)
}
})
return resolve({
App: app,
Components: app.components
})
})
}
} else {
if (app) {
return resolve({
App: app,
Components: app.components
})
}
// Single thread start
debug('starting DADI Web in single thread mode.')
app = require(path.join(__dirname, '/index.js'))(options)
app.start(function () {
debug('process %s is listening for incoming requests', process.pid)
return resolve({
App: app,
Components: app.components
})
})
}
})
}
function restartWorkers () {
let wid
let workerIds = []
for (wid in cluster.workers) {
workerIds.push(wid)
}
workerIds.forEach(function (wid) {
if (cluster.workers[wid]) {
cluster.workers[wid].send({
type: 'shutdown',
from: 'master'
})
setTimeout(function () {
if (cluster.workers[wid]) {
cluster.workers[wid].kill('SIGKILL')
}
}, 5000)
}
})
}
// export the modules
module.exports = createApp
module.exports.Config = require('./config')
module.exports.Event = require(path.join(__dirname, '/dadi/lib/event'))
module.exports.Preload = require(path.join(
__dirname,
'/dadi/lib/datasource/preload'
))