Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nil recipient in email callback after updating to 1.5+ #179

Closed
clinejj opened this issue Nov 18, 2021 · 1 comment · Fixed by #180
Closed

nil recipient in email callback after updating to 1.5+ #179

clinejj opened this issue Nov 18, 2021 · 1 comment · Fixed by #180

Comments

@clinejj
Copy link
Contributor

clinejj commented Nov 18, 2021

We've been getting an issue where the recipient is nil in the after_email callback of a notification. We use deliver_later, which I realize gets called around the enqueuing of the delivery (and not explicitly after). The recipient is nil only during the after_email callback, but is present in both the database and email delivery methods.

My guess is that the removal of the @recipient instance variable in #126 here caused an issue but haven't had a chance to fully debug yet: https://github.com/excid3/noticed/pull/126/files#diff-8d74699b16f13d2f080f755ae12faee3a7cd24dd19833cf203f97bb7278db400L74-L76

We've seen this issue with 1.5.0, 1.5.1, and 1.5.2, but downgrading to 1.4.1 fixed it for us.

The rough setup of our notification classes:

class MessageNotifier
  include Sidekiq::Worker

  def perform(message_type, message_url)
    message = MessageService.find_message_by_url(message_type, message_url)
    MessageService.unreads(message_url).each do |user_id, unread_ts|
      recipient = User.find(user_id)
      next if recipient.nil?

      params = { unread_ts: unread_ts, message: message }
      UnreadMessagesNotification.with(params).deliver_later(recipient) # This is the notification call
    end
  end
end

class UnreadMessagesNotification < Noticed::Base
  deliver_by :database
  deliver_by :email, mailer: 'MessageMailer',
                     method: :unread_messages_email,
                     format: :generate_mailer_params

  after_email :mark_recipient_as_read
 
  param :unread_ts, :message

  def mark_recipient_as_read
    user_id = recipient.id # Recipient is nil here
    MessageService.mark_as_read(user_id, message, as_of: unread_ts)
  end

  def generate_mailer_params
    {
      unread_ts: unread_ts,
      message: message
    }
  end
end
@clinejj
Copy link
Contributor Author

clinejj commented Nov 18, 2021

Some further debugging led to above PR, it appears #126 did affect before callbacks (although it's not clear to me why/how)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant