I want to re-queue the job automatically after it's done. There will be optional delay for 10 seconds if nothing was processed.
I can see two approaches. The first one is to put the logic inside perform block. The second is to use around_block feature to do that.
What is the more elegant way to do that?
Here is an example of code block with such logic
# Process queue automatically
class ProcessingJob < ApplicationJob
queue_as :processing_queue
around_perform do |_job, block|
if block.call.zero?
# wait for 10 seconds if 0 items was handled
self.class.set(wait: 10.seconds).perform_later
else
# don't rest, there are many work to do
self.class.perform_later
end
end
def perform
# will return number of processed items
ProcessingService.handle_next_batch
end
end
Should I put around_block logic into the perform function?