Search code examples
node.jsoptimizationproductionpm2

PM2 Production Optimization


I just tried deploying node app on a "production" environment.

I use PM2 for running the app by using this command. pm2 start app.js --name <appname>

My questions are:

  1. Why I can't see the console.log of the child_process I forked?
  2. Is it okay to call many child_process.fork?

EDIT 1

Basically, what the app do is watching a folder using chokidar. When there's a new file, it will check the file type, then it will do one of the following items:

  1. If the file type is .FLV the app will add a document to a MongoDB collection hosted in mLab.
  2. If the file type is .PNG the app will upload the file to S3 bucket using child_process.fork.
  3. If the file type is .MP4 the app will upload the file to S3 bucket using child_process.fork, then run and FFmpeg command to generate a screenshot that runs on another child_process.fork which then trigger process number 2 above, it also does a MongoDB update on another child_process.fork.

This is the code snippets

1. Chokidar Watcher (app.js)

Chokidar Watcher


EDIT 2

Added code snippets for how I use chokidar and forking a child process.


Solution

  • To get the children's stdout and stderr to use the parent's pipe, you need to tell it to do so as part of the spawn command. This is explained in the docs:

    https://nodejs.org/api/child_process.html#child_process_child_stdio

    It's fine to spawn other processes as needed, subject to whatever system limits you have, though code sample seems to just spawn one at a time.