Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Processing items synchronously #19

Closed
tciuro opened this issue Jan 7, 2014 · 1 comment
Closed

Processing items synchronously #19

tciuro opened this issue Jan 7, 2014 · 1 comment

Comments

@tciuro
Copy link

tciuro commented Jan 7, 2014

Hello,

I have the following scenario: my queue contains three scheduled items (i.e. A, B, C). The external process that handles these items cannot be launched multiple times at the same time. For this reason, I would need Agenda to process A, wait until it's completed (or failed), then process B, wait, then finally C.

If I understand the documentation correctly, I would have to invoke Agenda like so using your example in the documentation:

agenda.define('some long running job', function(job, done) {
  doSomelengthyTask(function(data) {
    formatThatData(data);
    sendThatData(data);
    // done();
  });
});

I should remove done() because I want Agenda to process the scheduled items synchronously, correct?

Also, what is the content type and value of job and done? What should I be expecting back?

Thanks!

@rschmukler
Copy link
Collaborator

Not calling done would still allow multiple jobs to be run at once, specifically on per-job concurrency, or entire queue concurrency. A job will be considered "finished" when you call done(). If you do not take done as an argument, then it will be marked as done when the function reaches the end. You shouldn't take done as an argument if you aren't going to call it, as then agenda will

If you want agenda to only ever run one job at a time, you can do the following:

agenda.maxConcurrency(1);

This tells agenda to only ever allow one job to be processing at once.

If you want to specify it for a specific job, you can do:

agenda.define('some job', {concurrency: 1}, function(job, done) {
});

So, for example:

agenda.maxConcurrency(3);

var start = new Date();

agenda.define('longer job', {}, function(job, done) {
   setTimeout(function() {
      var now = new Date();
      console.log("Hello one - %d ms later", now - start);
      done();
   }, 1700);
});

agenda.define('long job', {concurrency: 1}, function(job, done) {
   setTimeout(function() {
      var now = new Date();
      console.log("Hello two - %d ms later", now - start);
      done();
   }, 1000);
});

agenda.now('longer job');
agenda.now('longer job');
agenda.now('long job');
agenda.now('long job');
agenda.now('longer job');
agenda.start();

Results in the following:

Hello two - 1026 ms later
Hello one - 1724 ms later
Hello one - 1725 ms later
Hello two - 2028 ms later
Hello one - 3425 ms later

As for the callback signature:

job is a Job instance. See here for documentation.

done is a function which lets the agenda know that the job finished, and tells it to queue up the next job, given the current state of running jobs and concurrency limitations.

Hope this helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants