Module: MailDeliveryTask::BaseAttempt

Extended by:
ActiveSupport::Concern
Defined in:
lib/mail_delivery_task/base_attempt.rb

Instance Method Summary collapse

Instance Method Details

#deliver!Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mail_delivery_task/base_attempt.rb', line 36

def deliver!
  return unless may_schedule?

  begin
    reload
    raise MailDeliveryTask::InvalidStateError unless pending?
    increment!(:num_attempts)
  rescue ActiveRecord::StaleObjectError
    retry
  end

  mailer_class = mailer_class_name.constantize

  mail = if mailer_args.present?
    mailer_class.send(mailer_action_name, **mailer_args.symbolize_keys)
  else
    mailer_class.send(mailer_action_name)
  end

  with_lock do
    raise MailDeliveryTask::InvalidStateError unless pending?

    begin
      self.persistence_token = persist_mail(mail) if should_persist?
    rescue StandardError => e
      # If Trunk is down, simply catch the exception so that we won't retry
      # and thus send the mail multiple times.
      handle_persist_mail_error(e)
    end

    begin
      # Perform the actual delivery through SMTP / Butter
      deliver_mail(mail)

      # Note that this fails silently.
      self.mailer_message_id = mail.message_id
      update_status!('delivered')
    rescue StandardError => e
      handle_deliver_error(e)
    end
  end
end

#expire!Object



79
80
81
82
83
84
# File 'lib/mail_delivery_task/base_attempt.rb', line 79

def expire!
  with_lock do
    raise MailDeliveryTask::InvalidStateError unless pending?
    update_status!('expired')
  end
end

#fail!Object



86
87
88
89
90
91
# File 'lib/mail_delivery_task/base_attempt.rb', line 86

def fail!
  with_lock do
    raise MailDeliveryTask::InvalidStateError unless pending?
    update_status!('failed')
  end
end

#may_schedule?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/mail_delivery_task/base_attempt.rb', line 93

def may_schedule?
  scheduled_at.blank? || scheduled_at < Time.current
end