From 9db745f171a2fed53d3c75f2f16f2b68062a2595 Mon Sep 17 00:00:00 2001 From: sbejaoui Date: Fri, 24 Jul 2020 10:39:21 +0200 Subject: [PATCH] [IMP] - keep connexion alive in case of job inactivity fixes: #246 --- queue_job/jobrunner/runner.py | 11 +++++++++++ queue_job/readme/CONTRIBUTORS.rst | 1 + 2 files changed, 12 insertions(+) diff --git a/queue_job/jobrunner/runner.py b/queue_job/jobrunner/runner.py index 8ecb7bc24c..8dff3c45f5 100644 --- a/queue_job/jobrunner/runner.py +++ b/queue_job/jobrunner/runner.py @@ -335,6 +335,11 @@ def select_jobs(self, where, args): cr.execute(query, args) yield cr + def keep_alive(self): + query = "SELECT 1" + with closing(self.conn.cursor()) as cr: + cr.execute(query) + def set_job_enqueued(self, uuid): with closing(self.conn.cursor()) as cr: cr.execute("UPDATE queue_job SET state=%s, " @@ -437,6 +442,12 @@ def run_jobs(self): def process_notifications(self): for db in self.db_by_name.values(): + if not db.conn.notifies: + # If there are no activity in the queue_job table it seems that + # tcp keepalives are not sent (in that very specific scenario), + # causing some intermediaries (such as haproxy) to close the + # connection, making the jobrunner to restart on a socket error + db.keep_alive() while db.conn.notifies: if self._stop: break diff --git a/queue_job/readme/CONTRIBUTORS.rst b/queue_job/readme/CONTRIBUTORS.rst index 63d1d390a4..b6720bc92e 100644 --- a/queue_job/readme/CONTRIBUTORS.rst +++ b/queue_job/readme/CONTRIBUTORS.rst @@ -6,3 +6,4 @@ * Laurent Mignon * Laetitia Gangloff * Cédric Pigeon +* Souheil Bejaoui