Skip to content
Permalink
Browse files

Fixes issue #2554 - Downloading of emails via IMAP takes longer as on…

…e minute. E-Mail seems to be lost.
  • Loading branch information...
znuny-robo committed Apr 30, 2019
1 parent 5e522f4 commit 1e745f00d81881043a11035089ed31fcf3f1c5f7
Showing with 30 additions and 11 deletions.
  1. +30 −11 app/models/channel/driver/imap.rb
@@ -3,6 +3,10 @@

class Channel::Driver::Imap < Channel::EmailParser

FETCH_METADATA_TIMEOUT = 2.minutes
FETCH_MSG_TIMEOUT = 4.minutes
EXPUNGE_TIMEOUT = 16.minutes

def fetchable?(_channel)
true
end
@@ -171,7 +175,7 @@ def fetch(options, channel, check_type = '', verify_string = '')
message_ids.each do |message_id|

message_meta = nil
timeout(1.minute) do
timeout(FETCH_METADATA_TIMEOUT) do
message_meta = @imap.fetch(message_id, ['ENVELOPE'])[0].attr
end

@@ -215,7 +219,7 @@ def fetch(options, channel, check_type = '', verify_string = '')
Rails.logger.info " - message #{count}/#{count_all}"

message_meta = nil
timeout(1.minute) do
timeout(FETCH_METADATA_TIMEOUT) do
message_meta = @imap.fetch(message_id, ['RFC822.SIZE', 'ENVELOPE', 'FLAGS', 'INTERNALDATE', 'RFC822.HEADER'])[0]
end

@@ -237,26 +241,41 @@ def fetch(options, channel, check_type = '', verify_string = '')

# delete email from server after article was created
msg = nil
timeout(1.minute) do
msg = @imap.fetch(message_id, 'RFC822')[0].attr['RFC822']
begin
timeout(FETCH_MSG_TIMEOUT) do
msg = @imap.fetch(message_id, 'RFC822')[0].attr['RFC822']
end
rescue Timeout::Error => e
Rails.logger.error "Unable to fetch email from #{count}/#{count_all} from server (#{options[:host]}/#{options[:user]}): #{e.inspect}"
raise e
end
next if !msg

process(channel, msg, false)

timeout(1.minute) do
if !keep_on_server
@imap.store(message_id, '+FLAGS', [:Deleted])
else
@imap.store(message_id, '+FLAGS', [:Seen])
begin
timeout(FETCH_MSG_TIMEOUT) do
if !keep_on_server
@imap.store(message_id, '+FLAGS', [:Deleted])
else
@imap.store(message_id, '+FLAGS', [:Seen])
end
end
rescue Timeout::Error => e
Rails.logger.error "Unable to set +FLAGS for email #{count}/#{count_all} on server (#{options[:host]}/#{options[:user]}): #{e.inspect}"
raise e
end
count_fetched += 1
end

if !keep_on_server
timeout(10.minutes) do
@imap.expunge()
begin
timeout(EXPUNGE_TIMEOUT) do
@imap.expunge()
end
rescue Timeout::Error => e
Rails.logger.error "Unable to expunge server (#{options[:host]}/#{options[:user]}): #{e.inspect}"
raise e
end
end
disconnect

0 comments on commit 1e745f0

Please sign in to comment.
You can’t perform that action at this time.