Job priorities and queues help your app do many tasks in the background without slowing down the main work. You can decide which jobs are more important and run them first.
Job priorities and queues in Ruby on Rails
class MyJob < ApplicationJob queue_as :default def perform(*args) # Your job code here end end # To set priority, use different queues like :high, :default, :low class HighPriorityJob < ApplicationJob queue_as :high def perform(*args) # Important job code end end
Each job class sets which queue it belongs to with queue_as.
You can configure workers to process queues in order of priority.
mailers queue.class EmailJob < ApplicationJob queue_as :mailers def perform(user_id) user = User.find(user_id) UserMailer.welcome_email(user).deliver_now end end
low priority queue for less urgent tasks.class CleanupJob < ApplicationJob queue_as :low def perform # Clean old records end end
high priority queue for urgent tasks.class CriticalJob < ApplicationJob queue_as :high def perform # Handle urgent task end end
This example shows two jobs: one normal and one urgent. The urgent job runs first because it uses the high priority queue.
# app/jobs/print_message_job.rb class PrintMessageJob < ApplicationJob queue_as :default def perform(message) puts "Job says: #{message}" end end # app/jobs/urgent_job.rb class UrgentJob < ApplicationJob queue_as :high def perform puts "Urgent job running!" end end # In Rails console or a script # Enqueue jobs PrintMessageJob.perform_later("Hello from default queue") UrgentJob.perform_later # Assuming Sidekiq or another adapter is running and configured to process queues # with priority: high first, then default # Output when jobs run: # Urgent job running! # Job says: Hello from default queue
Time complexity depends on the job's own code, but queueing is very fast (constant time).
Space complexity is minimal for queue metadata but depends on job data size.
Common mistake: Not configuring workers to respect queue priorities, so all jobs run in random order.
Use job priorities when some tasks must finish before others. Use separate queues to organize and control job flow.
Job priorities let you run important tasks before less important ones.
Queues group jobs by priority or type for better control.
Set queue_as in your job classes to assign priorities.