-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycler.js
56 lines (49 loc) · 1.68 KB
/
cycler.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
const { spawn, fork } = require('child_process');
const cores = require('os').cpus();
class ProcessCycler {
constructor(config){
this.script = String(config.scriptPath);
this.maxCycles = Number(config.cycles);
this.cores = cores.length;
this.activeProcess = [];
this.cycles = 0;
}
startExecution(){
if( this.cycles === 0 ){
//console.log(`Max CPU cores available: ${this.cores}.\nStarting ${this.cores} child process...`);
console.log(`Starting script: ${this.script}`);
}
this.cycles++;
console.log(`Executing cycle: ${this.cycles}`);
spawn(process.execPath, [this.script], {
stdio: 'inherit'
});
// fix
for(let i = 0; i < this.cores; i++){
let childProcess = fork(this.script, {
stdio: 'inherit',
});
this.activeProcess.push({process: childProcess});
console.log(`Starting child process ${i} pid: ${childProcess.pid}`);
}
process.on('beforeExit', () => {
this.cycleExecution();
});
}
cycleExecution(){
if( this.cycles < this.maxCycles ){
for(let i = 0; i < this.activeProcess.length; i++){
let childProcess = this.activeProcess[i].process;
if( !childProcess.killed ){
console.log(`Killing child process: ${childProcess.pid}`);
childProcess.kill('SIGKILL');
}
}
this.activeProcess = [];
this.startExecution();
} else {
process.exit();
}
}
}
exports.ProcessCycler = ProcessCycler;