-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added watch functionality * added watch env var * changed test/start.js * updated help text * added watch test * updated help/start.text * updated README.md * gracefully shutdown * [improve] gracefully shutdown#2 * handle uncaughtException * child process exit in fastify.close(), don't need wait FULLY_CLOSED message * remove setTimeout when trigger fastify ready event * remove setTimeout in watch functionality test * unref GRACEFUL_SHUT event timer and use process.exit(1) instead of process.send({ type: FULLY_CLOSED, err: null }) * use pass callback to fastify.close() instead of fastify.addHook('onClose', function () {...}) * change process.send({ type: FULLY_CLOSED, err: null }) to process.exit(0) in fastify.close() * [improve] gracefully shutdown#3 * call unref() the child process.exit(1)'s timeout before calling fastify.close() * skip watch functionality test on travis * add lib/watch/constant.js file
- Loading branch information
1 parent
cfbd180
commit 02d7d1e
Showing
10 changed files
with
254 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,3 +50,4 @@ test.sock | |
|
||
# test artifacts | ||
test/workdir | ||
test/fixtures/*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
GRACEFUL_SHUT: 'GRACEFUL SHUTDOWN', | ||
READY: 'ready', | ||
TIMEOUT: 5000 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const chalk = require('chalk') | ||
const { stop, runFastify } = require('../../start') | ||
|
||
const { | ||
GRACEFUL_SHUT, | ||
READY, | ||
TIMEOUT | ||
} = require('./constants.js') | ||
|
||
const fastify = runFastify(process.argv.splice(2)) | ||
const type = process.env.childEvent | ||
|
||
process.send({ type: type, err: null }) | ||
|
||
fastify.ready(err => { | ||
process.send({ type: READY, err: err }) | ||
if (err) { stop(err) } | ||
}) | ||
|
||
process.on('message', function (event) { | ||
if (event === GRACEFUL_SHUT) { | ||
const message = chalk.red('[fastify-cli] process forced end') | ||
setTimeout(exit.bind({ message }), TIMEOUT).unref() | ||
fastify.close(() => { | ||
process.exit(0) | ||
}) | ||
} | ||
}) | ||
|
||
process.on('uncaughtException', (err) => { | ||
console.log(err) | ||
const message = chalk.red('[fastify-cli] app crashed - waiting for file changes before starting...') | ||
// fastify.close(exit) // It's looks that only happend fastify normally startup. so we should detect fastify is normally runing, it's normally running that graceful exit happen, other case I think it's better immediately exec process.exit(1) | ||
// setTimeout(exit.bind({ message }), TIMEOUT).unref() | ||
exit.bind({ message })() | ||
}) | ||
|
||
function exit () { | ||
if (this) { console.log(this.message) } | ||
process.exit(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
const path = require('path') | ||
const cp = require('child_process') | ||
|
||
const { | ||
GRACEFUL_SHUT | ||
} = require('./constants.js') | ||
|
||
const EventEmitter = require('events') | ||
const chokidar = require('chokidar') | ||
const forkPath = path.join(__dirname, './fork.js') | ||
|
||
const emitter = new EventEmitter() | ||
|
||
let allStop = false | ||
let childs = [] | ||
|
||
const stop = (watcher = null, err = null) => { | ||
if (childs.length > 0) { | ||
childs.forEach(function (child) { | ||
child.kill() | ||
}) | ||
} | ||
childs = [] | ||
if (err) { console.log(err) } | ||
if (watcher) { | ||
allStop = true | ||
watcher.close() | ||
} | ||
} | ||
|
||
const watch = function (args) { | ||
process.on('uncaughtException', () => { | ||
stop() | ||
childs.push(run('restart')) | ||
}) | ||
|
||
const run = (event) => { | ||
const childEvent = { childEvent: event } | ||
const env = Object.assign({}, process.env, childEvent) | ||
const _child = cp.fork(forkPath, args, { | ||
env: env, | ||
cwd: process.cwd(), | ||
encoding: 'utf8' | ||
}) | ||
|
||
_child.on('exit', function (code, signal) { | ||
if (!code === 0) { stop() } | ||
if (childs.length === 0 && !allStop) { childs.push(run('restart')) } | ||
return null | ||
}) | ||
|
||
_child.on('message', (event) => { | ||
const { type, err } = event | ||
if (err) { | ||
emitter.emit('error', err) | ||
return null | ||
} | ||
|
||
emitter.emit(type, err) | ||
}) | ||
|
||
return _child | ||
} | ||
|
||
childs.push(run('start')) | ||
|
||
const watcher = chokidar.watch(process.cwd(), { ignored: /(node_modules|\.git|bower_components|build|dist)/ }) | ||
watcher.on('ready', function () { | ||
watcher.on('all', function () { | ||
try { | ||
const child = childs.shift() | ||
child.send(GRACEFUL_SHUT) | ||
} catch (err) { | ||
if (!childs.length === 0) { | ||
console.log(err) | ||
stop(watcher, err) | ||
} | ||
} | ||
}) | ||
}) | ||
|
||
emitter.on('error', (err) => { | ||
stop(watcher, err) | ||
}) | ||
|
||
emitter.on('close', () => { | ||
stop(watcher) | ||
}) | ||
|
||
return emitter | ||
} | ||
|
||
module.exports = watch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.