class MailHandler::Handler

Base handler that does it all

Public Class Methods

begin_work_loop() click to toggle source
# File lib/mailhandler.rb, line 7
def self.begin_work_loop
  loop do
    begin
      new.do_work
    rescue StandardError => e
      Logger.error(
        subject: 'Main loop',
        error_type: e.class.to_s,
        trace: e.to_s
      )
      raise e
    end
  end
end

Public Instance Methods

do_work() click to toggle source
# File lib/mailhandler.rb, line 22
def do_work
  if messages.any?
    messages.each do |message|
      if response = send(email_from(message))
        message.delete unless response.error?
      end
    end
  else
    sleep 1
  end
end

Private Instance Methods

email_from(message) click to toggle source
# File lib/mailhandler.rb, line 40
def email_from(message)
  MailHandler::Models::Email.new(
    JSON.parse(message.body, symbolize_names: true)
  )
end
messages() click to toggle source
# File lib/mailhandler.rb, line 36
def messages
  @messages ||= SQS.client.fetch_messages
end
send(email) click to toggle source
# File lib/mailhandler.rb, line 46
def send(email)
  SES.mailer.send_mail email
end
wait_for(worker) click to toggle source
# File lib/mailhandler.rb, line 50
def wait_for(worker)
  sleep(0.01) until worker.done?
end