tutukin / tasks-queue

Put tasks in a queue and process them one by one, but not too often
MIT License
1 stars 0 forks source link

Continuously running queue #3

Closed kokujin closed 10 years ago

kokujin commented 10 years ago

I am trying to make sure that a queue runs continuously. The snippet below fails, can someone tell me what I am doing wrong? Thanks

function taskRunner(){ if(q.length() == 0){ console.log('All tests done!'.green); q.pushTask('task',{n:40}); q.pushTask('task',{n:50}); taskRunner();
}
q.execute();
}

taskRunner();

tutukin commented 10 years ago

Hi, kokujin,

looks like an infinite recursion.

Please check the docs on 'autostop' option. If you choose 'autostop' to be true (default), please add listener to 'stop' event that adds more tasks and executes the queue again. Alternatively, set autostop option to false to make the queue wait for tasks forever.

Example 1. (autostop)

var TasksQueue = require('tasks-queue');
var q = new TasksQueue();

q.setMinTime(1000);

q.on('stop', taskRunner);
q.on('task', ontask);

function taskRunner (jinn) {
    var q = jinn.getQueue();
    console.log('All tests are done');
    q.pushTask('task',{n:40});
    q.pushTask('task',{n:50});
    q.execute();
}

function ontask (jinn, data) {
    console.log('n = %d', data.n);
    jinn.done();
}

q.execute();

Example2 (noautostop)

var TasksQueue = require('tasks-queue');
var q = new TasksQueue({autostop:false});

q.setMinTime(100);
q.on('task', ontask);
q.execute();

setInterval(taskRunner, 1000)

function ontask (jinn, data) {
    console.log('n = %d', data.n);
    jinn.done();
}

function taskRunner () {
    console.log('All tests are done');
    q.pushTask('task',{n:40});
    q.pushTask('task',{n:50});
}
tutukin commented 10 years ago

Closing since there's no reply.