mhfs / devise-async

Send Devise's emails in background. Supports Resque, Sidekiq, Delayed::Job and QueueClassic.
MIT License
575 stars 110 forks source link

Getting ActiveRecord::RecordNotFound from time to time when using Sidekiq #95

Open Nowaker opened 8 years ago

Nowaker commented 8 years ago

I can clearly see after_commit being used in the code:

          after_commit :send_devise_pending_notifications

Yet, ActiveRecord::RecordNotFound still happens from time to time.

Any ideas how to fix it, @mperham?

mperham commented 8 years ago

I have no idea what the problem might be.

KazW commented 8 years ago

I've seen this in production in a few apps, my best guess is that Sidekiq is picking up the job before the transaction has completed. Adding a delay of a few seconds has fixed it in every case I've come across.

Nowaker commented 8 years ago

@KazW The real question is how. We can clearly see after_commit block is used. That means Sidekiq is either faster than light, or after_commit doesn't happen after the commit.

KazW commented 8 years ago

Ah, I've only used this with Devise 3.x when it was after_save... Nevermind!

voltechs commented 6 years ago

No resolution on this then? We're experiencing this with other Sidekiq jobs. Does anybody know of a way to simulate this in a test environment (we're specifically using Postgres)? We have some linear code that basically goes

some_model = SomeModel.create!(...)
SomeAsyncService.new.perform(some_model)

The service kicks off a Sidekiq Job, which picks up the job (impressively) before the model is committed (despite a proper primary key being set etc).

Nowaker commented 6 years ago

@voltechs No resolution, unfortunately. Given Sidekiq cannot be faster than light, after_commit must happen technically after a commit but before the result of that commit is available to database clients.