Permalink
Browse files

Fixed issue #2321 - IMAP fetch stops because of request timeout

  • Loading branch information...
thorsteneckel committed Nov 6, 2018
1 parent 13b3b84 commit ea318fb695f49553ec42335d2b129f4b827a47b3
Showing with 61 additions and 23 deletions.
  1. +61 −23 app/models/channel/driver/imap.rb
@@ -94,32 +94,40 @@ def fetch(options, channel, check_type = '', verify_string = '')
Rails.logger.info "fetching imap (#{options[:host]}/#{options[:user]} port=#{port},ssl=#{ssl},starttls=#{starttls},folder=#{folder},keep_on_server=#{keep_on_server})"
# on check, reduce open_timeout to have faster probing
timeout = 45
@timeout = 45
if check_type == 'check'
timeout = 6
@timeout = 6
end
Timeout.timeout(timeout) do
timeout do
@imap = ::Net::IMAP.new(options[:host], port, ssl, nil, false)
if starttls
@imap.starttls()
end
end
@imap.login(options[:user], options[:password])
timeout do
@imap.login(options[:user], options[:password])
end
# select folder
@imap.select(folder)
timeout do
# select folder
@imap.select(folder)
end
# sort messages by date on server (if not supported), if not fetch messages via search (first in, first out)
filter = ['ALL']
if keep_on_server && check_type != 'check' && check_type != 'verify'
filter = %w[NOT SEEN]
end
begin
message_ids = @imap.sort(['DATE'], filter, 'US-ASCII')
rescue
message_ids = @imap.search(filter)
message_ids = nil
timeout do
begin
message_ids = @imap.sort(['DATE'], filter, 'US-ASCII')
rescue
message_ids = @imap.search(filter)
end
end
# check mode only
@@ -131,7 +139,10 @@ def fetch(options, channel, check_type = '', verify_string = '')
# check messages
message_ids.each do |message_id|
message_meta = @imap.fetch(message_id, ['RFC822.HEADER'])[0].attr
message_meta = nil
timeout do
message_meta = @imap.fetch(message_id, ['RFC822.HEADER'])[0].attr
end
# check how many content messages we have, for notice used
header = message_meta['RFC822.HEADER']
@@ -158,16 +169,21 @@ def fetch(options, channel, check_type = '', verify_string = '')
# check for verify message
message_ids.each do |message_id|
message_meta = @imap.fetch(message_id, ['ENVELOPE'])[0].attr
message_meta = nil
timeout do
message_meta = @imap.fetch(message_id, ['ENVELOPE'])[0].attr
end
# check if verify message exists
subject = message_meta['ENVELOPE'].subject
next if !subject
next if subject !~ /#{verify_string}/
Rails.logger.info " - verify email #{verify_string} found"
@imap.store(message_id, '+FLAGS', [:Deleted])
@imap.expunge()
timeout do
@imap.store(message_id, '+FLAGS', [:Deleted])
@imap.expunge()
end
disconnect
return {
result: 'ok',
@@ -189,7 +205,10 @@ def fetch(options, channel, check_type = '', verify_string = '')
count += 1
Rails.logger.info " - message #{count}/#{count_all}"
message_meta = @imap.fetch(message_id, ['RFC822.SIZE', 'ENVELOPE', 'FLAGS', 'INTERNALDATE'])[0]
message_meta = nil
timeout do
message_meta = @imap.fetch(message_id, ['RFC822.SIZE', 'ENVELOPE', 'FLAGS', 'INTERNALDATE'])[0]
end
# ignore to big messages
info = too_big?(message_meta, count, count_all)
@@ -205,19 +224,28 @@ def fetch(options, channel, check_type = '', verify_string = '')
next if already_imported?(message_id, message_meta, count, count_all, keep_on_server, channel)
# delete email from server after article was created
msg = @imap.fetch(message_id, 'RFC822')[0].attr['RFC822']
msg = nil
timeout do
msg = @imap.fetch(message_id, 'RFC822')[0].attr['RFC822']
end
next if !msg
process(channel, msg, false)
if !keep_on_server
@imap.store(message_id, '+FLAGS', [:Deleted])
else
@imap.store(message_id, '+FLAGS', [:Seen])
timeout do
if !keep_on_server
@imap.store(message_id, '+FLAGS', [:Deleted])
else
@imap.store(message_id, '+FLAGS', [:Seen])
end
end
count_fetched += 1
end
if !keep_on_server
@imap.expunge()
timeout do
@imap.expunge()
end
end
disconnect
if count.zero?
@@ -234,7 +262,9 @@ def fetch(options, channel, check_type = '', verify_string = '')
def disconnect
return if !@imap
@imap.disconnect()
timeout do
@imap.disconnect()
end
end
=begin
@@ -273,7 +303,9 @@ def already_imported?(message_id, message_meta, count, count_all, keep_on_server
return false if ticket.preferences[:channel_id] != channel[:id]
end
@imap.store(message_id, '+FLAGS', [:Seen])
timeout do
@imap.store(message_id, '+FLAGS', [:Seen])
end
Rails.logger.info " - ignore message #{count}/#{count_all} - because message message id already imported"
true
end
@@ -296,4 +328,10 @@ def too_big?(message_meta, count, count_all)
false
end
def timeout
Timeout.timeout(@timeout) do
yield
end
end
end

0 comments on commit ea318fb

Please sign in to comment.