From c75ab4e7f0c09b0ad1c58ca4cbb56e677c67f074 Mon Sep 17 00:00:00 2001 From: Ilan Steemers Date: Wed, 20 Jan 2016 14:11:53 +0100 Subject: [PATCH] Only updates existing tasks if it was failing updates stopped, result and success only for existing task results if the original result was failing. Otherwise the result is discarded. --- django_q/cluster.py | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/django_q/cluster.py b/django_q/cluster.py index 68d226b..da7c35a 100644 --- a/django_q/cluster.py +++ b/django_q/cluster.py @@ -408,19 +408,28 @@ def save_task(task, broker): try: if task['success'] and 0 < Conf.SAVE_LIMIT <= Success.objects.count(): Success.objects.last().delete() - Task.objects.update_or_create(id=task['id'], - name=task['name'], - defaults={ - 'func': task['func'], - 'hook': task.get('hook'), - 'args': task['args'], - 'kwargs': task['kwargs'], - 'started': task['started'], - 'stopped': task['stopped'], - 'result': task['result'], - 'group': task.get('group'), - 'success': task['success']} - ) + # check if this task has previous results + if Task.objects.filter(id=task['id'], name=task['name']).exists(): + existing_task = 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.save() + else: + Task.objects.create(id=task['id'], + name=task['name'], + func=task['func'], + hook=task.get('hook'), + args=task['args'], + kwargs=task['kwargs'], + started=task['started'], + stopped=task['stopped'], + result=task['result'], + group=task.get('group'), + success=task['success'] + ) except Exception as e: logger.error(e)