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
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With