From 0eada5149f4dc0ca0873ce0ddd75e539c5330747 Mon Sep 17 00:00:00 2001 From: Lorenzo Battistini Date: Fri, 1 Feb 2019 18:36:52 +0100 Subject: [PATCH] IMP l10n_it_fatturapa_pec avoiding to block fetchmail server in case of temporary errors. (#920) Retry N (configurable) times before resetting fetchmail server. Notify contacts about reset --- l10n_it_fatturapa_pec/__manifest__.py | 5 +- l10n_it_fatturapa_pec/data/fetchmail_data.xml | 9 +++ l10n_it_fatturapa_pec/i18n/it.po | 23 +++++++- .../i18n/l10n_it_fatturapa_pec.pot | 17 ++++++ l10n_it_fatturapa_pec/models/fetchmail.py | 56 ++++++++++++++++--- .../views/fetchmail_view.xml | 3 + 6 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 l10n_it_fatturapa_pec/data/fetchmail_data.xml diff --git a/l10n_it_fatturapa_pec/__manifest__.py b/l10n_it_fatturapa_pec/__manifest__.py index 59507ac87927..66a79a9762d1 100644 --- a/l10n_it_fatturapa_pec/__manifest__.py +++ b/l10n_it_fatturapa_pec/__manifest__.py @@ -7,7 +7,7 @@ { 'name': 'Italian Localization - Fattura elettronica - Supporto PEC', - 'version': '12.0.1.2.0', + 'version': '12.0.1.3.0', 'category': 'Localization/Italy', 'summary': 'Invio fatture elettroniche tramite PEC', 'author': 'Openforce Srls Unipersonale, Odoo Community Association (OCA)', @@ -25,7 +25,8 @@ 'views/fatturapa_attachment_out.xml', 'wizard/send_pec_view.xml', 'views/fetchmail_view.xml', - 'security/ir.model.access.csv' + 'security/ir.model.access.csv', + 'data/fetchmail_data.xml', ], 'installable': True } diff --git a/l10n_it_fatturapa_pec/data/fetchmail_data.xml b/l10n_it_fatturapa_pec/data/fetchmail_data.xml new file mode 100644 index 000000000000..e3bd0201e9b3 --- /dev/null +++ b/l10n_it_fatturapa_pec/data/fetchmail_data.xml @@ -0,0 +1,9 @@ + + + + + fetchmail.pec.max.retry + 5 + + + diff --git a/l10n_it_fatturapa_pec/i18n/it.po b/l10n_it_fatturapa_pec/i18n/it.po index 882e9a605560..4149e03fab2a 100644 --- a/l10n_it_fatturapa_pec/i18n/it.po +++ b/l10n_it_fatturapa_pec/i18n/it.po @@ -26,8 +26,16 @@ msgid "" "with user %s

" msgstr "" "

I messaggi PEC con Message-Id %s sono stati letti ma non elaborati, non " -"sono relativi a una e-fattura.

Controllare la casella PEC %s: server %" -"s, utente %s

" +"sono relativi a una e-fattura.

Controllare la casella PEC %s: server " +"%s, utente %s

" + +#. module: l10n_it_fatturapa_pec +#: code:addons/l10n_it_fatturapa_pec/models/fetchmail.py:166 +#, python-format +msgid "" +"

PEC server %s has been reset. Last error message is

%s

" +msgstr "" #. module: l10n_it_fatturapa_pec #: selection:account.invoice,fatturapa_state:0 @@ -112,6 +120,12 @@ msgstr "Thread email" msgid "Error" msgstr "Errore" +#. module: l10n_it_fatturapa_pec +#: code:addons/l10n_it_fatturapa_pec/models/fetchmail.py:163 +#, python-format +msgid "Fetchmail PEC server [%s] reset" +msgstr "" + #. module: l10n_it_fatturapa_pec #: model:ir.model.fields,field_description:l10n_it_fatturapa_pec.field_wizard_fatturapa_send_pec__id msgid "ID" @@ -170,6 +184,11 @@ msgstr "Nessun server PEC in ingresso trovato, configurarne uno." msgid "Not delivered" msgstr "Non consegnato" +#. module: l10n_it_fatturapa_pec +#: model:ir.model.fields,field_description:l10n_it_fatturapa_pec.field_fetchmail_server__pec_error_count +msgid "PEC error count" +msgstr "" + #. module: l10n_it_fatturapa_pec #: code:addons/l10n_it_fatturapa_pec/models/mail_thread.py:121 #, python-format diff --git a/l10n_it_fatturapa_pec/i18n/l10n_it_fatturapa_pec.pot b/l10n_it_fatturapa_pec/i18n/l10n_it_fatturapa_pec.pot index 22db66f2d115..f31fa9737813 100644 --- a/l10n_it_fatturapa_pec/i18n/l10n_it_fatturapa_pec.pot +++ b/l10n_it_fatturapa_pec/i18n/l10n_it_fatturapa_pec.pot @@ -19,6 +19,12 @@ msgstr "" msgid "

PEC message with Message-Id %s has been read but not processed, as not related to an e-invoice.

Please check PEC mailbox %s, at server %s, with user %s

" msgstr "" +#. module: l10n_it_fatturapa_pec +#: code:addons/l10n_it_fatturapa_pec/models/fetchmail.py:166 +#, python-format +msgid "

PEC server %s has been reset. Last error message is

%s

" +msgstr "" + #. module: l10n_it_fatturapa_pec #: selection:account.invoice,fatturapa_state:0 #: selection:fatturapa.attachment.out,state:0 @@ -102,6 +108,12 @@ msgstr "" msgid "Error" msgstr "" +#. module: l10n_it_fatturapa_pec +#: code:addons/l10n_it_fatturapa_pec/models/fetchmail.py:163 +#, python-format +msgid "Fetchmail PEC server [%s] reset" +msgstr "" + #. module: l10n_it_fatturapa_pec #: model:ir.model.fields,field_description:l10n_it_fatturapa_pec.field_wizard_fatturapa_send_pec__id msgid "ID" @@ -160,6 +172,11 @@ msgstr "" msgid "Not delivered" msgstr "" +#. module: l10n_it_fatturapa_pec +#: model:ir.model.fields,field_description:l10n_it_fatturapa_pec.field_fetchmail_server__pec_error_count +msgid "PEC error count" +msgstr "" + #. module: l10n_it_fatturapa_pec #: code:addons/l10n_it_fatturapa_pec/models/mail_thread.py:121 #, python-format diff --git a/l10n_it_fatturapa_pec/models/fetchmail.py b/l10n_it_fatturapa_pec/models/fetchmail.py index dcf4c8a51361..d45a4ea72a58 100644 --- a/l10n_it_fatturapa_pec/models/fetchmail.py +++ b/l10n_it_fatturapa_pec/models/fetchmail.py @@ -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 @@ -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", @@ -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() @@ -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 @@ -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() @@ -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 @@ -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': _( + "

" + "PEC server %s has been reset. Last error message is

" + "

%s

" + ) % (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) diff --git a/l10n_it_fatturapa_pec/views/fetchmail_view.xml b/l10n_it_fatturapa_pec/views/fetchmail_view.xml index 7381f203a1c1..abc98942651d 100644 --- a/l10n_it_fatturapa_pec/views/fetchmail_view.xml +++ b/l10n_it_fatturapa_pec/views/fetchmail_view.xml @@ -9,6 +9,9 @@ + + +