handle more imap server failures

This commit is contained in:
Frédéric Péters 2014-09-23 14:25:57 +02:00
parent 410ea2902b
commit ddbcd61aa4
1 changed files with 21 additions and 5 deletions

View File

@ -126,10 +126,18 @@ while True:
M.login(section, cfg.get(section, 'password'))
except imaplib.IMAP4.error:
continue
M.select()
try:
M.select()
except socket.error:
logging.error('failure talking to imap server')
continue
typ, data = M.search(None, '(NOT SEEN)')
for num in data[0].split():
typ, data = M.fetch(num, '(RFC822)')
try:
typ, data = M.fetch(num, '(RFC822)')
except socket.error:
logging.error('failure talking to imap server')
break
msg = email.parser.Parser().parsestr(data[0][1])
enable_ocr = True
if 'disable_ocr' in msg['Subject']:
@ -141,10 +149,18 @@ while True:
payload = part.get_payload(decode=True)
if not process(dict(cfg.items(section)), filename, payload, enable_ocr):
logging.error(' error -> marking as unseen')
M.store(num, '-FLAGS', r'\Seen')
try:
M.store(num, '-FLAGS', r'\Seen')
except socket.error:
logging.error('failure talking to imap server')
pass
break
M.close()
M.logout()
try:
M.close()
M.logout()
except socket.error:
logging.error('failure talking to imap server')
pass
logging.debug('waiting a bit %s', time.strftime('%Y-%m-%d %H:%M:%S'))
time.sleep(30)