Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add graceful_ipc option like in node-dev package #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/ts-node-dev
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ var opts = minimist(devArgs, {
'interval',
'debounce',
'watch',
'restart-terminated'
'restart-terminated',
'graceful_ipc'
],
alias: {
transpileOnly: 'T',
Expand Down
25 changes: 19 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
})
compiler.writeReadyFile()
}
const sendGracefulIPC = (graceful_ipc) => {
if (!child) return
log.debug(`Sending "${graceful_ipc}" ipc message to child`)
child.send(graceful_ipc);
}
const killChild = () => {
if (!child) return
log.debug('Sending SIGTERM kill to child pid', child.pid)
Expand All @@ -191,11 +196,15 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
child.stopping = true
child.respawn = true
if (child.connected === undefined || child.connected === true) {
log.debug('Disconnecting from child')
child.disconnect()
//if (!willTerminate) {
killChild()
//}
if(opts.graceful_ipc){
sendGracefulIPC(opts.graceful_ipc)
} else {
log.debug('Disconnecting from child')
child.disconnect()
//if (!willTerminate) {
killChild()
//}
}
}
}

Expand Down Expand Up @@ -237,7 +246,11 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
// Relay SIGTERM
process.on('SIGTERM', function() {
log.debug('Process got SIGTERM')
killChild()
if(opts.graceful_ipc){
sendGracefulIPC(opts.graceful_ipc)
} else {
killChild()
}
if (opts['restart-terminated']) {
var timeout = opts['restart-terminated'] || 0
log.info('Restarting terminated in ' + timeout + ' seconds')
Expand Down
7 changes: 7 additions & 0 deletions test/fixture/graceful-ipc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
console.log('v1')

process.on('message', function(m) {
console.log(m);
process.disconnect();
process.exit();
})
16 changes: 16 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,19 @@ test('It allows use custom TS Transformers', async (t) => {
await ps.waitForLine(/transformed/)
await ps.exit()
})

// TODO: fix --graceful_ipc option won't work with spawnTsNodeDev()
// p.s the option works when manually running node on the terminal like this:
// node .\bin\ts-node-dev --graceful_ipc=AWESOME .\test\fixture\graceful-ipc.ts
test.skip('It send IPC message during restart events', async (t) => {
const ps = spawnTsNodeDev('--graceful_ipc="GRACETERM" graceful-ipc.ts')

// changing the file to trigger restart
setTimeout(() => replaceText('graceful-ipc.ts', 'v1', 'v2'), 250)

await ps.waitForLine(/GRACETERM/)

await ps.exit()

replaceText('graceful-ipc.ts', 'v2', 'v1')
})