mperham/sidekiq — pull request #1813 by sentience (2014-06-30 02:46:11+00:00)
Title: Improved (and more effective) fix for catching and ignoring exceptions caused by Sidekiq::Shutdown

This is a more complete fix for #1354 (see also #897) than that provided by #1805.

In the event a `Sidekiq::Shutdown` occurs during an ActiveRecord transaction, ActiveRecord will rescue the initial `ActiveRecord::StatementInvalid` and attempt to perform a rollback of the transaction. This in turn generates a second `ActiveRecord::StatementInvalid`, because the database connection is no longer available. By the time Sidekiq intercepts the exception, it’s an `ActiveRecord::StatementInvalid` caused by an `ActiveRecord::StatementInvalid`, caused by a `Sidekiq::Shutdown`.

The correct fix, therefore, is to recursively follow the `cause` chain of any exception raised by a job, and see if it has a `Sidekiq::Shutdown` at the end of it.

We have tested this fix in our application, which was producing duplicate jobs whenever we restarted Sidekiq during an ActiveRecord transaction, and the problem seems to be definitively solved.