Infant is a helper package that wraps some of the core node modules that are used to provide child process and cluster support.
NOTE For Node 0.10.x use Infant 0.9.5
NOTE For Node 0.12.0+ (including 5.x) use Infant 0.10+
This package comes with two main helpers Child, Cluster which provide enhanced functionality over the basic functionality that the core packages provide. Additionally the Lifecycle helper is provided for organizing complex startup and shutdown sequences with an init.d style interface.
Furthermore, Infant fixes some of the inherent problems with graceful startup and shutdown that are not supported using the raw node modules.
Finally, Infant can be used as a drop in replacement for child_process
and
for cluster
. Additionally there are simple helpers included to enhance
children to communicate with the master and provide features such as:
As this module relies heavily and mainly extends the core node modules. It is imperative to get familiar with these documents as well
Parent
'use strict';
var parent = require('infant').parent
var errorHandler = function(err){
console.error(err)
process.exit()
}
//instantiate the child (same as require('x'))
var child = parent('./myscript')
//start the child
child.start(function(err){
if(err) return errorHandler(err)
console.log('Child started successfully')
//stop the child
child.stop(function(err){
if(err) return errorHandler(err)
console.log('Child stopped successfully')
})
})
Child
'use strict';
var child = require('infant').child
if(require.main === module){
child(
'mychild', //child process title
function(done){
//startup operations
done()
},
function(done){
//shutdown operations
done()
}
)
}
Parent
'use strict';
var parent = require('infant').fork
var errorHandler = function(err){
console.error(err)
process.exit()
}
//run the child
parent('./myscript',function(err){
if(err) return errorHandler(err)
console.log('Child completed successfully')
})
Child
'use strict';
var child = require('infant').childOnce
if(require.main === module){
child(
'mychild', //child process title
function(done){
//script operations
done()
}
)
}
Parent
'use strict';
var cluster = require('infant').cluster
var master = cluster('./mychild')
var errorHandler = function(err){
console.error(err)
process.exit()
}
master.start(function(err){
if(err) return errorHandler(err)
console.log('cluster started')
master.stop(function(err){
if(err) return erroHandler(err)
console.log('cluster stopped')
})
})
Child
'use strict';
var http = require('http')
var server = http.createServer(req,res){
res.end('foo')
})
server.listen(3000)
Parent
'use strict';
var cluster = require('infant').cluster
var master = cluster('./mychild',{count: 4, enhanced: true})
var errorHandler = function(err){
console.error(err)
process.exit()
}
master.start(function(err){
if(err) return errorHandler(err)
console.log('cluster started')
master.stop(function(err){
if(err) return erroHandler(err)
console.log('cluster stopped')
})
})
Child
'use strict';
var http = require('http')
var worker = require('infant').worker
var server = http.createServer(req,res){
res.end('foo')
})
//setup the worker with advanced features
worker(server)
server.listen(3000)
'use strict';
var Lifecycle = require('infant').Lifecycle
var lifecycle = new Lifecycle()
//hook some events for logging
lifecycle.on('start',function(item){
console.log('Starting ' + item.title)
})
lifecycle.on('stop',function(item){
console.log('Stopping ' + item.title)
})
lifecycle.on('online',function(){
console.log('Startup complete')
})
lifecycle.on('offline',function(){
console.log('Shutdown complete')
})
//add a new startup accompanied by a shutdown
lifecycle.add(
'step 1',
function(done){
//startup stuff
done()
},
function(done){
//shutdown stuff
done()
}
)
//start the members of the lifecycle
lifecycle.start(function(err){
if(err) throw err
lifecycle.stop(function(err){
if(err) throw err
//shutdown complete
})
})
module
- Takes an argument similar to requireoptions
- Optional bject of optionsOptions
respawn
- (boolean) defaults to true (restart the process on exit)fork
- (object) options passed to child_process.fork()
see the node
documentationThis function takes no arguments and returns the current status
Status definitions
dead
- Nothing running, not configuredstarting
- Startup in progressstopping
- Shutdown in progressready
- Process is ready to startok
- Process is runningTakes only one argument, callback which is called cb(err)
on process
startup completion. Errors bubble from the children.
Takes either a callback as the only parameter or a timeout in ms and a callback which will shutdown the process without a kill timeout.
If timeout is omitted the process has an unlimited amount of time to shutdown.
This function is sync and forcefully kills the child. (SIGTERM
by default)
This is also called automatically on any running process during
process.on('exit')
with SIGKILL
Send the child a message through the IPC.
Takes any data type for msg that can be serialized and passed to the child.
The child receives the message through the process.on('message')
event.
This static function will execute a child that dies on completion of execution. It is considered a one time child.
module
- (string) file name to use (similar to require('x'))options
- (object) optional object of options
respawn
- (boolean) respawn a failed process (default: true
)timeout
- (number) kill the process after specified timeout
(default: null
)cb
- Called on completion with optional error cb(err)
NOTE If the callback is omitted the process will not be started and timeout functionality will not be implemented. The instance of the Child object is returned to be augmented manually.
Shortcut for the main constructor
This is a wrapper function for children to setup IPC communication for graceful startup and shutdown. Use this only in the child.
title
- String that defines the process titlestart
- Function that is called with a single parameter done
which is a
callback that should be fired when startup is complete, can be passed an
optional error as the only argument done(err)
stop
- Same as the start function, only used for shutdown.This wrapper is similar to Child.child
but is used to run process that
runs and exits immediately (childOnce)
title
- String that defines the process titleexec
- Same as the start
function in Child.child
status
- emitted when the status changes, args: status
the new statusexit
- emitted when the child exits, args: code
the exit code of the childclose
- emitted when the child closeserror
- emitted during an error, args: err
the errorrespawn
- emitted when the process respawns, args: pid
the pid of the
new processmessage
- emitted when the child process sends a message, args: msg
the
message sent by the childThe constructor only arms the instance, it should also be noted that this
class must be a singleton since a master can only maintain a single instance
of the cluster
module.
That is why it is not exposed as the default operator, use
require('infant').cluster
instead which takes the same parameters as this
constructor.
module
- File name to execute for workers (same as require('x'))
defaults to process.argv[1]
options
- optional bbject of options defined belowOptions
enhanced
- (boolean) default false, enable enhanced worker moderespawn
- (boolean) default true, enabled worker respawn on unexpected exitcount
- (number) number of workers to start, defaults to os.cpus().length
maxConnections
- (number) only available in enhanced mode, but will cause
a worker to be shutdown and a new one started (recycled) when the worker
achieves maxConnections.stopTimeout
- (number) Timeout in ms
to wait for workers to stop, defaults
to no timeout when in enhanced mode, however it defaults to 5000
in normal
mode.recycleTimeou
t - (number) Timeout in ms
to wait for a worker to stop when
it is being recycled, similar to stopTimeout, defaults to 5000
and must be
definedexecArgv
- (array) passed through to cluster.setupMaster()
see the node
documentationsilent
- (boolean) passed through to cluster.setupMaster()
see the node
documentationargs
- (array) passed through to cluster.setupMaster()
see the node
documentationenv
- (object) passed through to cluster.fork(env)
see the node
documentationExecute a callback on each worker that is currently running.
cb(worker)
Send each worker in the cluster the msg defined as msg
Start a new worker which will be boot strapped with advanced features in enhanced mode
This is an internal function that is used to add enhanced functionality to workers such as recycling.
Start the cluster and call cb(err)
when the cluster is online.
Stop the cluster and call cb(err)
when the cluster is offline.
Restart the cluster and call cb(err)
when complete.
This is an internal function used to respawn workers on unexpected exit
Kill all the workers with the given signal
defaults to SIGTERM
server
- (HTTP) Instance of HTTP server to be extendedtitle
- (string) Process titlestart
- (function) startup function passed done(err)
stop
- (function) shutdown function passed done(err)
Take an instance of the node HTTP server and wire to use enhanced features with the master, this should only be called in the child.
Also implements graceful startup and shutdown.
It is alias as require('infant').worker
online
- Emitted any time a new worker comes online, args: worker
recycle
- Emitted when a worker is recycled, args: worker
,
connectionCount
started
- Emitted on cluster startexit
- Emitted any time a worker exits, args: worker
, code
, signal
respawn
- Emitted when a worker respawns, args: worker,
code, signal
it should be noted that worker
is the new worker, while code
and signal
are from the previous workers exitstopping
- Emitted on beginning of cluster shutdownstopped
- Emitted on completion of cluster shutdownTakes no arguments, returns a new lifecycle instance
Title is a string that will be provided during events
Where start and stop are functions that are passed a done(err)
callback
Remove the member from the lifecycle, using the title to identify the member
Will start all the members in the order they were added and call done(err)
when complete.
Will stop all the members in reverse order that they were added and call
done(err)
when complete.
add
- Emitted when a member is added, args: item
the item being
addedremove
- Emitted when a member is removed, args: item
the item being
removedstart
- Emitted when a member is started, args: item
the item being
startedstop
- Emitted when a member is stopped, args: item
the item being
stoppedonline
- Emitted when the startup sequence is completeoffline
- Emitted when the shutdown sequence is completeIt is useful to see the interprocess communication for debugging and just overall feel that the system is working.
This package is built using the https://www.npmjs.org/package/debug package.
Use the following to see debug output
$ DEBUG=infant* node app
worker.disconnect()
to ensure that all existing connections are handled properly.oose
labelingChild.fork()
env
to options in order to pass environment variables to any calls to
cluster.fork()
Cluster
helperasync
was in devDependencies
require('infant').Cluster
now available for raw class accessLanguage | javascript |
Version | 0.12.0 |
Git URL | https://github.com/nullivex/infant |
License | MIT |
Description | |
Keywords |