Introduction
Active Job helps you run tasks in the background so your app stays fast. It makes it easy to do things later without slowing down users.
Active Job helps you run tasks in the background so your app stays fast. It makes it easy to do things later without slowing down users.
class MyJob < ApplicationJob queue_as :default def perform(*args) # Do the work here end end
class EmailUserJob < ApplicationJob queue_as :mailers def perform(user) UserMailer.welcome_email(user).deliver_now end end
class CleanupJob < ApplicationJob queue_as :low_priority def perform OldRecord.where('created_at < ?', 1.month.ago).delete_all end end
MyJob.perform_later(arg1, arg2)
This job prints a greeting message with the given name. It runs in the background when enqueued.
class GreetUserJob < ApplicationJob queue_as :default def perform(name) puts "Hello, #{name}!" end end # Enqueue the job GreetUserJob.perform_later('Alice')
Active Job works with many background systems like Sidekiq, Resque, or Delayed Job.
Use perform_later to run jobs asynchronously and perform_now to run immediately.
Always keep jobs small and focused to avoid long delays.
Active Job lets you run tasks in the background easily.
Define jobs by subclassing ApplicationJob and writing a perform method.
Use perform_later to enqueue jobs without blocking your app.
perform_later to enqueue jobs to run asynchronously.perform_now runs immediately, enqueue and run_async are not valid Active Job methods.class GreetingJob < ApplicationJob
def perform(name)
puts "Hello, #{name}!"
end
end
GreetingJob.perform_later('Alice')perform_later method enqueues the job to run asynchronously in the background.puts inside perform runs when the job executes later, not immediately.class EmailJob < ApplicationJob
def perform
UserMailer.welcome_email.deliver_now
end
endperform_now ignores verification and blocks the request.Pass the test and this pattern moves unknown