-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunner.js
94 lines (79 loc) · 1.94 KB
/
runner.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
"use strict";
const pusage = require('pidusage');
const ps = require('ps-node');
let intervals = {};
let pids = {};
let calls = [{
'primary': 'php',
'primary_arg': ['image-gd.php'],
'secondary': null
},{
'primary': 'php',
'primary_arg': ['image-im.php'],
'secondary': 'convert'
},
{
'primary': 'php',
'primary_arg': ['image-gm.php'],
'secondary': 'gm'
}];
callProcess(calls, 0);
function callProcess(calls, i) {
if (calls[i]) {
fireChildProcess(calls[i], function() {
callProcess(calls, i + 1);
});
} else {
// cleanup.
for (var interval in intervals) {
clearInterval(interval);
}
}
}
function fireChildProcess(call, cb) {
const spawn = require('child_process').spawn;
const childProcess = spawn(call.primary, call.primary_arg);
findProcess(call.primary);
childProcess.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
childProcess.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
childProcess.on('close', (code) => {
clearInterval(intervals[call.primary]);
clearInterval(intervals[call.secondary]);
cb();
});
}
function findProcess(command) {
console.log("find process", command);
ps.lookup({
command: command
}, function(err, resultList ) {
if (err) {
throw new Error( err );
}
resultList.forEach(function(process) {
console.log("results", command, process);
if( process ){
pids[process.pid] = command;
intervals[command] = setInterval(function() {
checkMemory(process.pid);
}, 250);
}
});
});
}
function checkMemory(pid) {
pusage.stat(pid, function(err, stat) {
if (!err) {
console.log(`Command: ${pids[pid]} - Pcpu: ${stat.cpu} Mem: ${bytes2Megabytes(stat.memory)}`);
}
});
// Unmonitor process
pusage.unmonitor(pid);
}
function bytes2Megabytes(bytes) {
return Math.floor((bytes / 1048576) * 100) / 100;
}