Arslan Ali Arslan Ali - 1 year ago 61
Ruby Question

How to enqueue a job within a job dynamically in Rails?

I've a job, and when the job is run, at the very bottom of it, and I want to

the same job again to run after 1 hour, but with different arguments.

What I've achieved so far:

class SimpleJob
@queue = :normal

def self.perform(start)
puts "Right now, start = #{start}"
start += 12
time = some_request_external_api
self.set(wait: time).perform_later(start)

I'm using resque gem, and running the job through
QUEUE=* rake resque:work
. Surely, it prints
Right now, start = 12
in beginning, but after that, nothing happens. How exactly can I achieve this functionality?

Answer Source

Rather than enqueuing the job again within itself, you could either use a scheduler, such as clockwork.

Or, if what you are trying to accomplish is in response to some event on another service, maybe you could look into it's documentation and see if it provides webhook functionality.

These would send post requests to your desired action whenever any action occurs on their side.