Brandon Woodruff
Brandon Woodruff

Reputation: 123

Run callback before (or during) Sidekiq retry

I am looking to run a callback before (or when) a retry occurs for my Sidekiq worker jobs. We store the state of our jobs as an attribute (queued, performing, completed, etc.), and retries do not automatically reset this state. I need a way to run a callback that sets the retried job's state back to queued before the retried job gets queued. For example:

before_retry do 
  self.status = "queued"
end

I read another post (Sidekiq Pro callback when batch is retried?) suggesting some sort of server middleware to detect retries and run callbacks, but I don't entirely know if that will work as I feel the retry would already be queued by the time any middleware catches it. Are there any other options out there? Thanks in advance!

Upvotes: 1

Views: 1162

Answers (1)

Luke
Luke

Reputation: 2063

Would it work to rescue inside perform?

def perform
  ...
rescue YourException
  self.status = "queued"
  raise
end

raiseing again so the job is retried.

Upvotes: 1

Related Questions