forked from mafintosh/respawn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
241 lines (198 loc) · 5.58 KB
/
index.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
var events = require('events')
var spawn = require('child_process').spawn
var fork = require('child_process').fork
var exec = require('child_process').exec
var ps = require('ps-tree')
var util = require('util')
var xtend = require('xtend')
var os = require('os')
var kill = function(pid, sig) {
if (os.platform() === 'win32') {
exec('taskkill /pid ' + pid + ' /T /F')
return
}
ps(pid, function(_, pids) {
pids = (pids || []).map(function(item) {
return parseInt(item.PID, 10)
})
pids.push(pid)
pids.forEach(function(pid) {
try {
process.kill(pid, sig)
} catch (err) {
// do nothing
}
})
})
}
var defaultSleep = function (sleep) {
sleep = Array.isArray(sleep) ? sleep : [sleep || 1000]
return function (restarts) {
return sleep[restarts - 1] || sleep[sleep.length - 1]
}
}
var Monitor = function(command, opts) {
events.EventEmitter.call(this)
this.id = null // for respawn-group
this.status = 'stopped'
this.command = command
this.name = opts.name
this.cwd = opts.cwd || '.'
this.env = opts.env || {}
this.data = opts.data || {}
this.uid = opts.uid
this.gid = opts.gid
this.pid = 0
this.crashes = 0
this.stdio = opts.stdio
this.stdout = opts.stdout
this.stderr = opts.stderr
this.silent = opts.silent
this.windowsVerbatimArguments = opts.windowsVerbatimArguments
this.spawnFn = opts.fork
? fork
: spawn
this.crashed = false
this.sleep = typeof opts.sleep === 'function' ? opts.sleep : defaultSleep(opts.sleep)
this.maxRestarts = opts.maxRestarts === 0 ? 0 : opts.maxRestarts || -1
this.kill = opts.kill === false ? false : opts.kill || 30000
this.child = null
this.started = null
this.timeout = null
}
util.inherits(Monitor, events.EventEmitter)
Monitor.prototype.stop = function(cb) {
if (this.status === 'stopped' || this.status === 'stopping') return cb && cb()
this.status = 'stopping'
clearTimeout(this.timeout)
if (cb) {
if (this.child) this.child.on('exit', cb)
else process.nextTick(cb)
}
if (!this.child) return this._stopped()
var self = this
var child = self.child
var sigkill = function() {
kill(child.pid, 'SIGKILL')
self.emit('force-kill')
}
var onexit = function() {
clearTimeout(wait)
}
if (this.kill !== false) {
var wait = setTimeout(sigkill, this.kill)
this.child.on('exit', onexit)
}
kill(this.child.pid)
}
Monitor.prototype.start = function() {
if (this.status === 'running') return
var self = this
var restarts = 0
var clock = 60000
var loop = function() {
var cmd = typeof self.command === 'function' ? self.command() : self.command
var child = self.spawnFn(cmd[0], cmd.slice(1), {
cwd: self.cwd,
env: xtend(process.env, self.env),
uid: self.uid,
gid: self.gid,
stdio: self.stdio,
silent: self.silent,
windowsVerbatimArguments: self.windowsVerbatimArguments
})
self.started = new Date()
self.status = 'running'
self.child = child
self.pid = child.pid
self.emit('spawn', child)
child.setMaxListeners(0)
if (child.stdout) {
child.stdout.on('data', function(data) {
self.emit('stdout', data)
})
if (self.stdout) {
child.stdout.pipe(self.stdout)
}
}
if (child.stderr) {
child.stderr.on('data', function(data) {
self.emit('stderr', data)
})
if (self.stderr) {
child.stderr.pipe(self.stderr)
}
}
child.on('message', function(message) {
self.emit('message', message)
})
var clear = function() {
if (self.child !== child) return false
self.child = null
self.pid = 0
return true
}
child.on('error', function(err) {
self.emit('warn', err) // too opionated? maybe just forward err
if (!clear()) return
if (self.status === 'stopping') return self._stopped()
self._crash()
})
child.on('exit', function(code, signal) {
self.emit('exit', code, signal)
if (!clear()) return
if (self.status === 'stopping') return self._stopped()
clock -= (Date.now() - (self.started ? self.started.getTime() : 0))
if (clock <= 0) {
clock = 60000
restarts = 0
}
if (++restarts > self.maxRestarts && self.maxRestarts !== -1) return self._crash()
self.status = 'sleeping'
self.emit('sleep')
var restartTimeout = self.sleep(restarts)
self.timeout = setTimeout(loop, restartTimeout)
})
}
clearTimeout(this.timeout)
loop()
if (this.status === 'running') this.emit('start')
}
Monitor.prototype.toJSON = function() {
var doc = {
id: this.id,
name: this.name,
status: this.status,
started: this.started,
pid: this.pid,
crashes: this.crashes,
command: this.command,
cwd: this.cwd,
env: this.env,
data: this.data
}
if (!doc.id) delete doc.id
if (!doc.pid) delete doc.pid
if (!doc.name) delete doc.name
if (!doc.data) delete doc.data
if (!doc.started) delete doc.started
return doc
}
Monitor.prototype._crash = function() {
if (this.status !== 'running') return
this.status = 'crashed'
this.crashes++
this.emit('crash')
if (this.status === 'crashed') this._stopped()
}
Monitor.prototype._stopped = function() {
if (this.status === 'stopped') return
if (this.status !== 'crashed') this.status = 'stopped'
this.started = null
this.emit('stop')
}
var respawn = function(command, opts) {
if (typeof command !== 'function' && !Array.isArray(command)) return respawn(command.command, command)
return new Monitor(command, opts || {})
}
module.exports = respawn