Search code examples
ruby-on-railsrubysidekiq

Sidekiq background job rollback when fail


I want to know if a sidekiq background job will rollback its database if the job fail to finish? i.e machine died or something like that.

I'm using rails 4.2.1, sidekiq 4.0.1


Solution

  • Sidekiq doesn't really handle transactions, but Rails does. So if you have several statements you should wrap them in a transaction i.e.:

    ActiveRecord::Base.transaction do
      User.update(amount: 100)
      Bank.update(amount: 0)
    end
    

    Then, if something goes wrong, these changes will be rolled back.