Skip to content

Commit

Permalink
Fix max attempts for value 1 (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
GDay committed May 6, 2024
1 parent 5e4aa65 commit 1d1d0a9
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions django_q/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,19 @@ def save_task(task, broker: Broker):

# check if this task has previous results
try:
existing_task = Task.objects.get(id=task["id"], name=task["name"])
task_obj = Task.objects.get(id=task["id"], name=task["name"])
# only update the result if it hasn't succeeded yet
if not existing_task.success:
existing_task.stopped = task["stopped"]
existing_task.result = task["result"]
existing_task.success = task["success"]
existing_task.attempt_count = existing_task.attempt_count + 1
existing_task.save()

if (
Conf.MAX_ATTEMPTS > 0
and existing_task.attempt_count >= Conf.MAX_ATTEMPTS
):
broker.acknowledge(task["ack_id"])
if not task_obj.success:
task_obj.stopped = task["stopped"]
task_obj.result = task["result"]
task_obj.success = task["success"]
task_obj.attempt_count = task_obj.attempt_count + 1
task_obj.save()

except Task.DoesNotExist:
# convert func to string
func = get_func_repr(task["func"])
Task.objects.create(
task_obj = Task.objects.create(
id=task["id"],
name=task["name"],
func=func,
Expand All @@ -150,6 +144,13 @@ def save_task(task, broker: Broker):
success=task["success"],
attempt_count=1,
)

if (
Conf.MAX_ATTEMPTS > 0
and task_obj.attempt_count >= Conf.MAX_ATTEMPTS
):
broker.acknowledge(task["ack_id"])

except Exception:
logger.exception("Could not save task result")

Expand Down

0 comments on commit 1d1d0a9

Please sign in to comment.