Skip to content

Commit

Permalink
[jobs] Delete tasks with no job due to Redis error
Browse files Browse the repository at this point in the history
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 <evamillan@bitergia.com>
  • Loading branch information
evamillan committed Aug 28, 2023
1 parent e35c071 commit 92da78b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
11 changes: 11 additions & 0 deletions releases/unreleased/delete-task-on-error.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Remove tasks that fail to be scheduled
category: fixed
author: Eva Millan <evamillan@bitergia.com>
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.
13 changes: 9 additions & 4 deletions sortinghat/core/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 92da78b

Please sign in to comment.