Alexander Beletsky
Alexander Beletsky

Reputation: 19831

Infinite execution of tasks for nodejs application

Suppose I have code, like this

function execute() {
    var tasks = buildListOfTasks();
    // ...
}

buildListOfTask creates array of functions. Functions are async, might issue HTTP requests or/and perform db operations.

If tasks list appears empty or all tasks are executed, I need to repeat same execute routine again. And again, in say "infinite loop". So, it's daemon like application.

I could quite understand how to accomplish that in sync-world, but bit confused how to make it possible in node.js async-world.

Upvotes: 3

Views: 4926

Answers (3)

Peter Lyons
Peter Lyons

Reputation: 146094

use async.js and it's queue object.

function runTask(task, callback) {
    //dispatch a single asynchronous task to do some real work
    task(callback);
}
//the 10 means allow up to 10 in parallel, then start queueing
var queue = async.queue(runTask, 10);

//Check for work to do and enqueue it
function refillQueue() {
  buildListOfTasks().forEach(function (task) {
    queue.push(task);
  });
}    

//queue will call this whenever all pending work is completed
//so wait 100ms and check again for more arriving work
queue.drain = function() {
  setTimeout(refillQueue, 100);
};

//start things off initially
refillQueue();

Upvotes: 6

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123533

If you're already familiar with libraries like async, you can use the execute() as the final callback to restart the tasks:

function execute(err) {
    if (!err) {
        async.series(buildListOfTasks(), execute);
    } else {
        // ...
    }
}

Upvotes: 1

MiLk
MiLk

Reputation: 86

I think you have to use async.js, probably the parallel function. https://github.com/caolan/async#parallel

In the global callback, just call execute to make a recursive call.

async.parallel(tasks,
    function(err, results){
        if(!err) execute();
    }
);

Upvotes: 0

Related Questions