Running delayed jobs in loop

593 views Asked by At

I am trying to put a function in delay. The delay will start in a loop:

class Test
  def check_function1
   while a < b  do
     self.delay.check_function2(t1, t2)
   end
  end
  def check_function(t1, t2)
    #SAVING DATA
  end
end

If the loop runs twice, will that mix up the data here? Is there any way to rectify? Also can we do any sleep after one loop?

1

There are 1 answers

0
Casper On

Delayed Job runs a polling daemon in the background that polls the jobs table at regular intervals for new jobs.

If you only run one DJ worker, then all delayed tasks will be processed in sequence and the potential of a race condition isn't there.

If you run multiple workers then you need to take into account race conditions and guard against "mixing up data" (I assume this is what you meant with "mix up the data").

So yes, running multiple delayed jobs has the potential to "mix data" IF you run multiple DJ workers.

See here for a more detailed explanation:
http://ternarylabs.com/2012/04/16/handle-job-queue-workers-concurrency-in-rails/

Sleeping when launching jobs has practically no effect on what happens to the job queue. Generally you would not want to sleep as it doesn't really do anything other than delay the user interface of your Rails app.