From 92da78b6f8c42a0245a81d92d8eb1eeb0efc2412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eva=20Mill=C3=A1n?= Date: Mon, 28 Aug 2023 12:20:05 +0200 Subject: [PATCH] [jobs] Delete tasks with no job due to Redis error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removes the newly created ScheduledTask from the database if the job could not be enqueued due to a Redis connection error. Signed-off-by: Eva Millán --- releases/unreleased/delete-task-on-error.yml | 11 +++++++++++ sortinghat/core/jobs.py | 13 +++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 releases/unreleased/delete-task-on-error.yml diff --git a/releases/unreleased/delete-task-on-error.yml b/releases/unreleased/delete-task-on-error.yml new file mode 100644 index 00000000..0c3213bc --- /dev/null +++ b/releases/unreleased/delete-task-on-error.yml @@ -0,0 +1,11 @@ +--- +title: Remove tasks that fail to be scheduled +category: fixed +author: Eva Millan +issue: null +notes: > + When there was an issue with the Redis connection when + a task was created, the task was added to the database + but there was not scheduled job linked to it. Tasks are + now removed from the database and an error is raised in + this case. diff --git a/sortinghat/core/jobs.py b/sortinghat/core/jobs.py index c8791bae..cd87b452 100644 --- a/sortinghat/core/jobs.py +++ b/sortinghat/core/jobs.py @@ -28,9 +28,10 @@ import django_rq.utils import pandas import rq +import redis.exceptions -from .db import find_individual_by_uuid, find_organization, add_scheduled_task -from .api import enroll, merge, update_profile +from .db import find_individual_by_uuid, find_organization +from .api import enroll, merge, update_profile, add_scheduled_task, delete_scheduled_task from .context import SortingHatContext from .decorators import job_using_tenant, job_callback_using_tenant from .errors import BaseError, NotFoundError, EqualIndividualError, InvalidValueError @@ -774,12 +775,16 @@ def create_scheduled_task(ctx, job, interval, params): trxl = TransactionsLog.open('create_scheduled_task', ctx) - task = add_scheduled_task(trxl, job, interval, params) + task = add_scheduled_task(ctx, job, interval, params) if not params: params = dict() - schedule_task(ctx, job_fn, task, **params) + try: + schedule_task(ctx, job_fn, task, **params) + except redis.exceptions.ConnectionError as e: + delete_scheduled_task(ctx, task.id) + raise(e) trxl.close()