This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
forked from mafintosh/respawn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
205 lines (164 loc) · 4.55 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
var events = require('events')
var spawn = require('child_process').spawn
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 Monitor = function(command, opts) {
events.EventEmitter.call(this)
this.id = null // for respawn-group
this.status = 'stopped'
this.command = command
this.cwd = opts.cwd || '.'
this.env = opts.env || {}
this.uid = opts.uid
this.gid = opts.gid
this.pid = 0
this.stdio = opts.stdio
this.windowsVerbatimArguments = opts.windowsVerbatimArguments
this.crashed = false
this.sleep = opts.sleep || 1000
this.maxRestarts = opts.maxRestarts === 0 ? 0 : opts.maxRestarts || 10
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 child = spawn(self.command[0], self.command.slice(1), {
cwd: self.cwd,
env: xtend(process.env, self.env),
uid: self.uid,
gid: self.gid,
stdio: self.stdio,
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 (child.stderr) {
child.stderr.on('data', function(data) {
self.emit('stderr', data)
})
}
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')
self.timeout = setTimeout(loop, self.sleep)
})
}
clearTimeout(this.timeout)
loop()
if (this.status === 'running') this.emit('start')
}
Monitor.prototype.toJSON = function() {
var doc = {
id: this.id,
status: this.status,
started: this.started,
pid: this.pid,
command: this.command,
cwd: this.cwd,
env: this.env
}
if (!doc.id) delete doc.id
if (!doc.pid) delete doc.pid
if (!doc.started) delete doc.started
return doc
}
Monitor.prototype._crash = function() {
if (this.status !== 'running') return
this.status = 'crashed'
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 (!Array.isArray(command)) return respawn(command.command, command)
return new Monitor(command, opts || {})
}
module.exports = respawn