Stress-free node child process spawning and management
Stress-free node child process spawning and management. A small wrapper around forever-monitor with a simple interface.
Not everything has a
node adapter. Often times, there is a need to spawn a child process to run another script (maybe even fork another
Forking and managing child processes in
node can prove challenging. If something fails in the pipe, you can be
left over with
phantom processes running in the background. Over time, these can accumulate and result in strange issues such as blocked ports,
misbehaving debug tools, "random" exits, etc. Even a number of popular
grunt plugins, etc. fail to exit cleanly.
Additionally, the child_process.spawn interface is not very desirable. It was written for flexibility, but the majority of the time it is used the same way. Managing all of this correctly across multiple processes and multiple scripts can be cumbersome and error-prone.
To defer this redudancy and avoid the risk,
spork was created to try to make spawning and forking smoother -- hence the name.
Spork follows a very similar syntax to
node core's child_process.spawn
to keep the interface simple. It also takes advantage of the power of forever-monitor to add some much-needed
robustness to the interface. The syntax should be straight-forward and familiar:
const spork = require'spork';sporkcommand args options;
Besides spawning child processes,
spork can do a few other things:
ANSIcolor character sequences through the pipe
$ npm install --save spork
In its simplest form:
spork'command' exit: true; // process will automatically exit when the command fails or succeeds
Normally, all of this is required for just one process, in order to spawn it, act on data events, and exit cleanly:
const done = async;let child = spawn'command' '--arg1' '--arg2'env: _extendprocessenvWHATEVER: 'isNeeded'stdio: processstdin 'pipe' 'pipe';childstdoutsetEncoding'utf8';childstderrsetEncoding'utf8';childstdouton'data'// .. do somethingprocessstdoutwritedata;;let errorBuffer = '';childstderron'data'errorBuffer += data;;childon'close'if codecustomErrorHandlererrorBuffer code;elsedone;;childon'error'console.errorerr;processexit99;;processstdinresume;processon'exit' exitHandler;processon'SIGINT' _partialexitHandler exitCode: 2;processon'uncaughtException' _partialexitHandler exitCode: 99;if err && errstackconsole.errorerr;processexit1;childkill;if exitData && exitDataexitprocessexitexitDataexit;
Instead, just do this, to produce the same result:
spork'command' '--arg1' '--arg2' env: WHATEVER: 'isNeeded'on'stdout'// .. do somethingon'exit:code'// .. do somethingprocessexitcode;;
spork handle everything:
spork'command' '--arg1' '--arg2' exit: true;
All options here, plus:
false. Defaults to
STDERR. Defaults to
STDERRto the parent process. Defaults to
['inherit', 'inherit', 'inherit'].
You can completely nix the built-in
stdio: [null, null, null]and manage it all yourself.
The MIT License (MIT)
Copyright (c) 2016 Justin Helmer
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.