fix: Terminate IMAP connection after the job is complete (#9225)
This commit is contained in:
@@ -3,10 +3,17 @@ require 'net/imap'
|
|||||||
class Imap::BaseFetchEmailService
|
class Imap::BaseFetchEmailService
|
||||||
pattr_initialize [:channel!]
|
pattr_initialize [:channel!]
|
||||||
|
|
||||||
def perform
|
def fetch_emails
|
||||||
# Override this method
|
# Override this method
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def perform
|
||||||
|
inbound_emails = fetch_emails
|
||||||
|
terminate_imap_connection
|
||||||
|
|
||||||
|
inbound_emails
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def authentication_type
|
def authentication_type
|
||||||
@@ -105,6 +112,13 @@ class Imap::BaseFetchEmailService
|
|||||||
imap
|
imap
|
||||||
end
|
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)
|
def build_mail_from_string(raw_email_content)
|
||||||
Mail.read_from_string(raw_email_content)
|
Mail.read_from_string(raw_email_content)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
class Imap::FetchEmailService < Imap::BaseFetchEmailService
|
class Imap::FetchEmailService < Imap::BaseFetchEmailService
|
||||||
def perform
|
def fetch_emails
|
||||||
fetch_mail_for_channel
|
fetch_mail_for_channel
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
class Imap::MicrosoftFetchEmailService < Imap::BaseFetchEmailService
|
class Imap::MicrosoftFetchEmailService < Imap::BaseFetchEmailService
|
||||||
def perform
|
def fetch_emails
|
||||||
return if channel.provider_config['access_token'].blank?
|
return if channel.provider_config['access_token'].blank?
|
||||||
|
|
||||||
fetch_mail_for_channel
|
fetch_mail_for_channel
|
||||||
|
|||||||
@@ -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(: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], 'BODY.PEEK[HEADER]').and_return([email_header])
|
||||||
allow(imap).to receive(:fetch).with(1, 'RFC822').and_return([imap_fetch_mail])
|
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
|
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], 'BODY.PEEK[HEADER]')
|
||||||
expect(imap).to have_received(:fetch).with(1, 'RFC822')
|
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(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
|
||||||
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(: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], 'BODY.PEEK[HEADER]').and_return([email_header])
|
||||||
|
allow(imap).to receive(:logout)
|
||||||
|
|
||||||
result = described_class.new(channel: imap_email_channel).perform
|
result = described_class.new(channel: imap_email_channel).perform
|
||||||
|
|
||||||
|
|||||||
@@ -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(: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], 'BODY.PEEK[HEADER]').and_return([email_header])
|
||||||
allow(imap).to receive(:fetch).with(1, 'RFC822').and_return([imap_fetch_mail])
|
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
|
result = described_class.new(channel: microsoft_channel).perform
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user