A little toolkit for running and managing child processes
$ npm install little-process-boxDevelopment/Testing/Documentation
const { Supervisor } = require('little-process-box')
const supervisor = new Supervisor()
const pool = supervisor.pool()
supervisor.service('http-server', {
pool,
exec: 'node',
args: ['server.js'],
env: { PORT: 3000 }
})
supervisor.service('discovery', {
pool,
exec: 'node',
args: ['discovery.js'],
env: { PORT: 9000 },
})
supervisor.start((err) => {
// http + discovery should both started
supervisor.stat({name: 'http-server'}, (err, stats) => {
console.log(stats)
})
supervisor.stat({name: 'discovery'}, (err, stats) => {
console.log(stats)
})
supervisor.stop() // stops all services
})The ProcessPool class represents a container of running processes
managed by the instance. This class extends the Pool class from the
nanoresource-pool module. You can override the
default factory by supplying your own Factory that should
implement the nanoresource
Creates and spawns a command in a new process managed by the pool.
Collects the stats of all running process
calling callback(err, stats) upon success or error. The
where object can be used to filter the results that get queried
for stats.
Launches a program by way of an input pathspec that may be an
application URL, path to an application directory, or something
suitable for the operating system to launch ("open").
The Supervisor class represents a higher level container for
ProcessPool instances in which it provides abilities to manage processes
as services with consolidated logging, mutex locks, graceful shutdowns, and
connected message channels.
A list of all pools.
Creates a new SupervisorProcessPool instance.
Creates a named service with options from a new or
given pool (opts.pool).
Queries all services in all process pools based on an optional
where filter calling callback(err, results) upon success or
error.
Starts all services in all pools calling callback(err) upon success
or error.
Stops all services in all pools calling callback(err) upon success
or error.
Restarts all services in all pools calling callback(err) upon success
or error.
The SupervisorProcessPool class represents an extended
ProcessPool that creates Service instances
as the process resource.
The Service class represents a named process that can be started,
restarted, and stopped. The Service class inherits all properties from
the Process class.
Environment object for the service.
The pool that created this service.
The service type. This can be anything.
The service name. This can be anything.
The command used to execute the service.
The command arguments used to execute the service.
A description of the service.
A Writable stream for the service's stdin.
A Readable stream for the service's stdout.
A Readable stream for the service's stderr.
true if the service has successfully started.
true if the service is in the middle of starting.
true if the service is stopped.
Starts the services calling callback(err) upon success or error.
Stops the services calling callback(err) upon success
or error. This function will reset the nanoresource
state (opened, opening, closed, closing, actives) to their
initial values.
Restarts the services calling callback(err) upon success
or error. This function will call service.stop(callback)
Same as [Process#stat()][nanoprocess#stat].
Returns a readable stream to the caller that pipes stderr from the
running service to the stream. If the service does not have a
readable stderr stream, then the returned stream will automatically
end.
The Process class exported from
nanoprocess.
The Channel class represents a channel for a running process that
leverages IPC channels falling back to stdin and stdout streams to
send and receive messages on where process can be a
Process instance, a
ChildProcess
instance, or the global
process object.
Sends a buffer to the process through the message channel where opts can
be:
{
id: 0, // The channel ID to send this on
type: 0, // The message type
}Closes channel calling callback(err) upon success or error.
Close process message channel and cleans up all resources.
Emitted when a message a sent over the process channel where message
is a Buffer of the message sent, id is the channel
identifier and type is the message type.
Emitted when an error occurs on the channel process during the life
time of the Channel instance.
Emitted when the channel closes.
MIT