Skip to content

Devise notification queueing not working as expected (sometimes?) #67

@jjb

Description

@jjb

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
end

then 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:

  1. User.email_confirmation_reminder_count is incremented on the in-memory object
  2. send_confirmation_instructions creates the background job. The background job is started and finishes before step 3...
  3. The User object, with email_confirmation_reminder_count incremented, 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

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions