fix: Terminate IMAP connection after the job is complete (#9225)

This commit is contained in:
Pranav
2024-04-11 15:47:43 -07:00
committed by GitHub
parent 0477b8ca14
commit e8fe3c7c05
5 changed files with 21 additions and 3 deletions

View File

@@ -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

View File

@@ -1,5 +1,5 @@
class Imap::FetchEmailService < Imap::BaseFetchEmailService
def perform
def fetch_emails
fetch_mail_for_channel
end

View File

@@ -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