From 68509e96094c6f80fbdc986d2b669f750b052b5a Mon Sep 17 00:00:00 2001 From: Andreas <andreasv@fsmpi.rwth-aachen.de> Date: Mon, 15 Jan 2018 12:49:15 +0100 Subject: [PATCH] changed db wraper to always retry transactions for mysql --- db.py | 10 +++++++++- jobs.py | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/db.py b/db.py index 26eb921..105c8dc 100644 --- a/db.py +++ b/db.py @@ -67,7 +67,15 @@ elif config['DB_ENGINE'] == 'mysql': else: db = mysql.connector.connect(user=config['MYSQL_USER'], password=config['MYSQL_PASSWD'], unix_socket=config.get('MYSQL_UNIX', None)) cur = db.cursor() - cur.execute(operation) + tries = 0 + while (tries < 10): + try: + cur.execute(operation) + except mysql.connector.errors.InternalError as e: + if e.msg == 'Deadlock found when trying to get lock; try restarting transaction': + tries += 1 + else: + raise rows = [] try: rows = cur.fetchall() diff --git a/jobs.py b/jobs.py index 884574d..0696c73 100644 --- a/jobs.py +++ b/jobs.py @@ -155,10 +155,10 @@ def jobs_schedule(hostname): return 'no jobs', 503 modify('UPDATE jobs SET state="scheduled", worker = ?, time_scheduled = ? WHERE id = ?', hostname, datetime.now(), job['id']) query("COMMIT") - except OperationalError: + except: tries += 1 job = None - sleep(random.randinti(0,50)) + sleep(random.random()) if tries > 10: return 'no jobs', 503 -- GitLab