Scheduled jobs let your app do tasks automatically at set times. This helps with things like sending emails or cleaning up data without you doing it manually.
Scheduled jobs in Ruby on Rails
class MyJob < ApplicationJob queue_as :default def perform(*args) # task code here end end # To schedule, use a scheduler like 'whenever' gem or Active Job with cron # Example with 'whenever' gem in schedule.rb: every 1.day, at: '9:00 am' do runner "MyJob.perform_later" end
Jobs are classes inheriting from ApplicationJob.
Use a scheduler gem like whenever or system cron to run jobs at set times.
class CleanupJob < ApplicationJob queue_as :default def perform TempFile.delete_old_files end end
whenever schedule runs a sync method every hour.every 1.hour do runner "SyncService.sync_data" end
class ReminderJob < ApplicationJob queue_as :default def perform(user_id) user = User.find(user_id) UserMailer.reminder_email(user).deliver_now end end
This simple job prints a greeting message with the given name. We run it immediately to see the output.
class GreetJob < ApplicationJob queue_as :default def perform(name) puts "Hello, #{name}! This job ran successfully." end end # Simulate running the job immediately GreetJob.perform_now('Alice')
Scheduled jobs run in the background, so they don't slow down your app's main work.
Use perform_later to run jobs asynchronously, and perform_now for immediate execution (usually in tests).
Make sure your scheduler (like cron or whenever) is properly set up on your server to trigger jobs on time.
Scheduled jobs automate tasks to run at specific times without manual effort.
Define jobs by creating classes inheriting from ApplicationJob.
Use scheduling tools like whenever gem or cron to run jobs regularly.
Practice
Solution
Step 1: Understand the role of scheduled jobs
Scheduled jobs are designed to run tasks automatically at set times without needing a user to start them manually.Step 2: Compare with other options
User authentication, database migrations, and UI styling are handled by other parts of Rails, not scheduled jobs.Final Answer:
To automate tasks to run at specific times without manual effort -> Option CQuick Check:
Scheduled jobs automate tasks = C [OK]
- Confusing scheduled jobs with user authentication
- Thinking scheduled jobs handle database migrations
- Assuming scheduled jobs style the UI
Solution
Step 1: Check the inheritance pattern for jobs
In Rails, jobs must inherit from ApplicationJob to integrate with Active Job framework.Step 2: Verify the method definition
The perform method defines the task to run. class MyJob < ApplicationJob def perform # task code end end correctly defines a class inheriting ApplicationJob with perform method.Final Answer:
class MyJob < ApplicationJob\n def perform\n # task code\n end\nend -> Option DQuick Check:
Job class inherits ApplicationJob = A [OK]
- Defining job without inheriting ApplicationJob
- Using module instead of class for jobs
- Defining job as a method instead of a class
MyJob.perform_now is called?class MyJob < ApplicationJob
def perform
puts 'Job executed'
end
endSolution
Step 1: Understand perform_now behavior
Calling perform_now runs the job immediately and synchronously, executing the perform method.Step 2: Check the perform method output
The perform method prints 'Job executed' to the console, so this string will appear as output.Final Answer:
Job executed -> Option AQuick Check:
perform_now runs perform immediately = B [OK]
- Thinking perform_now runs job silently
- Confusing perform_now with asynchronous perform_later
- Expecting an error due to missing method
class CleanupJob < ApplicationJob
def perform
User.delete_all
end
end
# Schedule in config/schedule.rb
every 1.day do
runner 'CleanupJob.perform_later'
endSolution
Step 1: Examine the job class definition
The class correctly inherits from ApplicationJob and defines the perform method.Step 2: Verify scheduling syntax
The config/schedule.rb uses standard Whenever syntax: every 1.day with runner 'CleanupJob.perform_later', which executes Ruby code to enqueue the job asynchronously. No errors present.Final Answer:
No error, code is correct -> Option AQuick Check:
runner 'Job.perform_later' in schedule.rb = D [OK]
- Thinking runner expects only instance methods
- Confusing runner with rake tasks
- Believing perform_later is invalid in cron schedules
whenever gem?class ArchiveJob < ApplicationJob
def perform
Record.archive_old
end
end
# config/schedule.rb
? Which snippet correctly schedules the job?Solution
Step 1: Check whenever syntax for daily jobs at midnight
The whenever gem uses symbols like :day and time strings like '00:00' to schedule jobs at midnight.Step 2: Verify the job call method
Using perform_later runs the job asynchronously, which is preferred for scheduled jobs to avoid blocking.Step 3: Compare options
every :day, at: '00:00' do runner 'ArchiveJob.perform_later' end uses correct symbol :day, time '00:00', and perform_later with runner, matching best practice.Final Answer:
every :day, at: '00:00' do\n runner 'ArchiveJob.perform_later'\nend -> Option BQuick Check:
whenever daily at midnight with perform_later = D [OK]
- Using perform_now instead of perform_later for scheduled jobs
- Incorrect time format like '12:00 am' instead of '00:00'
- Using rake task instead of runner for Active Job
Ready to prove it?
Pass the test and this pattern moves unknown
