Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • B bull
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 175
    • Issues 175
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 9
    • Merge requests 9
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OptimalBits
  • bull
  • Issues
  • #2151
Closed
Open
Issue created Sep 02, 2021 by Max@derN3rd

[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.

Assignee
Assign to
Time tracking