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

Exception handling with DelayedJob #35

Closed
jonathansimmons opened this issue Jul 8, 2013 · 3 comments
Closed

Exception handling with DelayedJob #35

jonathansimmons opened this issue Jul 8, 2013 · 3 comments

Comments

@jonathansimmons
Copy link

I've been reviewing issue #19 to clear up the best way to handle postmark exceptions. I thought I'd found the answer I was looking for but it seems i'm only halfway there. As @dgilperez points out the suggested code does work but doesn't seem to work with in conjunction with delayed job.

I believe this will probably be something we need to address with delayed job but thought I'd see if the postmark team had any thoughts or fixes for the issue before I got to far down the rabbit hole.

The following is working for me without DelayedJob in the workflow.

class ApplicationController
  rescue_from Postmark::InvalidMessageError, :with => :handle_email_error

  def handle_email_error
    # Handle exception here
  end
end
@temochka
Copy link
Contributor

temochka commented Jul 9, 2013

Hey,

This is all about context. If you use DelayedJob to deliver your emails in the background, any exceptions risen will appear in the context of a running worker. Whereas ActionController::Base.rescue_from will only help for ones delivered synchronously in your controller. Also, there is usually no universal solution to handle all of these errors. Ideally you should try to reactivate an inactive address using the Postmark API 1-2 times within a few days after it got bounced (to fix any temporary delivery issues), but if this doesn’t help, stop sending any emails to the address.

This has nothing to do with the gem, it’s a pure Ruby and your architectural choices. I don’t use DelayedJob too much, but I don’t see anything special about it in this case. From what I know this should work fine:

class User < ActiveRecord::Base
  def deliver_welcome_email
    UserMailer.welcome_email(self).deliver
  rescue Postmark::InvalidMessageError => e
    # ...
  end
end

u = User.first
u.delay.deliver_welcome_email

If you send a lot of different emails, you might want to delegate error handling to a separate class (this would also be handy if you decide to implement a bounce hook), but again, this is all up to you. Please let me know if I miss something or you need any further guidance here.

@temochka temochka closed this as completed Jul 9, 2013
@noctivityinc
Copy link

I have no idea why this is happening but this doesnt appear to work. While the email is being sent, we are getting tons of errors such as this:

NoMethodError: undefined method `deliver_comment_email' for #<Comment:0x00000009543748>

The code is pretty trivial:

def deliver_comment_email(email, board, attachments, reference)
  begin
    CommentMailer.comment_posted(email, self, board, attachments, reference).deliver 
  rescue Postmark::InvalidMessageError
    self.errors.add :base, "Sorry but the email address #{email} bounced."
  end
end

and we're calling it from a private method after_create:

self.delay.deliver_comment_email(email, board, attachments, reference)

Like I said, the email actually goes through and is delivered, and DJ eventually removes the message from the queue, but we still get these errors. Not sure where to go from here.

@temochka
Copy link
Contributor

@noctivityinc this looks like a problem in DJ to me. A similar problem has occurred before (see collectiveidea/delayed_job#323). Please make sure you’re currently running a version that includes collectiveidea/delayed_job@0234444. If you can’t figure out, feel free to mail us at support@postmarkapp.com or create a separate issue describing how we can reproduce the problem.

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

No branches or pull requests

3 participants