feat: Add configurable interval for IMAP sync (#9302)

This commit is contained in:
Pranav
2024-04-25 18:58:20 -07:00
committed by GitHub
parent e757237029
commit 77db0d0701
4 changed files with 51 additions and 15 deletions

View File

@@ -3,13 +3,13 @@ require 'net/imap'
class Inboxes::FetchImapEmailsJob < MutexApplicationJob
queue_as :scheduled_jobs
def perform(channel)
def perform(channel, interval = 1)
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)
process_email_for_channel(channel, interval)
end
rescue *ExceptionList::IMAP_EXCEPTIONS => e
Rails.logger.error "Authorization error for email channel - #{channel.inbox.id} : #{e.message}"
@@ -28,11 +28,11 @@ class Inboxes::FetchImapEmailsJob < MutexApplicationJob
channel.imap_enabled? && !channel.reauthorization_required?
end
def process_email_for_channel(channel)
def process_email_for_channel(channel, interval)
inbound_emails = if channel.microsoft?
Imap::MicrosoftFetchEmailService.new(channel: channel).perform
Imap::MicrosoftFetchEmailService.new(channel: channel, interval: interval).perform
else
Imap::FetchEmailService.new(channel: channel).perform
Imap::FetchEmailService.new(channel: channel, interval: interval).perform
end
inbound_emails.map do |inbound_mail|
process_mail(inbound_mail, channel)

View File

@@ -1,7 +1,7 @@
require 'net/imap'
class Imap::BaseFetchEmailService
pattr_initialize [:channel!]
pattr_initialize [:channel!, :interval]
def fetch_emails
# Override this method
@@ -99,10 +99,10 @@ class Imap::BaseFetchEmailService
end
# Sends a SEARCH command to search the mailbox for messages that were
# created between yesterday and today and returns message sequence numbers.
# created between yesterday (or given date) and today and returns message sequence numbers.
# Return <message set>
def fetch_available_mail_sequence_numbers
imap_client.search(['SINCE', yesterday])
imap_client.search(['SINCE', since])
end
def build_imap_client
@@ -123,7 +123,8 @@ class Imap::BaseFetchEmailService
Mail.read_from_string(raw_email_content)
end
def yesterday
(Time.zone.today - 1).strftime('%d-%b-%Y')
def since
previous_day = Time.zone.today - (interval || 1).to_i
previous_day.strftime('%d-%b-%Y')
end
end