-
Notifications
You must be signed in to change notification settings - Fork 108
Description
Here's a situation I am facing. It's very specific to my app, but I'm pretty sure has a root cause in devise-async.
I sometimes remind users who haven't confirmed their email addresses to do so. It goes something like this (some things renamed/simplified):
class User
def remind
self.email_confirmation_reminder_count += 1
send_confirmation_instructions
save!
end
endthen in the email template...
- if @user.email_confirmation_reminder_count > 0
%h6 Complete your account registration.
- else
%h6 Welcome!Problem: Sometimes when sending the confirmation instructions in this way, @user.email_confirmation_reminder_count is 0, even though it is incremented in User#remind. Inspecting the object at the console after the email has been sent, the incrementing was indeed successful as expected. So, for some reason the User object that was accessed within the template has the value set to 0, but persisting the incremented value within User#remind was successful.
And as a reminder, this problem only happens sometimes.
A Pretty Good Theory
I've actually seen a similar phenomenon before, and I thought it might be the case here too. If it is the case here, then this is what happens:
User.email_confirmation_reminder_countis incremented on the in-memory objectsend_confirmation_instructionscreates the background job. The background job is started and finishes before step 3...- The
Userobject, withemail_confirmation_reminder_countincremented, is persisted to the DB.
But That Theory Doesn't Hold With devise-async
As you may know, devise-async specifically goes to great lengths to avoid such a situation: https://github.com/mhfs/devise-async/blob/master/lib/devise/async/model.rb
Beginnings of Other Theories
- for some reason after incrementing
email_confirmation_reminder_count, theUserobject is not consideredchanged?by devise-async https://github.com/mhfs/devise-async/blob/master/lib/devise/async/model.rb#L38 - but this seems basically impossible - for some reason the
Userobject is reloaded... somewhere... in devise, devise-async, or elsewhere - the after_commit hook is being triggered prematurely https://github.com/mhfs/devise-async/blob/master/lib/devise/async/model.rb#L15
Help Me
If anyone has any ideas or suggestions for what to investigate, it would be much appreciated!
Hopefully when I solve this I can contribute some documentation that will help others!