Ruby on Rails Ruby on Rails Action Mailer, Background Jobs & Caching 2 — Questions and Answers
Question 1: What Rails command generates a new Active Job class called ProcessPayment?
- rails new job ProcessPayment
- rails generate job ProcessPayment (Correct answer)
- rails create job ProcessPayment
- rails make job ProcessPayment
Correct answer: rails generate job ProcessPayment
rails generate job (or rails g job) creates a new job file in app/jobs/ with a skeleton class and perform method.
Question 2: Which method enqueues an Active Job for asynchronous background processing?
- enqueue_later
- perform_background
- perform_later (Correct answer)
- run_async
Correct answer: perform_later
perform_later serializes the job arguments and enqueues the job to the configured queue adapter for asynchronous background execution.
Question 3: What is the default queue adapter in a Rails application when no background job gem is configured?
- :sidekiq
- :inline
- :async (Correct answer)
- :synchronous
Correct answer: :async
The :async adapter uses an in-process thread pool and is Rails' built-in default, though jobs are lost on server restart since they are not persisted.
Question 4: How do you specify which queue a job should be placed in using Active Job?
- set_queue :high_priority
- queue_as :high_priority (Correct answer)
- job.queue = :high_priority
- perform_on_queue :high_priority
Correct answer: queue_as :high_priority
queue_as is a class-level macro in Active Job that sets the queue name for all instances of that job class.
Question 5: Which background job processing gem is most widely used with Ruby on Rails in production?
- Delayed::Job
- Resque
- Sucker Punch
- Sidekiq (Correct answer)
Correct answer: Sidekiq
Sidekiq is the most popular Rails background job processor, using Redis for job persistence and multi-threaded workers for high throughput.
Question 6: What does the retry_on class method configure in an Active Job?
- Immediate synchronous retry on any exception
- Automatic retries for specified exceptions with configurable wait and attempt limits (Correct answer)
- Restarts all stopped workers in the queue backend
- Re-runs all previously failed jobs stored in the database
Correct answer: Automatic retries for specified exceptions with configurable wait and attempt limits
retry_on specifies which exception classes should trigger automatic retries, with configurable wait intervals (fixed or exponential) and maximum attempt counts.
Question 7: Which method executes an Active Job immediately in the current thread without queuing it?
- MyJob.run_now
- MyJob.execute_now
- MyJob.perform_now (Correct answer)
- MyJob.dispatch_now
Correct answer: MyJob.perform_now
perform_now executes the job synchronously in the current thread, bypassing the queue adapter — useful for testing and for immediate execution requirements.
What Rails command generates a new Active Job class called ProcessPayment?