0
0
Ruby on Railsframework~5 mins

Job priorities and queues in Ruby on Rails

Choose your learning style9 modes available
Introduction

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.

Sending emails after a user signs up without making them wait
Processing image uploads or resizing photos after upload
Running reports or data cleanup tasks overnight
Handling notifications or messages that don't need instant response
Managing tasks that might take a long time so the app stays fast
Syntax
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.

Examples
This job sends a welcome email using the mailers queue.
Ruby on Rails
class EmailJob < ApplicationJob
  queue_as :mailers

  def perform(user_id)
    user = User.find(user_id)
    UserMailer.welcome_email(user).deliver_now
  end
end
This job runs in the low priority queue for less urgent tasks.
Ruby on Rails
class CleanupJob < ApplicationJob
  queue_as :low

  def perform
    # Clean old records
  end
end
This job runs in the high priority queue for urgent tasks.
Ruby on Rails
class CriticalJob < ApplicationJob
  queue_as :high

  def perform
    # Handle urgent task
  end
end
Sample Program

This example shows two jobs: one normal and one urgent. The urgent job runs first because it uses the high priority queue.

Ruby on Rails
# 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
OutputSuccess
Important Notes

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.

Summary

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.