0
0
Ruby on Railsframework~10 mins

Scheduled jobs in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Scheduled jobs
Define job class
Schedule job with interval
Job queue stores job
Background worker picks job
Job performs task
Job completes and logs
Wait for next scheduled time
Scheduled jobs in Rails define tasks to run later or repeatedly, managed by background workers that pick and execute them on time.
Execution Sample
Ruby on Rails
class CleanupJob < ApplicationJob
  queue_as :default

  def perform
    User.where('last_sign_in_at < ?', 1.year.ago).delete_all
  end
end

CleanupJob.set(wait: 1.hour).perform_later
This code schedules a job to delete users inactive for over a year, running one hour later.
Execution Table
StepActionJob StateWorker ActionOutput/Result
1Define CleanupJob classJob class readyNo worker actionReady to schedule
2Schedule job with wait: 1.hourJob queued with timestampNo worker actionJob stored in queue
3Background worker checks queueJob queuedNo job ready yetWait for scheduled time
4One hour passesJob ready to runWorker picks jobJob starts performing
5Perform job: delete old usersJob runningWorker executes perform methodOld users deleted
6Job completesJob doneWorker logs completionJob finished successfully
7Wait for next scheduled jobNo job queuedIdle or picks next jobReady for next task
💡 Job finishes after performing task; worker waits for next scheduled job.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
Job StateNot definedQueued with timestampReady to runRunningDone
Worker StatusIdleIdlePicks jobExecutes jobIdle
Key Moments - 3 Insights
Why doesn't the job run immediately after scheduling?
Because the job is scheduled with a delay (wait: 1.hour), the worker waits until the scheduled time before running it, as shown in steps 3 and 4.
What happens if the worker checks the queue but the job is not ready?
The worker stays idle or checks other jobs until the scheduled time arrives, as seen in step 3 where no job runs yet.
How does the job know what task to perform?
The job class defines the perform method with the task code; the worker calls this method when running the job (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the Job State at Step 4?
AQueued with timestamp
BRunning
CReady to run
DDone
💡 Hint
Check the 'Job State' column at Step 4 in the execution_table.
At which step does the worker start executing the job's perform method?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look for when 'Worker executes perform method' happens in the execution_table.
If the job was scheduled with wait: 0.seconds, how would Step 3 change?
AJob would remain queued longer
BWorker would pick job immediately, no waiting
CWorker would ignore the job
DJob would be deleted
💡 Hint
Consider the 'Job queued with timestamp' and worker action timing in Step 3.
Concept Snapshot
Scheduled jobs in Rails run tasks later or repeatedly.
Define a job class with perform method.
Schedule with set(wait: time).perform_later.
Background workers run jobs at scheduled time.
Useful for cleanup, emails, reports.
Full Transcript
Scheduled jobs in Rails let you run code later or repeatedly without blocking the main app. You create a job class inheriting from ApplicationJob and define a perform method with the task. Then you schedule it using set(wait: time).perform_later, which queues the job with a timestamp. Background workers check the queue and run jobs when their scheduled time arrives. This process helps keep your app responsive by handling tasks like deleting old users or sending emails in the background. The execution table shows each step from defining the job to completion, tracking job state and worker actions. Key points include understanding the delay before execution and how the worker picks and runs the job. This visual trace helps beginners see how scheduled jobs flow in Rails.