Fixes issue #2554 - Downloading of emails via IMAP takes longer as one minute. E-Mail seems to be lost.

This commit is contained in:
Martin Edenhofer 2019-04-30 15:45:01 +02:00
parent 5e522f4276
commit 1e745f00d8

View file

@ -3,6 +3,10 @@ require 'net/imap'
class Channel::Driver::Imap < Channel::EmailParser class Channel::Driver::Imap < Channel::EmailParser
FETCH_METADATA_TIMEOUT = 2.minutes
FETCH_MSG_TIMEOUT = 4.minutes
EXPUNGE_TIMEOUT = 16.minutes
def fetchable?(_channel) def fetchable?(_channel)
true true
end end
@ -171,7 +175,7 @@ example
message_ids.each do |message_id| message_ids.each do |message_id|
message_meta = nil message_meta = nil
timeout(1.minute) do timeout(FETCH_METADATA_TIMEOUT) do
message_meta = @imap.fetch(message_id, ['ENVELOPE'])[0].attr message_meta = @imap.fetch(message_id, ['ENVELOPE'])[0].attr
end end
@ -215,7 +219,7 @@ example
Rails.logger.info " - message #{count}/#{count_all}" Rails.logger.info " - message #{count}/#{count_all}"
message_meta = nil 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] message_meta = @imap.fetch(message_id, ['RFC822.SIZE', 'ENVELOPE', 'FLAGS', 'INTERNALDATE', 'RFC822.HEADER'])[0]
end end
@ -237,26 +241,41 @@ example
# delete email from server after article was created # delete email from server after article was created
msg = nil msg = nil
timeout(1.minute) do begin
msg = @imap.fetch(message_id, 'RFC822')[0].attr['RFC822'] 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 end
next if !msg next if !msg
process(channel, msg, false) process(channel, msg, false)
timeout(1.minute) do begin
if !keep_on_server timeout(FETCH_MSG_TIMEOUT) do
@imap.store(message_id, '+FLAGS', [:Deleted]) if !keep_on_server
else @imap.store(message_id, '+FLAGS', [:Deleted])
@imap.store(message_id, '+FLAGS', [:Seen]) else
@imap.store(message_id, '+FLAGS', [:Seen])
end
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 end
count_fetched += 1 count_fetched += 1
end end
if !keep_on_server if !keep_on_server
timeout(10.minutes) do begin
@imap.expunge() 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
end end
disconnect disconnect