I want to be able to use readline, quit the current process and restart it, keeping stdin as well as readline capabilites.
Example code follows:
var readline = require('readline');
var fork = require('child_process').fork;
console.log("argv:", process.argv);
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', (input) => {
console.log(`>>>: ${input}`);
if (input==="exit"){
process.exit();
return;
}
if (input==="restart"){
rl.close(); // whatever, has no effect
restartApp();
return;
}
});
function restartApp() {
process.on('exit', (code) => {
console.log(`About to exit with code: ${code}`);
var forked = fork(process.argv[1], ["RESTARTED"]);
});
process.exit();
}
When this script runs it will wait until the user types an "exit" or "restart" string.
All is nice until it restarts and one keystroke is sent on stdin, throwing the following error:
events.js:161
throw er; // Unhandled 'error' event
^
Error: read EIO
at exports._errnoException (util.js:1028:11)
at TTY.onread (net.js:572:26)
How do I want to prevent this error?
I was reading child_process documentation as well as readline, but something is missing.
It seems that keystrokes are being sent to the old process.
Yes, I know nodemon, forever and pm2 modules. They can "solve" this problem, but I want to learn what I'm missing.
via ED209
No comments:
Post a Comment