Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting priority with ActiveJob when using Delayed::Job

How do you set the delayed job priority when using ActiveJob to enqueue your jobs?

class GuestsCleanupJob < ApplicationJob
  queue_as :high_priority

  def perform(*guests)
    # Do something later
  end
end
like image 595
Tom Rossi Avatar asked May 18 '18 15:05

Tom Rossi


2 Answers

It took me a while, but I found this method in the Delayed::Job documentation:

Delayed::Worker.queue_attributes = {
  default:       { priority: 11 },
  high_priority: { priority: 1 },
  low_priority:  { priority: 75 }
}

I've added this to my initializers and just wanted to share if anyone else runs into this!

like image 138
Tom Rossi Avatar answered Oct 14 '22 04:10

Tom Rossi


defining an instance method that defines priority works, however is doesn't allow me to overload the value. Given this class

class GuestsCleanupJob < ApplicationJob
  queue_as :high_priority

  def priority
    1
  end

  def perform(*guests)
    # Do something later
  end
end

if I run

GuestsCleanupJob.set(priority: 55).perform_later(user, lead) # priority set to 1 not 55

It will queue up a job with priority 1, and ignore the 55 I passed.

That didn't provide enough control for my use-case so instead I did.

class GuestsCleanupJob < ApplicationJob
  queue_as :high_priority

  def default_priority
    1
  end

  def priority
    @priority || default_priority
  end

  def perform(*guests)
    # Do something later
  end
end

Using the above code, by default the priority will be set to one, but I can use my

GuestsCleanupJob.set(priority: 55).perform_later(user, lead) # priority set to 55
like image 44
Nicholas Stock Avatar answered Oct 14 '22 04:10

Nicholas Stock