[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
This commit is contained in:
Martin Trigaux 2017-02-28 18:00:00 +01:00
parent 48cebafdaf
commit 69e91f6a7c
No known key found for this signature in database
GPG Key ID: 7B0E288E7C0F83A7
1 changed files with 12 additions and 14 deletions

View File

@ -197,20 +197,20 @@ class ir_cron(osv.osv):
""" """
db = openerp.sql_db.db_connect(db_name) db = openerp.sql_db.db_connect(db_name)
threading.current_thread().dbname = db_name threading.current_thread().dbname = db_name
cr = db.cursor()
jobs = [] jobs = []
try: try:
# Make sure the database we poll has the same version as the code of base with db.cursor() as cr:
cr.execute("SELECT 1 FROM ir_module_module WHERE name=%s AND latest_version=%s", ('base', BASE_VERSION)) # Make sure the database we poll has the same version as the code of base
if cr.fetchone(): cr.execute("SELECT 1 FROM ir_module_module WHERE name=%s AND latest_version=%s", ('base', BASE_VERSION))
# Careful to compare timestamps with 'UTC' - everything is UTC as of v6.1. if cr.fetchone():
cr.execute("""SELECT * FROM ir_cron # Careful to compare timestamps with 'UTC' - everything is UTC as of v6.1.
WHERE numbercall != 0 cr.execute("""SELECT * FROM ir_cron
AND active AND nextcall <= (now() at time zone 'UTC') WHERE numbercall != 0
ORDER BY priority""") AND active AND nextcall <= (now() at time zone 'UTC')
jobs = cr.dictfetchall() ORDER BY priority""")
else: jobs = cr.dictfetchall()
_logger.warning('Skipping database %s as its base version is not %s.', db_name, BASE_VERSION) else:
_logger.warning('Skipping database %s as its base version is not %s.', db_name, BASE_VERSION)
except psycopg2.ProgrammingError, e: except psycopg2.ProgrammingError, e:
if e.pgcode == '42P01': if e.pgcode == '42P01':
# Class 42 — Syntax Error or Access Rule Violation; 42P01: undefined_table # Class 42 — Syntax Error or Access Rule Violation; 42P01: undefined_table
@ -220,8 +220,6 @@ class ir_cron(osv.osv):
raise raise
except Exception: except Exception:
_logger.warning('Exception in cron:', exc_info=True) _logger.warning('Exception in cron:', exc_info=True)
finally:
cr.close()
for job in jobs: for job in jobs:
lock_cr = db.cursor() lock_cr = db.cursor()