require 'net/imap' class Inboxes::FetchImapEmailsJob < MutexApplicationJob queue_as :scheduled_jobs def perform(channel) return unless should_fetch_email?(channel) key = format(::Redis::Alfred::EMAIL_MESSAGE_MUTEX, inbox_id: channel.inbox.id) with_lock(key, 5.minutes) do process_email_for_channel(channel) end rescue *ExceptionList::IMAP_EXCEPTIONS => e Rails.logger.error e channel.authorization_error! rescue EOFError, OpenSSL::SSL::SSLError, Net::IMAP::NoResponseError, Net::IMAP::BadResponseError, Net::IMAP::InvalidResponseError => e Rails.logger.error e rescue LockAcquisitionError Rails.logger.error "Lock failed for #{channel.inbox.id}" rescue StandardError => e ChatwootExceptionTracker.new(e, account: channel.account).capture_exception end private def should_fetch_email?(channel) channel.imap_enabled? && !channel.reauthorization_required? end def process_email_for_channel(channel) inbound_emails = if channel.microsoft? Imap::MicrosoftFetchEmailService.new(channel: channel).perform else Imap::FetchEmailService.new(channel: channel).perform end inbound_emails.map do |inbound_mail| process_mail(inbound_mail, channel) end end def process_mail(inbound_mail, channel) Imap::ImapMailbox.new.process(inbound_mail, channel) rescue StandardError => e ChatwootExceptionTracker.new(e, account: channel.account).capture_exception Rails.logger.error(" #{channel.provider} Email dropped: #{inbound_mail.from} and message_source_id: #{inbound_mail.message_id}") end end