Fixed issue #2321 - IMAP fetch stops because of request timeout
This commit is contained in:
parent
13b3b841d5
commit
ea318fb695
1 changed files with 61 additions and 23 deletions
|
@ -94,33 +94,41 @@ example
|
|||
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
|
||||
|
||||
timeout do
|
||||
@imap.login(options[:user], options[:password])
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
if check_type == 'check'
|
||||
|
@ -131,7 +139,10 @@ example
|
|||
# check messages
|
||||
message_ids.each do |message_id|
|
||||
|
||||
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,7 +169,10 @@ example
|
|||
# check for verify message
|
||||
message_ids.each do |message_id|
|
||||
|
||||
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
|
||||
|
@ -166,8 +180,10 @@ example
|
|||
next if subject !~ /#{verify_string}/
|
||||
|
||||
Rails.logger.info " - verify email #{verify_string} found"
|
||||
timeout do
|
||||
@imap.store(message_id, '+FLAGS', [:Deleted])
|
||||
@imap.expunge()
|
||||
end
|
||||
disconnect
|
||||
return {
|
||||
result: 'ok',
|
||||
|
@ -189,7 +205,10 @@ example
|
|||
count += 1
|
||||
Rails.logger.info " - message #{count}/#{count_all}"
|
||||
|
||||
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,20 +224,29 @@ example
|
|||
next if already_imported?(message_id, message_meta, count, count_all, keep_on_server, channel)
|
||||
|
||||
# delete email from server after article was created
|
||||
msg = nil
|
||||
timeout do
|
||||
msg = @imap.fetch(message_id, 'RFC822')[0].attr['RFC822']
|
||||
end
|
||||
next if !msg
|
||||
|
||||
process(channel, msg, false)
|
||||
|
||||
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
|
||||
timeout do
|
||||
@imap.expunge()
|
||||
end
|
||||
end
|
||||
disconnect
|
||||
if count.zero?
|
||||
Rails.logger.info ' - no message'
|
||||
|
@ -234,8 +262,10 @@ example
|
|||
def disconnect
|
||||
return if !@imap
|
||||
|
||||
timeout do
|
||||
@imap.disconnect()
|
||||
end
|
||||
end
|
||||
|
||||
=begin
|
||||
|
||||
|
@ -273,7 +303,9 @@ returns
|
|||
return false if ticket.preferences[:channel_id] != channel[:id]
|
||||
end
|
||||
|
||||
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 @@ returns
|
|||
false
|
||||
end
|
||||
|
||||
def timeout
|
||||
Timeout.timeout(@timeout) do
|
||||
yield
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue