Search code examples
node.jscronbackgroundcron-taskbackground-task

NodeJs Bull for Background tasks


I have been using Bull for background tasks in my node application.

https://github.com/OptimalBits/bull

Now that node is single threaded by nature. Does bull use the same thread on which node application is running or it forks another thread and runs as a separate instance?


Solution

  • From bull quick guide:

    The process function can also be run1 in a separate process. This has several advantages:

    • The process is sandboxed so if it crashes it does not affect the worker.
    • You can run blocking code without affecting the queue (jobs will not stall).
    • Much better utilization of multi-core CPUs.
    • Less connections to redis.

    In order to use this feature just create a separate file with the processor:

    // processor.js
    module.exports = function(job){
      // Do some heavy work
    
      return Promise.resolve(result);
    }
    

    And define the processor like this:

    // Single process:
    queue.process('/path/to/my/processor.js');
    
    // You can use concurrency as well:
    queue.process(5, '/path/to/my/processor.js');
    
    // and named processors:
    queue.process('my processor', 5, '/path/to/my/processor.js');
    

    1. This indicates, naturally bull doesn't create separate process for execution.

    Basic guides: https://github.com/OptimalBits/bull#separate-processes