From 69e91f6a7c5f10b9b4967d8ba5a29d5b4f3ecabb Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Tue, 28 Feb 2017 18:00:00 +0100 Subject: [PATCH] [FIX] base: recover thread from down postgresql If postgresql database is temporarly down, the cron thread may fail. The cursor creation fails when trying to connect to the server which leads to the cron thread to die (uncatched exception) and will not restart when postgres is back. Fixes #15666 --- openerp/addons/base/ir/ir_cron.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/openerp/addons/base/ir/ir_cron.py b/openerp/addons/base/ir/ir_cron.py index eaeecdbeb35..0131a3cae1c 100644 --- a/openerp/addons/base/ir/ir_cron.py +++ b/openerp/addons/base/ir/ir_cron.py @@ -197,20 +197,20 @@ class ir_cron(osv.osv): """ db = openerp.sql_db.db_connect(db_name) threading.current_thread().dbname = db_name - cr = db.cursor() jobs = [] try: - # Make sure the database we poll has the same version as the code of base - cr.execute("SELECT 1 FROM ir_module_module WHERE name=%s AND latest_version=%s", ('base', BASE_VERSION)) - if cr.fetchone(): - # Careful to compare timestamps with 'UTC' - everything is UTC as of v6.1. - cr.execute("""SELECT * FROM ir_cron - WHERE numbercall != 0 - AND active AND nextcall <= (now() at time zone 'UTC') - ORDER BY priority""") - jobs = cr.dictfetchall() - else: - _logger.warning('Skipping database %s as its base version is not %s.', db_name, BASE_VERSION) + with db.cursor() as cr: + # Make sure the database we poll has the same version as the code of base + cr.execute("SELECT 1 FROM ir_module_module WHERE name=%s AND latest_version=%s", ('base', BASE_VERSION)) + if cr.fetchone(): + # Careful to compare timestamps with 'UTC' - everything is UTC as of v6.1. + cr.execute("""SELECT * FROM ir_cron + WHERE numbercall != 0 + AND active AND nextcall <= (now() at time zone 'UTC') + ORDER BY priority""") + jobs = cr.dictfetchall() + else: + _logger.warning('Skipping database %s as its base version is not %s.', db_name, BASE_VERSION) except psycopg2.ProgrammingError, e: if e.pgcode == '42P01': # Class 42 — Syntax Error or Access Rule Violation; 42P01: undefined_table @@ -220,8 +220,6 @@ class ir_cron(osv.osv): raise except Exception: _logger.warning('Exception in cron:', exc_info=True) - finally: - cr.close() for job in jobs: lock_cr = db.cursor()