mcpField
S
solid-queue-setup

solid-queue-setup

github.com/thibautbaissac開発67164 回インストール21 週間前に更新
リポジトリ

solid-queue-setup について

Rails 8 アプリで Solid Queue を使うためのセットアップと利用ガイドです。Active Job のバックエンド設定、ジョブの実行、テスト、監視を扱います。

主な機能

  1. 01Solid Queue の導入と設定
  2. 02Active Job アダプターの指定
  3. 03再試行や優先度付きジョブの作成
  4. 04遅延・定期ジョブの設定
  5. 05ジョブのテストと監視

活用シーン

  1. 01Rails アプリのバックグラウンドジョブ設定
  2. 02ジョブのキューと優先度の指定
  3. 03遅延・定期処理のスケジュール
  4. 04失敗ジョブの確認と再試行

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/thibautbaissac/rails_ai_agents --skill solid-queue-setup

インストール後、Claude Code で/solid-queue-setupを入力して読み込まれたか確認します。

詳細

Rails 8 向け Solid Queue のセットアップ

概要

Solid Queue は Rails 8 のデフォルトの Active Job バックエンドです。

  • データベースベース(Redis は不要)
  • 同時実行制御を内蔵
  • 優先度と複数のキューに対応
  • ミッションクリティカルなジョブ処理
  • Mission Control から利用できる Web UI

クイックスタート

インストール
# Add to Gemfile (included in Rails 8 by default)
bundle add solid_queue

# Install Solid Queue
bin/rails solid_queue:install

# Run migrations
bin/rails db:migrate
設定
# config/solid_queue.yml
default: &default
  dispatchers:
    - polling_interval: 1
      batch_size: 500
  workers:
    - queues: "*"
      threads: 3
      processes: 1
      polling_interval: 0.1

development:
  <<: *default

production:
  <<: *default
  workers:
    - queues: [critical, default]
      threads: 5
      processes: 2
    - queues: [low]
      threads: 2
      processes: 1
Active Job アダプターとして設定
# config/application.rb
config.active_job.queue_adapter = :solid_queue

# Or per environment
# config/environments/production.rb
config.active_job.queue_adapter = :solid_queue

ワークフローチェックリスト

Solid Queue Setup:
- [ ] Add solid_queue gem
- [ ] Run solid_queue:install
- [ ] Run migrations
- [ ] Configure queues in solid_queue.yml
- [ ] Set queue adapter in config
- [ ] Create first job with spec
- [ ] Test job execution
- [ ] Configure recurring jobs (if needed)

ジョブの作成

基本的なジョブ
# app/jobs/send_welcome_email_job.rb
class SendWelcomeEmailJob < ApplicationJob
  queue_as :default

  def perform(user_id)
    user = User.find(user_id)
    UserMailer.welcome(user).deliver_now
  end
end
リトライ付きジョブ
# app/jobs/process_payment_job.rb
class ProcessPaymentJob < ApplicationJob
  queue_as :critical

  # Retry on specific errors
  retry_on PaymentGatewayError, wait: :polynomially_longer, attempts: 5

  # Don't retry on these
  discard_on ActiveRecord::RecordNotFound

  # Custom error handling
  rescue_from(StandardError) do |exception|
    ErrorNotifier.notify(exception)
    raise # Re-raise to trigger retry
  end

  def perform(order_id)
    order = Order.find(order_id)
    PaymentService.new.charge(order)
  end
end
優先度付きジョブ
class UrgentNotificationJob < ApplicationJob
  queue_as :critical

  # Lower number = higher priority
  # Default is 0
  def priority
    -10
  end

  def perform(notification_id)
    # Process urgent notification
  end
end

ジョブのキューへの追加

# Enqueue immediately
SendWelcomeEmailJob.perform_later(user.id)

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

# Enqueue at specific time
SendReportJob.set(wait_until: Date.tomorrow.noon).perform_later

# Enqueue on specific queue
ProcessJob.set(queue: :low).perform_later(data)

# Perform immediately (skips queue - use sparingly)
SendWelcomeEmailJob.perform_now(user.id)

定期実行ジョブ

# config/recurring.yml
production:
  daily_report:
    class: GenerateDailyReportJob
    schedule: every day at 6am
    queue: low

  cleanup:
    class: CleanupOldRecordsJob
    schedule: every sunday at 2am

  sync:
    class: SyncExternalDataJob
    schedule: every 15 minutes

ジョブのテスト

ジョブスペックのテンプレート
# spec/jobs/send_welcome_email_job_spec.rb
require 'rails_helper'

RSpec.describe SendWelcomeEmailJob, type: :job do
  let(:user) { create(:user) }

  describe '#perform' do
    it 'sends welcome email' do
      expect {
        described_class.perform_now(user.id)
      }.to have_enqueued_mail(UserMailer, :welcome)
    end
  end

  describe 'enqueueing' do
    it 'enqueues the job' do
      expect {
        described_class.perform_later(user.id)
      }.to have_enqueued_job(described_class)
        .with(user.id)
        .on_queue('default')
    end
  end

  describe 'retry behavior' do
    it 'retries on PaymentGatewayError' do
      expect(described_class).to have_retry_on(PaymentGatewayError)
    end
  end
end
テストヘルパー
# spec/rails_helper.rb
RSpec.configure do |config|
  config.include ActiveJob::TestHelper
end

# In specs
it 'processes all jobs' do
  perform_enqueued_jobs do
    UserSignupService.call(user_params)
  end
  expect(user.reload.welcome_email_sent?).to be true
end

it 'enqueues multiple jobs' do
  expect {
    BatchProcessor.process(items)
  }.to have_enqueued_job(ProcessItemJob).exactly(items.count).times
end

Solid Queue の実行

# Development (runs in separate terminal)
bin/rails solid_queue:start

# Production (via Procfile)
# Procfile
web: bin/rails server
worker: bin/rails solid_queue:start

モニタリング

Mission Control(Web UI)
# Gemfile
gem "mission_control-jobs"

# config/routes.rb
mount MissionControl::Jobs::Engine, at: "/jobs"
コンソールクエリ
# Check pending jobs
SolidQueue::Job.where(finished_at: nil).count

# Check failed jobs
SolidQueue::FailedExecution.count

# Retry failed job
SolidQueue::FailedExecution.last.retry

# Clear old completed jobs
SolidQueue::Job.where('finished_at < ?', 1.week.ago).delete_all

Sidekiq からの移行

SidekiqSolid Queue
perform_async(args)perform_later(args)
perform_in(5.minutes, args)set(wait: 5.minutes).perform_later(args)
sidekiq_options queue: 'critical'queue_as :critical
sidekiq_retry_inwait: を指定した retry_on

コメント