Skip to content

Commit 322c2b2

Browse files
authored
On commit cleanup (#4388)
Ensures that: - only celery tasks are used in `on_commit` - the function passed to `on_commit` is a real method and not a lambda - `apply_async` is never called directly See DIAGNijmegen/rse-grand-challenge-admin#663
1 parent f9f8d74 commit 322c2b2

24 files changed

Lines changed: 194 additions & 119 deletions

File tree

app/grandchallenge/algorithms/tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def execute_algorithm_job_for_inputs(*, job_pk):
6767
job.status = job.PENDING
6868
job.save()
6969

70-
on_commit(job.execute)
70+
job.execute()
7171

7272

7373
def create_algorithm_jobs(
@@ -175,7 +175,7 @@ def create_algorithm_jobs(
175175
job.utilization.challenge = job_utilization_challenge
176176
job.utilization.save()
177177

178-
on_commit(job.execute)
178+
job.execute()
179179

180180
jobs.append(job)
181181

app/grandchallenge/algorithms/views.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from django.core.exceptions import PermissionDenied, ValidationError
1616
from django.db.models import Q, Window
1717
from django.db.models.functions import Rank
18+
from django.db.transaction import on_commit
1819
from django.forms.utils import ErrorList
1920
from django.http import FileResponse, HttpResponse, HttpResponseRedirect
2021
from django.shortcuts import get_object_or_404
@@ -501,14 +502,16 @@ def form_valid(self, form):
501502
algorithm_image.import_status = ImportStatusChoices.QUEUED
502503
algorithm_image.save()
503504

504-
upload_to_registry_and_sagemaker.signature(
505-
kwargs={
506-
"app_label": algorithm_image._meta.app_label,
507-
"model_name": algorithm_image._meta.model_name,
508-
"pk": algorithm_image.pk,
509-
"mark_as_desired": True,
510-
}
511-
).apply_async()
505+
on_commit(
506+
upload_to_registry_and_sagemaker.signature(
507+
kwargs={
508+
"app_label": algorithm_image._meta.app_label,
509+
"model_name": algorithm_image._meta.model_name,
510+
"pk": algorithm_image.pk,
511+
"mark_as_desired": True,
512+
}
513+
).apply_async
514+
)
512515

513516
return response
514517

app/grandchallenge/challenges/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,13 +532,13 @@ def save(self, *args, **kwargs):
532532

533533
if adding or self.hidden != self._hidden_orig:
534534
on_commit(
535-
lambda: assign_evaluation_permissions.apply_async(
535+
assign_evaluation_permissions.signature(
536536
kwargs={
537537
"phase_pks": list(
538538
self.phase_set.values_list("id", flat=True)
539539
)
540540
}
541-
)
541+
).apply_async
542542
)
543543

544544
if self.has_changed("compute_cost_euro_millicents"):

app/grandchallenge/codebuild/tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ def handle_completed_build_event(*, build_arn, build_status):
5353

5454
if build.status == build.BuildStatusChoices.SUCCEEDED:
5555
on_commit(
56-
lambda: add_image_to_algorithm.apply_async(
56+
add_image_to_algorithm.signature(
5757
kwargs={"build_pk": str(build.pk)}
58-
)
58+
).apply_async
5959
)
6060

6161

app/grandchallenge/components/admin.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def requeue_jobs(modeladmin, request, queryset):
127127

128128
jobs.append(job)
129129

130-
on_commit(job.execute)
130+
job.execute()
131131

132132
queryset.model.objects.bulk_update(
133133
jobs,
@@ -170,4 +170,6 @@ def cancel_jobs(modeladmin, request, queryset):
170170
)
171171
def deprovision_jobs(modeladmin, request, queryset):
172172
for job in queryset:
173-
deprovision_job.signature(**job.signature_kwargs).apply_async()
173+
on_commit(
174+
deprovision_job.signature(**job.signature_kwargs).apply_async
175+
)

app/grandchallenge/components/management/commands/interface_to_object_store.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.core.management import BaseCommand
2+
from django.db.transaction import on_commit
23

34
from grandchallenge.components.models import (
45
ComponentInterface,
@@ -31,6 +32,10 @@ def handle(self, *args, **options):
3132
interface.save()
3233

3334
for pk in pks:
34-
civ_value_to_file.apply_async(kwargs={"civ_pk": pk})
35+
on_commit(
36+
civ_value_to_file.signature(
37+
kwargs={"civ_pk": pk}
38+
).apply_async
39+
)
3540

3641
self.stdout.write("Conversion tasks scheduled")

app/grandchallenge/components/models.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,9 +1791,9 @@ def update_status( # noqa:C901
17911791
self.save()
17921792

17931793
if self.status == self.SUCCESS:
1794-
on_commit(self.execute_task_on_success)
1794+
self.execute_task_on_success()
17951795
elif self.status in [self.FAILURE, self.CANCELLED]:
1796-
on_commit(self.execute_task_on_failure)
1796+
self.execute_task_on_failure()
17971797

17981798
@property
17991799
def executor_kwargs(self):
@@ -1842,17 +1842,21 @@ def signature_kwargs(self):
18421842
}
18431843

18441844
def execute(self):
1845-
return provision_job.signature(**self.signature_kwargs).apply_async()
1845+
on_commit(provision_job.signature(**self.signature_kwargs).apply_async)
18461846

18471847
def execute_task_on_success(self):
1848-
deprovision_job.signature(**self.signature_kwargs).apply_async()
1848+
on_commit(
1849+
deprovision_job.signature(**self.signature_kwargs).apply_async
1850+
)
18491851
if self.task_on_success:
1850-
signature(self.task_on_success).apply_async()
1852+
on_commit(signature(self.task_on_success).apply_async)
18511853

18521854
def execute_task_on_failure(self):
1853-
deprovision_job.signature(**self.signature_kwargs).apply_async()
1855+
on_commit(
1856+
deprovision_job.signature(**self.signature_kwargs).apply_async
1857+
)
18541858
if self.task_on_failure:
1855-
signature(self.task_on_failure).apply_async()
1859+
on_commit(signature(self.task_on_failure).apply_async)
18561860

18571861
@property
18581862
def animate(self):

app/grandchallenge/components/tasks.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,9 @@ def execute_job(
813813
if job.status == job.PROVISIONED:
814814
job.update_status(status=job.EXECUTING)
815815
else:
816-
deprovision_job.signature(**job.signature_kwargs).apply_async()
816+
on_commit(
817+
deprovision_job.signature(**job.signature_kwargs).apply_async
818+
)
817819
raise PriorStepFailed("Job is not set to be executed")
818820

819821
if not job.container.can_execute:

app/grandchallenge/evaluation/models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -770,9 +770,9 @@ def save(self, *args, skip_calculate_ranks=False, **kwargs):
770770

771771
if not skip_calculate_ranks:
772772
on_commit(
773-
lambda: calculate_ranks.apply_async(
773+
calculate_ranks.signature(
774774
kwargs={"phase_pk": self.pk}
775-
)
775+
).apply_async
776776
)
777777

778778
def clean(self):
@@ -1955,9 +1955,9 @@ def save(self, *args, **kwargs):
19551955
self.assign_permissions()
19561956

19571957
on_commit(
1958-
lambda: calculate_ranks.apply_async(
1958+
calculate_ranks.signature(
19591959
kwargs={"phase_pk": self.submission.phase.pk}
1960-
)
1960+
).apply_async
19611961
)
19621962

19631963
@property

app/grandchallenge/evaluation/tasks.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ def prepare_and_execute_evaluation(*, evaluation_pk):
149149
evaluation.status = Evaluation.PENDING
150150
evaluation.save()
151151
on_commit(
152-
lambda: create_algorithm_jobs_for_evaluation.apply_async(
152+
create_algorithm_jobs_for_evaluation.signature(
153153
kwargs={
154154
"evaluation_pk": evaluation_pk,
155155
"first_run": True,
156156
}
157-
)
157+
).apply_async
158158
)
159159
elif evaluation.submission.predictions_file:
160160
mimetype = get_file_mimetype(evaluation.submission.predictions_file)
@@ -191,7 +191,8 @@ def prepare_and_execute_evaluation(*, evaluation_pk):
191191
evaluation.inputs.add(civ)
192192
evaluation.status = Evaluation.PENDING
193193
evaluation.save()
194-
on_commit(evaluation.execute)
194+
195+
evaluation.execute()
195196
else:
196197
evaluation.update_status(
197198
status=Evaluation.FAILURE,
@@ -463,7 +464,7 @@ def set_evaluation_inputs(*, evaluation_pk):
463464
evaluation.status = evaluation.PENDING
464465
evaluation.save()
465466

466-
on_commit(evaluation.execute)
467+
evaluation.execute()
467468

468469

469470
def filter_by_creators_most_recent(*, evaluations):

0 commit comments

Comments
 (0)