Search code examples
javascriptnode.jsexpressworker

Start Node.js project with worker


I have a requirement to run a node js project with worker scripts

My folder structure as follows

Projects 
    app.js //starting script 
    controllers 
    services 
    routes 
    workers 
        worker.js // worker logic 
        workers.js //forky script to start worker.js file 

In my package json file given like that

{
  "name": "project",
  "version": "1.0.0",
  "main": "app.js",
  "scripts": {
    "start": "node app.js && ./workers/workers "
  }
}

workers.js

const { cpus } = require('os');
const { fork } = require('child_process');
const numWorkers = cpus().length;
console.log('numWorkers', numWorkers);
for (let i = 0; i < numWorkers; i += 1) {
  fork('./worker');
}

I am starting my project by 'npm start', here app.js file is running but my workers didn't start. I am not sure this is the way to start a worker project along with app. Your help is much appreciated.

Thank you


Solution

  • your file workers.js will never called. I will give you breif explanation. According to your package.json start script "node app.js && ./workers/workers" workers.js file will only be called after successfull execution of app.js file.But in app.js file we have a server listening on some xxxx port so the app.js file will never stop its execution unless you stop it using ctrl+c command. But only after the completing execution of app.js file your wokers.js file will be called according to your start script.The best way to call your workers.js file is call it in your app.js file ex:-

    const app = require('express')();
    require('./workers/workers.js');
    app.listen(3000);
    

    or the one more way (usually I dont recommend this for your use case) use npm-run-all

    "start": "npm-run-all --parallel server workers",
    "server":"node server.js",
    "workers":"node ./workers/workers.js"