Search code examples
ruby-on-railsruby-on-rails-4delayed-job

Get Id of created Job in Delayed Jobs


I am adding some emails in the queue using Delayed Jobs. The way I create a job is given below:

EventNotifications.reminder("email", "name", id).deliver_later

where EventNotifications is the class and reminder is the method inside it.

This creates a job in delayed_jobs table but I want to get the Id of the job created as I have some custom field inside the delayed_jobs table that I want to update.

What I currently get after running the above line is:

#<ActionMailer::DeliveryJob:0x00000005eb22d8 @arguments=["EventNotifications", "reminder", "deliver_now", "email", "name", 12], @job_id="6a549235-e8c1-407b-ac75-be8736559eaa", @queue_name="mailers">

This does not have the Id of the job that was created. How do I get that Id?


Solution

  • You could bypass ActiveJob and use the DelayedJob API directly. This has the downside of not abstracting your job system away. You can do something like this:

    job = EventNotifications.delay.reminder("email", "name", id)
    

    At that point, you have access to job.id.