0
0
Ruby on Railsframework~10 mins

Job priorities and queues in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Job priorities and queues
Job Created
Assign Priority
Place in Queue Based on Priority
Worker Picks Job from Highest Priority Queue
Job Executed
Job Completed or Failed
Repeat for Next Job
Jobs are created with priorities, placed in queues accordingly, and workers pick jobs from the highest priority queue first.
Execution Sample
Ruby on Rails
class MyJob < ApplicationJob
  queue_as :default
  def perform(*args)
    # job code here
  end
end

MyJob.set(queue: :high_priority).perform_later(*args)
This code creates a job assigned to a high priority queue and enqueues it for later execution.
Execution Table
StepActionJob PriorityQueue PlacedWorker Picks JobJob Status
1Job CreateddefaultdefaultNoPending
2Job Created with High Priorityhigh_priorityhigh_priorityNoPending
3Worker Checks Queues--Picks from high_priority-
4Worker Executes Jobhigh_priorityhigh_priorityYesRunning
5Job Completes Successfullyhigh_priorityhigh_priorityYesCompleted
6Worker Checks Queues Again--Picks from default if any-
7No More Jobs--NoIdle
💡 No more jobs in any queue, workers become idle
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4After Step 5Final
Job Prioritynonedefaulthigh_priorityhigh_priorityhigh_prioritynone
Queue Stateemptydefault:1 jobdefault:1 job, high_priority:1 jobdefault:1 job, high_priority:0 jobsdefault:1 job, high_priority:0 jobsempty
Worker Statusidleidleidlerunning jobidleidle
Key Moments - 3 Insights
Why does the worker pick from the high_priority queue before the default queue?
Because the execution_table at Step 3 shows the worker checks queues and picks from the highest priority queue first, ensuring urgent jobs run before others.
What happens if no jobs are in the high_priority queue but jobs exist in the default queue?
At Step 6, the worker checks queues again and picks jobs from the default queue if the high_priority queue is empty, as shown in the execution_table.
How does assigning a queue affect job execution order?
Assigning a queue with a priority (like high_priority) places the job in that queue, so workers pick jobs from higher priority queues first, as seen in Steps 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the job status at Step 4?
APending
BRunning
CCompleted
DIdle
💡 Hint
Check the 'Job Status' column at Step 4 in the execution_table
At which step does the worker pick a job from the default queue?
AStep 6
BStep 5
CStep 3
DStep 7
💡 Hint
Look at the 'Worker Picks Job' column in the execution_table for when default queue is chosen
If a job is assigned to a low_priority queue, how would the worker pick order change?
AWorker picks low_priority jobs before high_priority
BWorker picks low_priority jobs before default but after high_priority
CWorker picks low_priority jobs after default and high_priority
DWorker ignores low_priority jobs
💡 Hint
Recall that workers pick jobs from highest priority queues first as shown in the concept_flow and execution_table
Concept Snapshot
In Rails, jobs can be assigned to different queues representing priorities.
Workers pick jobs from higher priority queues first.
Use queue_as or set(queue:) to assign jobs.
Jobs run asynchronously in order of queue priority.
This helps manage workload and urgent tasks efficiently.
Full Transcript
In Rails, background jobs can be assigned to different queues to manage priorities. When a job is created, it is assigned a priority by placing it in a specific queue, such as 'default' or 'high_priority'. Workers look at these queues and always pick jobs from the highest priority queue available. The execution table shows that when a job is created with high priority, it goes into the high_priority queue. Workers pick jobs from this queue before the default queue. After the job runs and completes, the worker checks for more jobs. If no jobs remain, the worker becomes idle. This system ensures urgent jobs run first and helps organize background work efficiently.