diff --git a/app/services/imap/base_fetch_email_service.rb b/app/services/imap/base_fetch_email_service.rb index 4f49dc980..1af3bdb5d 100644 --- a/app/services/imap/base_fetch_email_service.rb +++ b/app/services/imap/base_fetch_email_service.rb @@ -3,10 +3,17 @@ require 'net/imap' class Imap::BaseFetchEmailService pattr_initialize [:channel!] - def perform + def fetch_emails # Override this method end + def perform + inbound_emails = fetch_emails + terminate_imap_connection + + inbound_emails + end + private def authentication_type @@ -105,6 +112,13 @@ class Imap::BaseFetchEmailService imap end + def terminate_imap_connection + imap_client.logout + rescue Net::IMAP::Error => e + Rails.logger.info "Logout failed for #{channel.email} - #{e.message}." + imap_client.disconnect + end + def build_mail_from_string(raw_email_content) Mail.read_from_string(raw_email_content) end diff --git a/app/services/imap/fetch_email_service.rb b/app/services/imap/fetch_email_service.rb index f6d8de774..f602b3375 100644 --- a/app/services/imap/fetch_email_service.rb +++ b/app/services/imap/fetch_email_service.rb @@ -1,5 +1,5 @@ class Imap::FetchEmailService < Imap::BaseFetchEmailService - def perform + def fetch_emails fetch_mail_for_channel end diff --git a/app/services/imap/microsoft_fetch_email_service.rb b/app/services/imap/microsoft_fetch_email_service.rb index b6d3c03b8..ed3eb20f1 100644 --- a/app/services/imap/microsoft_fetch_email_service.rb +++ b/app/services/imap/microsoft_fetch_email_service.rb @@ -1,5 +1,5 @@ class Imap::MicrosoftFetchEmailService < Imap::BaseFetchEmailService - def perform + def fetch_emails return if channel.provider_config['access_token'].blank? fetch_mail_for_channel diff --git a/spec/services/imap/fetch_email_service_spec.rb b/spec/services/imap/fetch_email_service_spec.rb index 28a173951..46336bf0f 100644 --- a/spec/services/imap/fetch_email_service_spec.rb +++ b/spec/services/imap/fetch_email_service_spec.rb @@ -30,6 +30,7 @@ RSpec.describe Imap::FetchEmailService do allow(imap).to receive(:search).with(%w[SINCE 25-Oct-2020]).and_return([1]) allow(imap).to receive(:fetch).with([1], 'BODY.PEEK[HEADER]').and_return([email_header]) allow(imap).to receive(:fetch).with(1, 'RFC822').and_return([imap_fetch_mail]) + allow(imap).to receive(:logout) result = described_class.new(channel: imap_email_channel).perform @@ -39,6 +40,7 @@ RSpec.describe Imap::FetchEmailService do expect(imap).to have_received(:fetch).with([1], 'BODY.PEEK[HEADER]') expect(imap).to have_received(:fetch).with(1, 'RFC822') expect(logger).to have_received(:info).with("[IMAP::FETCH_EMAIL_SERVICE] Fetching mails from #{imap_email_channel.email}, found 1.") + expect(imap).to have_received(:logout) end end @@ -51,6 +53,7 @@ RSpec.describe Imap::FetchEmailService do allow(imap).to receive(:search).with(%w[SINCE 25-Oct-2020]).and_return([1]) allow(imap).to receive(:fetch).with([1], 'BODY.PEEK[HEADER]').and_return([email_header]) + allow(imap).to receive(:logout) result = described_class.new(channel: imap_email_channel).perform diff --git a/spec/services/imap/microsoft_fetch_email_service_spec.rb b/spec/services/imap/microsoft_fetch_email_service_spec.rb index 8f79b86b5..3a13ce0c6 100644 --- a/spec/services/imap/microsoft_fetch_email_service_spec.rb +++ b/spec/services/imap/microsoft_fetch_email_service_spec.rb @@ -35,6 +35,7 @@ RSpec.describe Imap::MicrosoftFetchEmailService do allow(imap).to receive(:search).with(%w[SINCE 25-Oct-2020]).and_return([1]) allow(imap).to receive(:fetch).with([1], 'BODY.PEEK[HEADER]').and_return([email_header]) allow(imap).to receive(:fetch).with(1, 'RFC822').and_return([imap_fetch_mail]) + allow(imap).to receive(:logout) result = described_class.new(channel: microsoft_channel).perform