UNPKG

1.15 kBJavaScriptView Raw
1const fs = require('fs');
2const path = require('path');
3const cluster = require("cluster");
4
5module.exports = () => {
6
7 if(Config("_server").restart) new fs._watcher(path.join(_vap.server)).watch(restart);
8
9 function restart (event,pathToIt) {
10 if(!pathToIt.endsWith(".js")) return; // watch JS files only!
11 consol.warning(" # Application files edit occurred");
12 consol.info(` - File "${pathToIt.substr(pathToIt.lastIndexOf("/")+1).substr(pathToIt.lastIndexOf("\\")+1)}" has been ${event}`);
13 consol.info(" - Now Killing all workers and creating new ones");
14
15 // if the program restarts when a file is changed
16 // we will preserve one worker and only kill it after
17 // all sibling workers have been killed
18 // and new generation spawned
19 let saved = 0;
20 for (let id in cluster.workers) {
21 if(!saved && Object.keys(cluster.workers).length > 1) {
22 saved = id;
23 continue;
24 }
25 cluster.workers[id].send("die");
26 }
27 cluster.on('listening', (worker, address) => {
28 if(saved && Object.keys(cluster.workers).length > 1) {
29 cluster.workers[saved].send("die");
30 saved = 0;
31 }
32 });
33 }
34};
\No newline at end of file