Skip to content

Commit

Permalink
Merge pull request #7 from calvinmetcalf/sleep-function
Browse files Browse the repository at this point in the history
ability to pass a function to sleep
  • Loading branch information
mafintosh committed May 18, 2015
2 parents 4b09a8f + 2731997 commit 6e08816
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 27 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ If `sleep` is an array of numbers it will use the value at the position of the c

`sleep: [1000, 60000, 60000, 12000, 1000]` will wait 1000ms before retrying, then it will wait 60000 before the next retry and so forth.

If `sleep` is a function it will be passed the number of times (including this one) that the app has been restarted (i.e. first time will be called with 1, second time 2 etc.) and should return a time in milliseconds.

## API

* `monitor.start()` Starts the monitor
Expand Down
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ var kill = function(pid, sig) {
})
})
}

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)

Expand All @@ -44,7 +49,7 @@ var Monitor = function(command, opts) {
this.windowsVerbatimArguments = opts.windowsVerbatimArguments

this.crashed = false
this.sleep = Array.isArray(opts.sleep) ? opts.sleep : [opts.sleep || 1000]
this.sleep = typeof opts.sleep === 'function' ? opts.sleep : defaultSleep(opts.sleep)
this.maxRestarts = opts.maxRestarts === 0 ? 0 : opts.maxRestarts || 10
this.kill = opts.kill === false ? false : opts.kill || 30000

Expand Down Expand Up @@ -155,7 +160,7 @@ Monitor.prototype.start = function() {
self.status = 'sleeping'
self.emit('sleep')

var restartTimeout = self.sleep[restarts - 1] || self.sleep[self.sleep.length - 1]
var restartTimeout = self.sleep(restarts)
self.timeout = setTimeout(loop, restartTimeout)
})
}
Expand Down
78 changes: 54 additions & 24 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,27 +236,57 @@ test('stop status', function(t) {
mon.start()
})

// test('restart using restart strategy', function (t) {
// var timeouts = [2, 100, 450, 40]
// var mon = respawn([node, crash], { maxRestarts: 5, sleep: timeouts})
// var times = [Date.now()]
// mon.on('sleep', function() {
// times.push(Date.now())
// })
// mon.on('crash', function() {
// times.forEach(function(current, key, times) {
// if (key <= 1) return
// var previous = times[key - 1]
// var timeout = timeouts[key - 2] || 0
// var delta = (current - previous)
// var gracePeriod = 75
// t.ok(delta - timeout < gracePeriod, util.format(
// 'should trigger shortly after defined timeout. '
// + 'Got %d ms, should have stayed within %d-%d ms',
// delta, timeout, timeout + gracePeriod)
// )
// })
// t.end()
// })
// mon.start()
// })
test('restart using restart strategy', function (t) {
var timeouts = [2, 100, 450, 40]
var mon = respawn([node, crash], { maxRestarts: 5, sleep: timeouts})
var times = [Date.now()]
mon.on('sleep', function() {
times.push(Date.now())
})
mon.on('crash', function() {
times.forEach(function(current, key, times) {
if (key <= 1) return
var previous = times[key - 1]
var timeout = (timeouts[key - 2] || 0) + 100
var delta = (current - previous)
var gracePeriod = 75
t.ok(delta - timeout < gracePeriod, util.format(
'should trigger shortly after defined timeout. '
+ 'Got %d ms, should have stayed within %d-%d ms',
delta, timeout, timeout + gracePeriod)
)
})
t.end()
})
mon.start()
})

test('restart using restart strategy function', function (t) {
var timeouts = [50, 100, 200, 400]
var mon = respawn([node, crash], {
maxRestarts: 5,
sleep: function(i) {
return 50 << (i - 1)
}
})
var times = [Date.now()]
mon.on('sleep', function() {
times.push(Date.now())
})
mon.on('crash', function() {
times.forEach(function(current, key, times) {
if (key <= 1) return
var previous = times[key - 1]
var timeout = (timeouts[key - 2] || 0) + 100
var delta = (current - previous)
var gracePeriod = 75
t.ok(delta - timeout < gracePeriod, util.format(
'should trigger shortly after defined timeout. '
+ 'Got %d ms, should have stayed within %d-%d ms',
delta, timeout, timeout + gracePeriod)
)
})
t.end()
})
mon.start()
})

0 comments on commit 6e08816

Please sign in to comment.