forked from tbenbrahim/windows-service-manager
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwindows-service-manager.js
147 lines (132 loc) · 3.8 KB
/
windows-service-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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* A nodejs module to query, start and stop windows services
* @author Tony BenBrahim <tony.benbrahim at gmail.com>
*/
if (require('os').platform().indexOf('win32') === -1) {
throw "windows-service-manager is for Microsoft Windows only.";
}
var exec = require('child_process').exec;
var getValue = function(line) {
var i = line.indexOf(':');
return i === -1 ? null : line.substr(i + 1).trim();
};
var splitValue = function(line) {
var i = line.indexOf(' ');
return i === -1 ? [ null, null ] : [ line.substr(0, i).trim(), line.substr(i + 1).trim() ];
};
var Enumerator = function(items) {
this.items = items;
this.index = 0;
};
Enumerator.prototype.hasNext = function() {
return this.index < this.items.length;
};
Enumerator.prototype.next = function() {
var result = this.items[this.index];
this.index += 1;
return result;
};
var parseNextEntry = function(linesEnum) {
var entry;
while (linesEnum.hasNext()) {
var line = linesEnum.next();
if (line.indexOf('SERVICE_NAME') === 0) {
entry = {
"name" : getValue(line)
};
} else if (line.indexOf('DISPLAY_NAME') === 0) {
entry.displayName = getValue(line);
} else if (line.indexOf(' STATE') === 0) {
var states = splitValue(getValue(line));
entry.state = parseInt(states[0], 10);
entry.stateDescription = states[1];
} else if (line.indexOf(' PID') === 0) {
entry.pid = parseInt(getValue(line), 10);
break;
}
}
return entry;
};
exports.queryServices = function(callback) {
exec('sc queryex', function(error, stdout, stderr) {
if (error) {
callback(error, null);
} else {
var linesEnum = new Enumerator(stdout.split("\n"));
var entries = [];
while (linesEnum.hasNext()) {
var entry = parseNextEntry(linesEnum);
if (entry) {
entries.push(entry);
}
}
callback(null, entries);
}
});
};
exports.queryService = function(name, callback) {
exec('sc queryex "' + name + '"', function(error, stdout, stderr) {
if (error) {
callback(error, null);
} else {
var linesEnum = new Enumerator(stdout.split("\n"));
var entry = parseNextEntry(linesEnum);
callback(null, entry);
}
});
};
exports.waitForState = function(service, state, timeoutSeconds, callback) {
var repeatId = setInterval(function() {
exports.queryService(service.name, function(error, service) {
if (error) {
clearTimeout(timeoutId);
clearInterval(repeatId);
callback(error, null);
} else if (service.state === state) {
clearTimeout(timeoutId);
clearInterval(repeatId);
callback(null, service);
}
});
}, 200);
var timeoutId = setTimeout(function() {
callback('timeout', service);
}, timeoutSeconds * 1000);
};
var switchToState = function(name, state, timeoutSeconds, callback) {
exports.queryService(name, function(error, service) {
if (error) {
callback(error, null);
} else if (service.state === state) {
callback(null, service);
} else {
exec('sc ' + (state === 1 ? 'stop' : 'start') + ' "' + name + '"', function(error, stdout, stderr) {
if (error) {
callback(error, null);
} else if (timeoutSeconds !== 0) {
exports.waitForState(service, state, timeoutSeconds, callback);
}
});
}
});
};
exports.stopService = function(name, timeoutSeconds, kill, callback) {
switchToState(name, 1, timeoutSeconds, !kill ? callback : function(error, service) {
if (error === 'timeout') {
exec('taskkill /F /PID ' + service.pid, function(error, stdout, stderr) {
if (error) {
callback(error, null);
} else {
exports.queryService(service.name, function(error, service) {
callback(error || service.status !== 1 ? "Unable to kill" : null, service);
});
}
});
} else {
callback(error, service);
}
});
};
exports.startService = function(name, timeoutSeconds, callback) {
switchToState(name, 4, timeoutSeconds, callback);
};