single-child
Spawn a single child process which kills itself on restart.
This was built as a module of listen-spawn, a CLI tool that starts a server and runs a command every time it is pinged; a nodemon over HTTP.
Supported and tested on Linux and Windows.
Getting Started
Install the module with: npm install single-child
Below is a simplified implementation of listen-spawn:
// Inside of app-restarter.js// Create a new child which starts my appvar SingleChild = child = 'npm' 'start' stdio: 012; // Start my child (and hence my app)childstart; // Begin a server which restarts the app when hitvar http = ;http; // Notify the user the server is upconsole;
$ # Inside of a shell $ # Start and fork our App Restarter $ node app-restarter.js &App restarter is listening at http://localhost:3000/ > my-app@0.1.0 start /home/todd/github/my-app> echo 'Running app...' Running app... $ # Ping the App Restarter (which restarts the app) $ curl http://localhost:3000/ > my-app@0.1.0 start /home/todd/github/my-app> echo 'Running app...' Running app...
Documentation
SingleChild
acts as a constructor and accepts the same parameters as child_process#spawn with a few extras.
cmd args options;/** * Spawn for a single child. Always guarantees only one child exists * @see child_process.spawn * @param * @param * @param * @param */
Methods
SingleChild#start
and SingleChild#restart
are how we start children. These are aliases.
childstartcb;child;/** * Stop the currently running child and start a new one * @param * @callback * @callback */
SingleChild#stop
and SingleChild#kill
are how we stop children. stop
invokes kill
with SIGINT
rather than allowing for input.
child;child;/** * Stop the currently running program * @param * @param * @param * Receives same params as node's ChildProcess#exit event */
Events
SingleChild
fires the following events over its lifecycle:
starting()
is run before a child is spawnedstarted(child)
is run after a child is spawned (does not mean process is running)exited(code, signal)
is run when a child exitskilling(child)
is run before a child is killedkilled(code, signal)
is run after a child is killed
Examples
Below is the same example as before but utilizing events for better notifications.
// Create a new child which starts my appvar SingleChild = child = 'npm' 'start' stdio: 012; // Start my child (and hence my app)childstart; // Notify user on start and exit eventschild;child; // Begin a server which restarts the app when hitvar http = ;http; // Notify the user the server is upconsole;
$ # Start and fork our App Restarter $ node app-restarter.js &App restarter is listening at http://localhost:3000/Starting app! > my-app@0.1.0 start /home/todd/github/my-app> echo 'Running app...' Running app...App exited! $ # Ping the App Restarter (which restarts the app) $ curl http://localhost:3000/Starting app! > my-app@0.1.0 start /home/todd/github/my-app> echo 'Running app...' Running app...App exited!
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via grunt and test via npm test
.
License
Copyright (c) 2013 Todd Wolfson
Licensed under the MIT license.