Skip to content

Commit

Permalink
Graceful shutdown (#16)
Browse files Browse the repository at this point in the history
* Add package-lock.json

* Add myself to contributors (so folks know who to blame) ;)

* Initial automatic npm audit fix; all tests passing

* Results of npm audit fix --force; all tests passing

(But test not exiting. Same behaviour as I see on Site.js with instant instead of Express static. Will sort out later by implementing graceful shutdown as I believe it is due to lack of garbage collection of the file system watcher.)

* Update supertest to latest; now at zero npm audit vulnerabilities

* Replace deprecated prepublish npm task with prepare

* Update filewatcher to latest version; all tests passing

* Bump version

* Clean up on shutdown (when asked manually and/or if app is killed)

* Ensure cleanUp/graceful shutdown is only active if not bypassed

* Ensure cleanUp() method exists whether bypass is requested or not

* ci: update node version to lts/*
  • Loading branch information
aral authored and fgnass committed Oct 25, 2019
1 parent 0641783 commit 125ccdb
Show file tree
Hide file tree
Showing 5 changed files with 2,303 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,3 +1,3 @@
language: node_js
node_js:
- 0.10
- lts/*
30 changes: 27 additions & 3 deletions lib/index.js
Expand Up @@ -3,6 +3,7 @@ var sendevent = require('sendevent')
, serve = require('./serve')
, stack = require('stacked')
, filewatcher = require('filewatcher')
, Graceful = require('node-graceful')

// timestamp to detect server-restarts
var startup = Date.now()
Expand Down Expand Up @@ -45,10 +46,23 @@ module.exports = function(root, opts) {

if (root && opts.watch !== false) {
var urlsByFile = {}
, watcher = filewatcher({ delay: opts.delay })

fn.watcher = filewatcher({ delay: opts.delay })

// Stop the file system watcher so apps can exit gracefully when
// terminated by Ctrl+c (SIGINT) or a polite termination request (SIGTERM).
// You can also call the cleanUp() function directly to handle housekeeping
// in your own server (e.g., when it is asked to close).
fn.cleanUp = function (done) {
fn.watcher.removeAll()
done()
}
Graceful.timeout = 3000
Graceful.on('SIGINT', fn.cleanUp)
Graceful.on('SIGTERM', fn.cleanUp)

// when a file is modifed tell all clients to reload it
watcher.on('change', function(file) {
fn.watcher.on('change', function(file) {
fn.reload(urlsByFile[file])
})

Expand All @@ -62,10 +76,20 @@ module.exports = function(root, opts) {
if (!re.test(path)) return
urlsByFile[path] = this.path
this._maxage = 0
watcher.add(path)
fn.watcher.add(path)
}}
})
}
} else {
// Bypass requested.

// Create a noop cleanUp() method so that it can be called without
// having the calling app having to check for whether we are running
// in production mode or not.
fn.cleanUp = function (done) {
// Do nothing on purpose.
done()
}
}

if (root) {
Expand Down

0 comments on commit 125ccdb

Please sign in to comment.