backburner
backburner copied to clipboard
Delay within a worker
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
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.
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.
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.