Skip to content

Commit

Permalink
ANPL-1704 dont run completed tasks again
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljcollinsuk committed Sep 26, 2023
1 parent 28f79bc commit 5294699
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions controlpanel/api/tasks/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@


class BaseTaskHandler(CeleryTask):
# can be applied to project settings also
# these settings mean that messages are only removed from the queue (acknowledged)
# when returned. if an error occurs, they remain in the queue, and will be resent
# to the worker when the "visibility_timeout" has expired. "visibility_timeout" is
# a setting that is configured in SQS per queue. Currently set to 30secs
acks_late = True
acks_on_failure_or_timeout = False
task_obj = None

def complete(self):
task = Task.objects.filter(task_id=self.request.id).first()
if task:
task.completed = True
task.save()
if self.task_obj:
self.task_obj.completed = True
self.task_obj.save()

def get_task_obj(self):
return Task.objects.filter(task_id=self.request.id).first()

def run(self, *args, **kwargs):
self.task_obj = self.get_task_obj()
if self.task_obj and self.task_obj.completed:
return
self.handle(*args, **kwargs)

def handle(self, *args, **kwargs):
Expand All @@ -29,13 +42,6 @@ class BaseModelTaskHandler(BaseTaskHandler):
model = None
object = None
task_user_pk = None
# can be applied to project settings also
# these settings mean that messages are only removed from the queue (acknowledged)
# when returned. if an error occurs, they remain in the queue, and will be resent
# to the worker when the "visibility_timeout" has expired. "visibility_timeout" is
# a setting that is configured in SQS per queue. Currently set to 30secs
acks_late = True
acks_on_failure_or_timeout = False

def get_object(self, pk):
try:
Expand All @@ -53,6 +59,9 @@ def run(self, obj_pk, task_user_pk, *args, **kwargs):
to look up the user later if required. The `handle` method is then called
with any other args and kwargs sent.
"""
self.task_obj = self.get_task_obj()
if self.task_obj and self.task_obj.completed:
return
self.object = self.get_object(obj_pk)
self.task_user_pk = task_user_pk
self.handle(*args, **kwargs)

0 comments on commit 5294699

Please sign in to comment.