Jobs
Nerve tracks individual jobs through their lifecycle - from enqueue to completion or failure.Job Lifecycle
Copy
Enqueued → Waiting → Running → Completed
↓
Failed → Retry → ...
↓
Dead (exhausted retries)
Listing Jobs
Copy
# Recent jobs
jobs = BrainzLab::Nerve.jobs(limit: 50)
# Filter by status
running = BrainzLab::Nerve.jobs(status: :running)
failed = BrainzLab::Nerve.jobs(status: :failed)
# Filter by queue
mailer_jobs = BrainzLab::Nerve.jobs(queue: :mailers)
# Filter by job class
user_jobs = BrainzLab::Nerve.jobs(class_name: "UserMailer")
Job Details
Copy
job = BrainzLab::Nerve.job("job_abc123")
puts job.id # => "job_abc123"
puts job.class_name # => "SendWelcomeEmailJob"
puts job.queue # => "mailers"
puts job.status # => :completed
puts job.enqueued_at # => 2024-01-15 10:30:00
puts job.started_at # => 2024-01-15 10:30:01
puts job.completed_at # => 2024-01-15 10:30:03
puts job.duration # => 2.1 (seconds)
puts job.arguments # => [user_id: 123]
puts job.attempts # => 1
Job Performance
Track job duration by class:Copy
stats = BrainzLab::Nerve.job_stats("SendWelcomeEmailJob")
puts stats.total_runs # => 1523
puts stats.avg_duration # => 2.3 (seconds)
puts stats.p50_duration # => 1.8
puts stats.p95_duration # => 4.2
puts stats.p99_duration # => 8.1
puts stats.failure_rate # => 0.5 (percent)
puts stats.avg_wait_time # => 0.3 (seconds)
Failed Jobs
View failed jobs with error details:Copy
failed = BrainzLab::Nerve.failed_jobs(limit: 20)
failed.each do |job|
puts "#{job.class_name}: #{job.error_class}"
puts " Message: #{job.error_message}"
puts " Backtrace: #{job.backtrace.first(5).join("\n")}"
puts " Attempts: #{job.attempts}/#{job.max_attempts}"
end
Retry Jobs
Retry a failed job:Copy
# Retry a specific job
BrainzLab::Nerve.retry_job("job_abc123")
# Retry all failed jobs of a class
BrainzLab::Nerve.retry_all(class_name: "ImportDataJob")
# Retry failed jobs from last hour
BrainzLab::Nerve.retry_all(failed_since: 1.hour.ago)
Job Search
Find specific jobs:Copy
# Search by arguments
jobs = BrainzLab::Nerve.search_jobs(
arguments: { user_id: 123 }
)
# Search by error
jobs = BrainzLab::Nerve.search_jobs(
error_class: "ActiveRecord::RecordNotFound"
)
# Search by time range
jobs = BrainzLab::Nerve.search_jobs(
started_after: 1.hour.ago,
started_before: Time.current
)
Scheduled Jobs
View jobs scheduled for future execution:Copy
scheduled = BrainzLab::Nerve.scheduled_jobs
scheduled.each do |job|
puts "#{job.class_name} scheduled for #{job.scheduled_at}"
end
# Filter by time
upcoming = BrainzLab::Nerve.scheduled_jobs(
scheduled_before: 1.hour.from_now
)
Job Cancellation
Cancel a pending job:Copy
# Cancel a scheduled job
BrainzLab::Nerve.cancel_job("job_abc123")
# Cancel all scheduled jobs of a class
BrainzLab::Nerve.cancel_all(class_name: "ExpiredDataCleanupJob")