Spaces:
Sleeping
Sleeping
Upload index.js
Browse files
index.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const moment = require("moment-timezone")
|
| 2 |
+
let cluster = require('cluster')
|
| 3 |
+
let path = require('path')
|
| 4 |
+
let fs = require('fs')
|
| 5 |
+
const {
|
| 6 |
+
createServer
|
| 7 |
+
} = require("http");
|
| 8 |
+
const {
|
| 9 |
+
Server
|
| 10 |
+
} = require("socket.io");
|
| 11 |
+
const Readline = require('readline')
|
| 12 |
+
const yargs = require('yargs/yargs')
|
| 13 |
+
const rl = Readline.createInterface(process.stdin, process.stdout)
|
| 14 |
+
|
| 15 |
+
var isRunning = false
|
| 16 |
+
function start(file) {
|
| 17 |
+
if (isRunning) return
|
| 18 |
+
isRunning = true
|
| 19 |
+
let args = [path.join(__dirname, file), ...process.argv.slice(2)]
|
| 20 |
+
cluster.setupMaster({
|
| 21 |
+
exec: path.join(__dirname, file),
|
| 22 |
+
args: args.slice(1),
|
| 23 |
+
})
|
| 24 |
+
let p = cluster.fork()
|
| 25 |
+
p.on('message', data => {
|
| 26 |
+
console.log('[RECEIVED]', data)
|
| 27 |
+
|
| 28 |
+
switch (data) {
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
case 'reset':
|
| 32 |
+
p.process.kill()
|
| 33 |
+
isRunning = false
|
| 34 |
+
start.apply(this, arguments)
|
| 35 |
+
break
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
case 'null':
|
| 39 |
+
p.process.kill()
|
| 40 |
+
isRunning = false
|
| 41 |
+
start.apply(this, arguments)
|
| 42 |
+
break
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
}
|
| 46 |
+
})
|
| 47 |
+
p.on('exit', (_, code) => {
|
| 48 |
+
if (code == null) process.exit()
|
| 49 |
+
isRunning = false
|
| 50 |
+
console.error('Exited with code:', code)
|
| 51 |
+
|
| 52 |
+
if (code === 0) return
|
| 53 |
+
fs.watchFile(args[0], () => {
|
| 54 |
+
fs.unwatchFile(args[0])
|
| 55 |
+
start(file)
|
| 56 |
+
})
|
| 57 |
+
})
|
| 58 |
+
let opts = new Object(yargs(process.argv.slice(2)).exitProcess(false).parse())
|
| 59 |
+
if (!opts['test'])
|
| 60 |
+
if (!rl.listenerCount()) rl.on('line', line => {
|
| 61 |
+
p.emit('message', line.trim())
|
| 62 |
+
})
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
start('main.js')
|