agents/background-job/SKILL.md
Orchestrates robust background job implementation: design job → TDD implementation → configure retry/discard strategies → test failure scenarios → production monitoring. Use when adding async processing, implementing background jobs, or configuring job queues. Trigger: background job, async processing, sidekiq, solid queue, active job, job queue, worker.
npx skillsauth add igmarin/rails-agent-skills background-jobInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Orchestrates robust background job implementation with TDD discipline, proper retry/discard strategies, comprehensive failure scenario testing, and production monitoring to ensure reliable async processing.
Objective: Define job responsibilities, idempotency strategy, and error classification before writing code.
Steps:
HARD GATE — Job Design Complete:
If gate fails: Clarify requirements before implementation.
Objective: Implement job logic under TDD discipline.
Steps:
HARD GATE — Tests Pass:
Example job test skeleton:
# spec/jobs/order_confirmation_email_job_spec.rb
RSpec.describe OrderConfirmationEmailJob do
let(:order) { create(:order, :completed) }
it 'sends confirmation email' do
expect(EmailService).to receive(:send_confirmation).with(order.id, order.customer_email, order.total)
described_class.perform_now(order.id, order.customer_email, order.total)
end
it 'is idempotent' do
expect(EmailService).to receive(:send_confirmation).once
2.times { described_class.perform_now(order.id, order.customer_email, order.total) }
end
it 'raises on transient errors so retry triggers' do
allow(EmailService).to receive(:send_confirmation).and_raise(EmailService::TimeoutError)
expect { described_class.perform_now(order.id, order.customer_email, order.total) }.to raise_error(EmailService::TimeoutError)
end
end
Example job implementation skeleton:
# app/jobs/order_confirmation_email_job.rb
class OrderConfirmationEmailJob < ApplicationJob
queue_as :default
retry_on EmailService::TimeoutError, wait: :exponentially_longer, attempts: 5
retry_on EmailService::RateLimitError, wait: :exponentially_longer, attempts: 3
discard_on ActiveRecord::RecordNotFound
discard_on EmailService::InvalidEmailError
def perform(order_id, customer_email, order_total)
order = Order.find(order_id)
return if order.email_sent_at.present? # idempotency guard
EmailService.send_confirmation(order_id, customer_email, order_total)
order.update!(email_sent_at: Time.current)
rescue EmailService::TimeoutError, EmailService::RateLimitError => e
Rails.logger.error("[#{self.class}] transient error: #{e.message}")
raise
end
end
Note:
discard_onhandles permanent errors at the framework level — no rescue block is needed for them. The rescue block above covers only transient errors that need logging before being re-raised to trigger retry.
Objective: Harden job for production with correct retry backoff, discard rules, timeouts, and monitoring hooks.
Steps:
retry_on with exponential backoff and a capped attempt count (3–5) for every transient error class.discard_on for every permanent error class; log discards.ApplicationJob callbacks.Solid Queue (Rails 8+) snippet:
# config/initializers/solid_queue.rb
SolidQueue.configure { |c| c.worker = { processes: 2, threads: 5, polling_interval: 1 } }
Sidekiq snippet:
# config/initializers/sidekiq.rb
Sidekiq.configure_server { |c| c.redis = { url: ENV['REDIS_URL'] } }
Monitoring hook in ApplicationJob:
class ApplicationJob < ActiveJob::Base
around_perform do |job, block|
start = Time.current
block.call
StatsD.timing("jobs.#{job.class.name.underscore}.duration", Time.current - start)
StatsD.increment("jobs.#{job.class.name.underscore}.success")
rescue StandardError
StatsD.increment("jobs.#{job.class.name.underscore}.failure")
raise
end
end
HARD GATE — Retry Strategy Configured:
retry_on declared for every transient error with backoff and attempt capdiscard_on declared for every permanent error with loggingIf gate fails: Job is not production-ready.
Objective: Verify retry/discard behaviour under injected failures and confirm observability.
Steps:
Example failure scenario tests:
RSpec.describe OrderConfirmationEmailJob do
let(:order) { create(:order, :completed) }
it 'logs and re-raises on transient error' do
allow(EmailService).to receive(:send_confirmation).and_raise(EmailService::TimeoutError)
expect(Rails.logger).to receive(:error).with(/transient error/)
expect { described_class.perform_now(order.id, order.customer_email, order.total) }
.to raise_error(EmailService::TimeoutError)
end
it 'discards silently on permanent error' do
allow(EmailService).to receive(:send_confirmation).and_raise(EmailService::InvalidEmailError)
expect { described_class.perform_now(order.id, "bad", order.total) }.not_to raise_error
end
end
HARD GATE — Failure Scenarios Tested:
If gate fails: Address failure scenarios before deploying.
Never deploy a background job without:
retry_on with backoffdiscard_on with loggingJob fails repeatedly in production:
retry_on/discard_on if mis-classified.Queue backs up:
development
Entry point for Rails development workflows covering TDD, RSpec, Service Objects, DDD, GraphQL, Engines, and Code Quality. Use when the user asks about Ruby on Rails development patterns, needs RSpec test suites generated, wants service objects scaffolded, is setting up GraphQL schemas, performing Rails code review, refactoring .rb files, working with domain-driven design, implementing background jobs, conducting Rails security checks, or building Rails engines. Generates RSpec tests, structures service objects, enforces TDD workflows, configures GraphQL schemas, and coordinates domain-driven design patterns. Trigger keywords: Rails, RSpec, TDD, Rails testing, Rails refactor, Rails API, Rails code review, domain driven design, service objects, GraphQL, Rails engine, Ruby, .rb, background jobs, Rails migrations, Rails security check.
development
Use when shipping a Rails engine gem — FIRST run full test suite (`bundle exec rspec`) and fix ALL failures, verify gemspec metadata and dependencies match tested Rails/Ruby versions, dry-run: `gem build *.gemspec && gem push --dry-run *.gem` and verify contents, generate CHANGELOG.md organized by category (added/changed/deprecated/removed/fixed), produce step-by-step upgrade notes with before/after code, set semantic version in `lib/[engine_name]/version.rb`, document deprecations with migration paths, load release assets conditionally and state which one informed the output. Trigger words: version bump, changelog, deprecation, gemspec, upgrade, release, publish gem, ship gem.
development
Orchestrates the full Rails TDD cycle with hard gates: test MUST exist, be run, and FAIL for the correct reason (e.g. undefined method, not syntax error) before any implementation code — propose minimal implementation and wait for user approval → verify test PASSES → run full suite with rubocop, brakeman, rspec all green → produce YARD documentation and self-reviewed PR; phases context/test design→implementation→iterate→finish. Use when practicing test-driven development, red-green-refactor, TDD workflow, writing tests before code, adding tests first, or building a Rails feature where specs must gate implementation.
development
Complete Rails project setup loop with hard gates: verify Ruby version matches .ruby-version, Bundler installed, database connection successful, all env vars loaded, and ALL external CI actions pinned to immutable commit SHAs (never mutable tags like @v4) → configure CI/CD pipeline with linting, testing, and security scanning → validate end-to-end with bundle install, db:create, db:migrate, rspec, and write SETUP_CHECKLIST.md; phases context/onboarding→CI/CD configuration→environment validation. Use when starting a new Rails project, running `rails new`, configuring a Gemfile or .ruby-version, setting up a development environment, or wiring up CI/CD for a Ruby on Rails app. Trigger: setup project, new Rails app, configure CI/CD, dev environment setup, rails new, Gemfile setup, .ruby-version, Ruby on Rails project bootstrap.