Search code examples
sidekiqrails-activejob

How to handle exceptions from nested jobs?


I have a Sidekiq / ActiveJob class that looks like:

class ParentJob < ApplicationJob
  queue_as :default

  def perform(user)
    ChildJobA.perform_now(user)
    ChildJobB.perform_now(user)
    ChildJobC.perform_now(user)
    ...
  rescue CustomError => e
    handle_error(e)
  end
end

The parent job is called via ParentJob.perform_later(user).

The child jobs can raise a CustomError. I thought that I could rescue those in the parent job – but my rescue block is never called. It seems that the child job simply exits and control does not pass back to the parent.

Is there a way to rescue custom errors in the parent job or do I have to repeat the same error handler in every child job?


Solution

  • Call it like normal Ruby code and remove ActiveJob from the picture:

    ChildJobA.new.perform(user)
    ChildJobB.new.perform(user)
    ChildJobC.new.perform(user)
    

    That should raise as normal.