[Feature Request] Piping stdout from child process into main process
While working with sub-process workers (sandboxed job processors) I tried to find a good way to get debug information within the job code and in general from the process. (Same as #1021 (closed))
The easiest way I came up with is to patch the lib/process/child-pool.js with:
@@ -4,7 +4,6 @@
@@ -4,6 +4,7 @@
const path = require('path');
const _ = require('lodash');
const getPort = require('get-port');
+const { Transform } = require('stream');
const ChildPool = function ChildPool() {
if (!(this instanceof ChildPool)) {
@@ -47,8 +48,21 @@
return convertExecArgv(process.execArgv).then(execArgv => {
child = fork(path.join(__dirname, './master.js'), {
- execArgv
+ execArgv,
+ stdio: 'pipe',
});
+
+ // main folder that includes the node_modules
+ const projectFolder = __dirname.split('node_modules/').shift()
+ const readableProcessName = processFile.replace(projectFolder, '')
+
+ const processNamePrependTransform = new Transform({
+ transform: (chunk, encoding, done) => {
+ const result = `[${readableProcessName}] ${chunk.toString()}`
+ done(null, result)
+ }
+ })
+
+ child.stdout.pipe(processNamePrependTransform).pipe(process.stdout);
+
child.processFile = processFile;
this will pipe any child stdout into the main process stdout with its processFile (e.g. [src/jobs/processImage.js] found invalid image file '/tmp/x.png'
). (Obviously the transform part is not needed and only adds from which process the output came. This could also be extended with the process pid etc)
I think this could be useful for other people as well e.g. behind a config or environment flag.
As I didn't work with fork
before I don't know about negative side effects for this in production environments, but in my local dev setup it worked and helped me a lot already.