rails-active-job

📁 shivamsinghchahar/rails-skills 📅 11 days ago
3
总安装量
3
周安装量
#59995
全站排名
安装命令
npx skills add https://github.com/shivamsinghchahar/rails-skills --skill rails-active-job

Agent 安装分布

amp 3
gemini-cli 3
github-copilot 3
codex 3
cursor 3
opencode 3

Skill 文档

Active Job: Background Job Processing

Active Job is Rails’ framework for declaring background jobs and processing them asynchronously. Use it to offload work from the request-response cycle, improving application responsiveness.

When to Use Active Jobs

  • Email sending: Async mailers to avoid request delays
  • Heavy computations: Process data without blocking users
  • External API calls: Retry logic for unreliable services
  • Bulk operations: Process large datasets in the background
  • Scheduled tasks: Recurring operations with cron jobs
  • Event processing: Handle webhooks and event notifications
  • Cleanup tasks: Database maintenance and data archival

Quick Start

Create a Job

rails generate job SendWelcomeEmail

Define the Job

class SendWelcomeEmailJob < ApplicationJob
  queue_as :default  # Route to 'default' queue
  
  def perform(user_id)
    user = User.find(user_id)
    UserMailer.welcome(user).deliver_later
  end
end

Enqueue the Job

# Enqueue immediately
SendWelcomeEmailJob.perform_later(user.id)

# Enqueue with delay
SendWelcomeEmailJob.set(wait: 1.hour).perform_later(user.id)

# Enqueue at specific time
SendWelcomeEmailJob.set(wait_until: 2.days.from_now).perform_later(user.id)

# Enqueue multiple jobs at once
user_ids.map { |id| [id] }.then do |args|
  SendWelcomeEmailJob.perform_all_later(args)
end

Job Basics

Learn fundamental job concepts and lifecycle:

Queue Management

Configure and manage job queues:

Advanced Features

Master sophisticated job patterns:

Examples

See Real-world implementations covering:

  • Email notifications
  • Data imports and exports
  • Image processing
  • External API synchronization
  • Report generation
  • Webhook handling

Default Backend: Solid Queue

Rails 8+ includes Solid Queue as the default job backend. Configure in config/solid_queue.yml:

production:
  queues:
    - name: default
      threads: 5
    - name: critical
      threads: 2
    - name: batch
      threads: 1
  
  workers:
    - name: worker_1
      queues: [ default, critical, batch ]
  
  scheduler:
    workers: 1

Solid Queue provides:

  • Database-backed job storage (no additional dependencies)
  • Queue ordering and priority
  • Concurrency limiting
  • Repeating jobs (cron-like scheduling)
  • Multi-threaded workers
  • Built-in error tracking

Key Concepts

Concept Description
Job Ruby class defining async work (inherits from ApplicationJob)
Queue Named container for jobs (default, critical, batch, etc.)
Enqueue Add job to queue for processing (perform_later)
Worker Process that executes jobs from queues
Retry Automatic job re-execution after failure
Discard Permanently skip job after error
Continuation Resume job execution in steps (Rails 8+)

Next Steps

  1. Start with Job Definition to understand job structure
  2. Configure Queue Setup for your environment
  3. Add Error Handling for reliability
  4. Learn Testing strategies for your jobs
  5. Explore Examples for your use case

Resources