backburner icon indicating copy to clipboard operation
backburner copied to clipboard

Delay within a worker

Open andrusha opened this issue 11 years ago • 3 comments

I didn't find it anywhere in docs, but what I would like to do is within a job call a delay method, for example:

class ImportSongs
  include Backburner::Queue

  def self.perform(api_token, songs)
    api = API.new api_token

    songs.each_with_index do |song, i|
      # make current worker proceed with another job while it's sleeping
      delay 60*60  if i != 0 && i % 100 == 0

      api.import_song song
    end
  end
end

andrusha avatar Feb 03 '13 11:02 andrusha

I like this idea but there isn't a current way to do this. If anyone wants to contribute a patch that would make support for this possible, I would appreciate it. I personally haven't run into this use case in my jobs so far.

nesquena avatar Apr 23 '13 03:04 nesquena

You can simply have steps in your code, and at the end of each step enqueue a new job with the next step to do as argument.

rewritten avatar Sep 12 '13 19:09 rewritten

Yes, this is the kind of solution I've used in https://github.com/andrusha/lastfm-to-vk. Althought, I don't like that with each job I would have to carry the full list of remaining songs and it gets tricky when you handle failed jobs.

andrusha avatar Sep 12 '13 19:09 andrusha