Re: [delayed_job] How quickly does DJ pick up new jobs?

196 views
Skip to first unread message

David Genord II

unread,
May 17, 2013, 10:39:28 AM5/17/13
to delay...@googlegroups.com
Short answer: immediately.

Longer answer: it depends.

If there are no jobs in the queue and you add a job to the queue it can take up to 5 seconds for the job to run. This is configured by Delayed::Worker.sleep_delay and is set to 5 by default. How this works in practice, is that the worker tries checks the DB for something to do, if nothing is found the process sleeps for sleep_delay seconds, and then checks again.

If the worker is already working on a job, the new job will get run when all jobs in front of it in the queue are done.

David Genord II


On Fri, May 17, 2013 at 2:53 AM, Arman H <arm...@gmail.com> wrote:
Hello all, new to the list. I appreciate all responses in advance. My question is, when I schedule a new job, how quickly does DJ act upon them? Is there any way to configure this option?

--
 
---
You received this message because you are subscribed to the Google Groups "delayed_job" group.
To unsubscribe from this group and stop receiving emails from it, send an email to delayed_job...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Arman H

unread,
May 18, 2013, 4:29:48 PM5/18/13
to delay...@googlegroups.com
David, thanks for the short and (especially) the long answer! Another question, somewhat unrelated: I noticed DJ processes the que in reverse (e.g. LIFO). Is there a way to change the behavior to FIFO?

David Genord II

unread,
May 19, 2013, 12:15:52 AM5/19/13
to delay...@googlegroups.com
Technically it is not LIFO or FIFO. Jobs are processed in order of priority, lowest value first, then run_at, earliest time first. This is not configurable.

David Genord II

Arman H

unread,
May 19, 2013, 6:29:02 AM5/19/13
to delay...@googlegroups.com
Thanks for clarification. How does DJ determine which jobs to run when all jobs have the same priority and no `run_at` time is specified?

P.S. This is purely out of curiosity. I needed to run jobs in sequential order, so setting the `run_at` property to Time.now, got the job done.

Robert Mathews

unread,
May 19, 2013, 9:49:36 AM5/19/13
to delay...@googlegroups.com
If you need to run them in sequential order, you could queue them with increasing priority.

David Genord II

unread,
May 19, 2013, 11:32:14 AM5/19/13
to delay...@googlegroups.com
What database backend are you using?

David Genord II

Arman H

unread,
May 19, 2013, 3:18:25 PM5/19/13
to delay...@googlegroups.com
Robert, thanks for the reply. In order to enqueue them with increasing priority, I would have to keep track of previous jobs' priorities. That's an extra query to the DB every time I'm creating a new job, which I did not want to do. Instead, I'm setting the `run_at` to current time, which seems to work fine ( at least in development environment).

David, I'm running on PostgreSQL (same as Heroku, that's where the app is staged).

Robert Mathews

unread,
May 19, 2013, 4:09:20 PM5/19/13
to delay...@googlegroups.com
If you were queuing a whole bunch at once the extra query wouldn't matter; If you really need them in order, why don't you have the completion of one of them queue the next calculation?

As for what is wrong with time? Well, you have to choose the granularity of your increment, too fine and the jobs risk running at the same time, too large and it is safe, but slower than optimum. But, hey, whatever works for you.

One difference between development and production might well be the number of queue servers deployed; in development, you have one worker, for us production has 4 queue servers, each with 3 workers.

David Genord II

unread,
May 19, 2013, 8:40:59 PM5/19/13
to delay...@googlegroups.com
Ok, so you said you were seeing jobs with nil run_at values. That is not suppose to happen. By default DJ sets the run at time to Time.now when the job is created. What gem versions are you using for Rails, DJ, and DJ active record?

David Genord II

Bill Burcham

unread,
May 20, 2013, 10:45:16 AM5/20/13
to delay...@googlegroups.com
Arman H, Orchestrated (a Ruby Gem) will let you express dependencies between delayed jobs. The syntax is similar to regular delayed job except "delay" is replaced with "orchestrate". The "orchestrate" method (unlike "delay") lets you specify prerequisites.

Imagine you want to "rinse" to run after you "lather". You could do something like this:

B.new.orchestrate( A.new.orchestrate.lather("shampoo") ).rinse("water")

There are primitives for combining prerequisites too. So if you have more than one prerequisite and you want to run after all are complete you use LastCompletion. If you want to run after the first one is complete, use FirstCompletion.
Reply all
Reply to author
Forward
0 new messages