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