From 4142ab579fbbbf2bc0b97cdbdc7d46b2e08a39c1 Mon Sep 17 00:00:00 2001 From: Jose Javier Merchante Date: Thu, 29 Aug 2024 12:12:11 +0200 Subject: [PATCH] Close DB connection before job completion RQ workers create a fork to run the function. The issue arises when, after completing the job, the MariaDB logs display the warning: `Aborted connection to db. Got an error reading communication packets`. This change ensures the database connection is closed before the fork ends, preventing the warning from appearing. Signed-off-by: Jose Javier Merchante --- .../connection-closed-when-job-is-executed.yml | 12 ++++++++++++ sortinghat/config/settings.py | 4 ++++ sortinghat/core/jobs.py | 18 +++++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 releases/unreleased/connection-closed-when-job-is-executed.yml diff --git a/releases/unreleased/connection-closed-when-job-is-executed.yml b/releases/unreleased/connection-closed-when-job-is-executed.yml new file mode 100644 index 00000000..25739aeb --- /dev/null +++ b/releases/unreleased/connection-closed-when-job-is-executed.yml @@ -0,0 +1,12 @@ +--- +title: Connection closed when job is executed +category: fixed +author: Jose Javier Merchante +issue: null +notes: > + RQ workers create a fork to run the jobs. The issue arises + when, after completing the job, the MariaDB logs display the + warning: `Aborted connection to db. Got an error reading + communication packets`. This change ensures the database + connection is closed before the fork ends, preventing the + warning from appearing. diff --git a/sortinghat/config/settings.py b/sortinghat/config/settings.py index 13028740..e7cf6fee 100644 --- a/sortinghat/config/settings.py +++ b/sortinghat/config/settings.py @@ -292,6 +292,10 @@ } } +RQ = { + 'JOB_CLASS': 'sortinghat.core.jobs.SortingHatJob' +} + # # SortingHat Multi-tenant # diff --git a/sortinghat/core/jobs.py b/sortinghat/core/jobs.py index dd029823..8b9d7df1 100644 --- a/sortinghat/core/jobs.py +++ b/sortinghat/core/jobs.py @@ -28,7 +28,8 @@ import django_rq.utils import rq import redis.exceptions -from django.db import IntegrityError, transaction +from django.db import IntegrityError, transaction, connection +from rq.job import Job from .db import find_individual_by_uuid, find_organization from .api import enroll, merge, update_profile, add_scheduled_task, delete_scheduled_task @@ -140,6 +141,21 @@ def job_in_tenant(job, tenant): return sorted_jobs +class SortingHatJob(Job): + """Custom RQ Job class for SortingHat jobs. + + This class closes the db connection before finishing + the job to avoid the 'aborted connection' error in + the database. + + See also https://github.com/rq/django-rq/issues/17 + """ + def perform(self): + result = super().perform() + connection.close() + return result + + @django_rq.job @job_using_tenant def recommend_affiliations(ctx, uuids=None, last_modified=MIN_PERIOD_DATE):