summaryrefslogtreecommitdiff
path: root/addons/fetchmail/fetchmail.py
diff options
context:
space:
mode:
authorThibault Delavallée <tde@openerp.com>2014-02-13 12:59:22 +0100
committerThibault Delavallée <tde@openerp.com>2014-02-13 12:59:22 +0100
commit91305885e7dc365c039765d83d446dbbf5d8aa26 (patch)
treeca49251a749ccb9ab84e99e8e457378811aef308 /addons/fetchmail/fetchmail.py
parent409c3cfe9f1cdda1b8d9672fc3c0c246ed425894 (diff)
parent0f111f7b9656fa545dc068bf192ece06c92e87fd (diff)
[MERGE] [FIX] fetchmail: more coherent and robust behavior of fetchmail. Notably, do not discard the whole email stack or stop after first crashed email processing.
Featuring: - [FORWARD] [FIX] Forward port of revision 6920 of 6.1 addons branch, revid:odo@openerp.com-20120727150051-e1q0m92tyrazz82f. Purpose: some fixes and more robuts handling of incoming emails - [IMP] mail: mail_thread: message routing: raise exceptions instead of using asserts. Indeed assert are statements meant to be used when developping, for debug purpose. In a production environment it is safer to use real exceptions that can be managed accordingly. - [FIX] mail: fixed tests, no more AssertionError, but ValueError - [IMP] fetchmail: try / except each email processing. This allows to print an exception in the logs for every failed email processing, without crashing the whole stack. Any failed or succeeded email is set as seen, to avoid crashing every time the scheduler runs. bzr revid: tde@openerp.com-20140213115922-33vxgj7nfy86ashw
Diffstat (limited to 'addons/fetchmail/fetchmail.py')
-rw-r--r--addons/fetchmail/fetchmail.py49
1 files changed, 30 insertions, 19 deletions
diff --git a/addons/fetchmail/fetchmail.py b/addons/fetchmail/fetchmail.py
index d408e7da9ac..515881e8582 100644
--- a/addons/fetchmail/fetchmail.py
+++ b/addons/fetchmail/fetchmail.py
@@ -188,7 +188,7 @@ openerp_mailgate: "|/path/to/openerp-mailgate.py --host=localhost -u %(uid)d -p
for server in self.browse(cr, uid, ids, context=context):
_logger.info('start checking for new emails on %s server %s', server.type, server.name)
context.update({'fetchmail_server_id': server.id, 'server_type': server.type})
- count = 0
+ count, failed = 0, 0
imap_server = False
pop_server = False
if server.type == 'imap':
@@ -197,20 +197,26 @@ openerp_mailgate: "|/path/to/openerp-mailgate.py --host=localhost -u %(uid)d -p
imap_server.select()
result, data = imap_server.search(None, '(UNSEEN)')
for num in data[0].split():
+ res_id = None
result, data = imap_server.fetch(num, '(RFC822)')
- res_id = mail_thread.message_process(cr, uid, server.object_id.model,
- data[0][1],
- save_original=server.original,
- strip_attachments=(not server.attach),
- context=context)
+ imap_server.store(num, '-FLAGS', '\\Seen')
+ try:
+ res_id = mail_thread.message_process(cr, uid, server.object_id.model,
+ data[0][1],
+ save_original=server.original,
+ strip_attachments=(not server.attach),
+ context=context)
+ except Exception:
+ _logger.exception('Failed to process mail from %s server %s.', server.type, server.name)
+ failed += 1
if res_id and server.action_id:
- action_pool.run(cr, uid, [server.action_id.id], {'active_id': res_id, 'active_ids':[res_id], 'active_model': context.get("thread_model", server.object_id.model)})
- imap_server.store(num, '+FLAGS', '\\Seen')
- cr.commit()
+ action_pool.run(cr, uid, [server.action_id.id], {'active_id': res_id, 'active_ids': [res_id], 'active_model': context.get("thread_model", server.object_id.model)})
+ imap_server.store(num, '+FLAGS', '\\Seen')
+ cr.commit()
count += 1
- _logger.info("fetched/processed %s email(s) on %s server %s", count, server.type, server.name)
+ _logger.info("Fetched %d email(s) on %s server %s; %d succeeded, %d failed.", count, server.type, server.name, (count - failed), failed)
except Exception:
- _logger.exception("Failed to fetch mail from %s server %s.", server.type, server.name)
+ _logger.exception("General failure when trying to fetch mail from %s server %s.", server.type, server.name)
finally:
if imap_server:
imap_server.close()
@@ -223,18 +229,23 @@ openerp_mailgate: "|/path/to/openerp-mailgate.py --host=localhost -u %(uid)d -p
for num in range(1, numMsgs + 1):
(header, msges, octets) = pop_server.retr(num)
msg = '\n'.join(msges)
- res_id = mail_thread.message_process(cr, uid, server.object_id.model,
- msg,
- save_original=server.original,
- strip_attachments=(not server.attach),
- context=context)
+ res_id = None
+ try:
+ res_id = mail_thread.message_process(cr, uid, server.object_id.model,
+ msg,
+ save_original=server.original,
+ strip_attachments=(not server.attach),
+ context=context)
+ except Exception:
+ _logger.exception('Failed to process mail from %s server %s.', server.type, server.name)
+ failed += 1
if res_id and server.action_id:
- action_pool.run(cr, uid, [server.action_id.id], {'active_id': res_id, 'active_ids':[res_id], 'active_model': context.get("thread_model", server.object_id.model)})
+ action_pool.run(cr, uid, [server.action_id.id], {'active_id': res_id, 'active_ids': [res_id], 'active_model': context.get("thread_model", server.object_id.model)})
pop_server.dele(num)
cr.commit()
- _logger.info("fetched/processed %s email(s) on %s server %s", numMsgs, server.type, server.name)
+ _logger.info("Fetched %d email(s) on %s server %s; %d succeeded, %d failed.", numMsgs, server.type, server.name, (numMsgs - failed), failed)
except Exception:
- _logger.exception("Failed to fetch mail from %s server %s.", server.type, server.name)
+ _logger.exception("General failure when trying to fetch mail from %s server %s.", server.type, server.name)
finally:
if pop_server:
pop_server.quit()