8

Consider there is a task A and other n tasks. I wan to run a task A in parallel to other n tasks. Task A is just fetching data from queue in every 5 seconds.

I am new to Node JS. Is there any way to run this task/job A in background or is there any solution ??

user2874299
  • 81
  • 1
  • 1
  • 3
  • Yes, of course there is. Not sure how to provide you any more information without knowing what you are trying to do. Define "background job"... you mean at a system level, or within your application? – Brad Oct 13 '13 at 19:13
  • 1
    Actually it is not possible within single NodeJS process, since NodeJS is single-threaded, i.e. there is no parallelism at all. – freakish Oct 13 '13 at 19:16
  • I agree with @Brad, There are so many ways to make things happen in parallel that I don't think there's enough information in the question to be able to make a considered recommendation. You'd take different approaches depending on whether tasks were local or remote, interdependent or not, had to be balanced across different servers, etc. What are you trying to do? – Richard Marr Oct 13 '13 at 21:01
  • @freakish That isn't entirely accurate. There's only one thread for your JavaScript, but other threads for IO and what not. It doesn't matter though, without any other information there's no way to know what he's trying to do, and whether or not it would require additional threads. – Brad Oct 13 '13 at 21:06
  • Hi, This is what I am trying to do... I have two methods, say X and Y, both methods pushes some data in a queue. Method X and Y are called 20 times like X(), Y(), X(), Y()....and so on. In parallel to execution of these methods, I want to retrieve data from queue at regular intervals and perform some operation with data. – user2874299 Oct 14 '13 at 16:02

3 Answers3

11

Depends a lot on what the tasks are. If I understand your question, you can do this two ways: 1, run a function with a timer, and 2, spawn a child process.

1

function taskA(){...}

setInterval(taskA,5000);

2

//same code as 1, but in a child process
var spawn = require('child_process').spawn,
ls    = spawn('taskA.js');
//taskA.js has the code from example 1

You might prefer 2 to 1 if you are doing a lot of other things in the main process, because node is single threaded. It should also be noted that there are likely better ways to do this in certain circumstances. For example, in a cloud-based webapp, I might rely on the PAAS's services to run the background task. You also might want to look into https://github.com/nodejitsu/forever-monitor

Here's a great article on how to handle background jobs in webapps. https://devcenter.heroku.com/articles/background-jobs-queueing It isn't node specific, however. It is also specific to the Heroku platform.

Zeke Nierenberg
  • 2,216
  • 1
  • 19
  • 30
  • Thanks a lot..! Earlier I was using just setInterval. But setInterval code has to wait for n tasks to complete first to take it's turn. Spawning a process and run setInterval seems to be right way to do it. What exactly is the difference between spawn and fork in node? – user2874299 Oct 14 '13 at 15:02
  • 1
    Great answer here: http://stackoverflow.com/questions/17861362/node-js-child-process-difference-between-spawn-fork. Would you mind marking it correct? – Zeke Nierenberg Oct 14 '13 at 18:12
  • This is not running in a browser so this is obviously not a solution. The program is going to call setInterval (or setTimeout if you used that) and just fall off the end and terminate. – Rick O'Shea Apr 21 '20 at 22:47
  • @RickO'Shea, thanks for your comment. This isn't the behavior I experience. If I create a node script with `setInterval(() => {}, 1000);` in it, it never ends the process until I kill it. I believe timers, open connections, etc keep the process running. – Zeke Nierenberg Apr 23 '20 at 19:59
1

You can use the standard setTimeout() method.

function task() {
  console.log("Timer");
  setTimeout(task, 5000);
}

task();
Markus
  • 192
  • 6
0

Have a look at Kue - https://github.com/LearnBoost/kue This is a node message queue system that's easy to use. Basically you'd have one node program that puts things onto the queue, and other processes that deal with the queue.

So, Task A would be run every 5 seconds (cron if you like, or another system). It would look at the Queue and deal with relevant items. The other n workers would share config for Kue but would only push events on.

Luke H
  • 3,125
  • 27
  • 31