Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js workers/background processes

How can I create and use background jobs in node.js?

I've come across two libs (node-resque and node-worker) but would like to know if there's something more used.

like image 548
donald Avatar asked Jan 21 '11 17:01

donald


People also ask

What is worker process in Node js?

Worker Threads in Node. js is useful for performing heavy JavaScript tasks. With the help of threads, Worker makes it easy to run javascript codes in parallel making it much faster and efficient. We can do heavy tasks without even disturbing the main thread.

Why is Node js not suitable for CPU-intensive work?

The single-threaded implementation makes Node a bad choice for CPU-intensive programs. When a time-consuming task is running in the program it blocks the event loop from moving forward for a longer period.

What is the use of the worker_threads module in Node js?

The node:worker_threads module enables the use of threads that execute JavaScript in parallel. To access it: const worker = require('node:worker_threads'); Workers (threads) are useful for performing CPU-intensive JavaScript operations.


2 Answers

I did some research on this and I would do it like this.

Setup

beanstalkd

  1. Install beanstalkd. Another message queue, BUT this one supports DELAYED PUTS. If you compile from source it is going to be a little harder because it depends on libevent(like memcached). But then again, I don't think you have to compile it from source, because there are a lot of binary packages available. For example on Ubuntu you can install beanstalkd by issuing the command:

    sudo apt-get install beanstalkd

node-beanstalk-client

  1. Install a beanstalkd client library. The best one I found was node-beanstalk-client. Because on the beanstalkd client library list this library isn't/wasn't mentioned(Then again I can add entries to the list, so I will add this one). The reasons I prefer this library over the others are:

    1. Npm package: I liked to use a npm package to install client library. The others did not have any.
    2. Active development: I prefer libraries which have later/more commits.

So to install it, after you have installed npm(the write way) you would just issue the following command:

npm install beanstalk_client 

Code

consumer.js

var client = require('beanstalk_client').Client;  client.connect('127.0.0.1:11300', function(err, conn) {     var reserve = function() {         conn.reserve(function(err, job_id, job_json) {             console.log('got job: ' + job_id);             console.log('got job data: ' + job_json);             console.log('module name is ' + JSON.parse(job_json).data.name);             conn.destroy(job_id, function(err) {                 console.log('destroyed job');                 reserve();             });         });     }      reserve(); }); 

First start the consumer:

node consumer.js  

Next start producer.js. Five seconds(delay I specified) after you execute producer.js, consumer.js will process the message.

producer.js

var client = require('beanstalk_client').Client; client.connect('127.0.0.1:11300', function(err, conn) {     var job_data = {"data": {"name": "node-beanstalk-client"}};     var priority = 0;     var delay = 5;     var timeToRun = 1;     conn.put(priority, delay, timeToRun, JSON.stringify(job_data), function(err, job_id) {         console.log('put job: ' + job_id);         process.exit();     }); }); 

to start just issue:

node producer.js 
like image 114
Alfred Avatar answered Sep 30 '22 17:09

Alfred


I'm using Kue.

Kue is a feature rich priority job queue for node.js backed by redis. A key feature of Kue is its clean user-interface for viewing and managing queued, active, failed, and completed jobs.

like image 35
webjay Avatar answered Sep 30 '22 18:09

webjay