Skip to content

Commit

Permalink
IMP l10n_it_fatturapa_pec avoiding to block fetchmail server in case …
Browse files Browse the repository at this point in the history
…of temporary errors. (OCA#914)

Retry N (configurable) times before resetting fetchmail server.
Notify contacts about reset
  • Loading branch information
eLBati authored and SimoRubi committed Aug 7, 2019
1 parent 9c165e1 commit 5e40567
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
3 changes: 2 additions & 1 deletion l10n_it_fatturapa_pec/__manifest__.py
Expand Up @@ -7,7 +7,7 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).
{
'name': 'Italian Localization - Fattura elettronica - Supporto PEC',
'version': '10.0.1.4.0',
'version': '10.0.1.5.0',
'category': 'Localization/Italy',
'summary': 'Invio fatture elettroniche tramite PEC',
'author': 'Openforce Srls Unipersonale, Odoo Community Association (OCA)',
Expand All @@ -27,6 +27,7 @@
'views/fetchmail_view.xml',
'security/ir.model.access.csv',
'views/company_view.xml',
'data/fetchmail_data.xml',
],
'installable': True
}
9 changes: 9 additions & 0 deletions l10n_it_fatturapa_pec/data/fetchmail_data.xml
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<record id="fetchmail_pec_max_retry" model="ir.config_parameter">
<field name="key">fetchmail.pec.max.retry</field>
<field name="value">5</field>
</record>
</data>
</odoo>
56 changes: 47 additions & 9 deletions l10n_it_fatturapa_pec/models/fetchmail.py
Expand Up @@ -3,7 +3,7 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).

import logging
from odoo import models, api, fields
from odoo import models, api, fields, _

_logger = logging.getLogger(__name__)
MAX_POP_MESSAGES = 50
Expand All @@ -17,6 +17,7 @@ def _default_e_inv_notify_partner_ids(self):

last_pec_error_message = fields.Text(
"Last PEC Error Message", readonly=True)
pec_error_count = fields.Integer("PEC error count", readonly=True)
e_inv_notify_partner_ids = fields.Many2many(
"res.partner", string="Contacts to notify",
help="Contacts to notify when PEC message can't be processed",
Expand Down Expand Up @@ -46,6 +47,7 @@ def fetch_mail(self):
additional_context['server_type'] = server.type
imap_server = None
pop_server = None
error_raised = False
if server.type == 'imap':
try:
imap_server = server.connect()
Expand All @@ -71,11 +73,9 @@ def fetch_mail(self):
server.type, server.name, exc_info=True
)
# Here is where we need to intervene.
# Setting to draft prevents new e-invoices to
# be sent via PEC
server.state = 'draft'
server.last_pec_error_message = str(e)
break
error_raised = True
continue
imap_server.store(num, '+FLAGS', '\\Seen')
# We need to commit because message is processed:
# Possible next exceptions, out of try, should not
Expand All @@ -86,8 +86,8 @@ def fetch_mail(self):
"General failure when trying to fetch mail from "
"%s server %s.",
server.type, server.name, exc_info=True)
server.state = 'draft'
server.last_pec_error_message = str(e)
error_raised = True
finally:
if imap_server:
imap_server.close()
Expand Down Expand Up @@ -122,9 +122,9 @@ def fetch_mail(self):
server.type, server.name, exc_info=True
)
# See the comments in the IMAP part
server.state = 'draft'
error_raised = True
server.last_pec_error_message = str(e)
break
continue
self._cr.commit()
if num_messages < MAX_POP_MESSAGES:
break
Expand All @@ -135,10 +135,48 @@ def fetch_mail(self):
" server %s.",
server.type, server.name, exc_info=True)
# See the comments in the IMAP part
server.state = 'draft'
error_raised = True
server.last_pec_error_message = str(e)
finally:
if pop_server:
pop_server.quit()
if error_raised:
server.pec_error_count += 1
max_retry = self.env['ir.config_parameter'].get_param(
'fetchmail.pec.max.retry')
if server.pec_error_count > int(max_retry):
# Setting to draft prevents new e-invoices to
# be sent via PEC.
# Resetting server state only after N fails.
# So that the system can try to fetch again after
# temporary connection errors
server.state = 'draft'
server.notify_about_server_reset()
else:
server.pec_error_count = 0
server.write({'date': fields.Datetime.now()})
return True

def notify_about_server_reset(self):
if self.e_inv_notify_partner_ids:
self.env['mail.mail'].create({
'subject': _(
"Fetchmail PEC server [%s] reset"
) % self.name,
'body_html': _(
"<p>"
"PEC server %s has been reset. Last error message is</p>"
"<p><strong>%s</strong></p>"
) % (self.name, self.last_pec_error_message),
'recipient_ids': [(
6, 0,
self.e_inv_notify_partner_ids.ids
)]
})
_logger.info(
'Notifying partners %s about PEC server %s reset'
% (self.e_inv_notify_partner_ids.ids, self.name)
)
else:
_logger.error(
"Can't notify anyone about PEC server %s reset" % self.name)
3 changes: 3 additions & 0 deletions l10n_it_fatturapa_pec/views/fetchmail_view.xml
Expand Up @@ -9,6 +9,9 @@
<notebook position="inside">
<page string="Last error message" attrs="{'invisible': [('last_pec_error_message', '=', False)]}">
<field name="last_pec_error_message"/>
<group>
<field name="pec_error_count"/>
</group>
</page>
</notebook>
<xpath expr="//notebook/page[1]/group[1]/group[3]" position="attributes">
Expand Down

0 comments on commit 5e40567

Please sign in to comment.