vsivsi / meteor-job-collection

A persistent and reactive job queue for Meteor, supporting distributed workers that can run anywhere.
https://atmospherejs.com/vsivsi/job-collection
Other
388 stars 68 forks source link

Cannot get pollInterval to work properly #226

Open callmephilip opened 7 years ago

callmephilip commented 7 years ago

I am having a hard time trying to get my job queue. Given the following setup:

const Jobs = new JobCollection("Jobs", {
  noCollectionSuffix: true
});

Meteor.startup(() => {
  Jobs.processJobs("import/media/sync", { pollInterval: 30 * 1000, payload: 5 }, (jobs, callback) => {
    _.each(jobs, job => {
       // do the job thing here
       job.done();
    });

    callback();
  });
});

// jobs are added like so:
new Job(Jobs, "import/media/sync", {}).priority("normal").save({ cancelRepeats: true });

I expect "import/media/sync" to be called every 30 seconds with 5 jobs (when available). Instead, when timing the calls, I see the worker invoked pretty much immediately after the previous worker finished processing. Am I missing something or misusing the API?

vsivsi commented 7 years ago

Hi, job-collection is optimized for throughput. the pollInterval is the rate at which processJobs will ask for work when its local queue is completely empty. When the queue is not empty, it also asks for work each time a job finishes (when the callback is invoked in the worker function).

So in your case, it will request more work immediately after to invoke callback(). This is by design.

If my understanding of what you want is correct, you can achieve it by simply doing:

// Replace callback(); with:
Meteor.setTimeout(callback, 5000)