-
Notifications
You must be signed in to change notification settings - Fork 1
/
manager.js
65 lines (58 loc) · 1.42 KB
/
manager.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
'use strict';
let
ps = require('ps-node'), // for checking tunnel processes
Bluebird = require('bluebird')
class Manager {
static running(ProcessClass, command, tunnelIdArg) {
return find(ProcessClass, command, tunnelIdArg)
}
static withId(ProcessClass, command, tunnelIdArg) {
return find(ProcessClass, command, tunnelIdArg)
.then(found => {
let withId = [ ]
found.forEach(proc => {
if(proc.tunnelId) {
withId.push(proc)
}
})
return withId
})
}
static withoutId(ProcessClass, command, tunnelIdArg) {
return find(ProcessClass, command, tunnelIdArg)
.then(found => {
let withoutId = [ ]
found.forEach(proc => {
if(!proc.tunnelId) {
withoutId.push(proc)
}
})
return withoutId
})
}
}
function find(ProcessClass, command, tunnelIdArg) {
return new Bluebird((resolve, reject) => {
let found = [ ]
ps.lookup({
command: command
},
(err, list) => {
if(err) {
reject(err)
return
}
list.forEach(proc => {
let idx = proc.arguments.indexOf(tunnelIdArg)
if(-1 === idx || !proc.arguments[idx+1].match(/fork/)) {
found.push(new ProcessClass(
parseInt(proc.pid, 10),
(-1 !== idx ? proc.arguments[idx+1] : undefined)
))
}
})
resolve(found)
})
})
}
exports.Manager = Manager