Skip to content

Commit 1c4506d

Browse files
authored
Remove and migrate more celery tasks (#4742)
These have now been migrated to `lambda_tasks` and the periodic tasks set up. See #4408
1 parent 8261cf5 commit 1c4506d

12 files changed

Lines changed: 63 additions & 90 deletions

File tree

app/config/settings.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,18 +1229,6 @@ def get_private_ip():
12291229
"task": "grandchallenge.algorithms.tasks.update_associated_challenges",
12301230
"schedule": crontab(hour=3, minute=0),
12311231
},
1232-
"send_new_unread_direct_messages_emails": {
1233-
"task": "grandchallenge.direct_messages.tasks.send_new_unread_direct_messages_emails",
1234-
"schedule": crontab(hour=3, minute=30),
1235-
},
1236-
"send_unread_notification_emails": {
1237-
"task": "grandchallenge.notifications.tasks.send_unread_notification_emails",
1238-
"schedule": crontab(hour=4, minute=0),
1239-
},
1240-
"update_site_statistics": {
1241-
"task": "grandchallenge.statistics.tasks.update_site_statistics_cache",
1242-
"schedule": crontab(hour=5, minute=30),
1243-
},
12441232
"send_onboarding_task_reminder_emails": {
12451233
"task": "grandchallenge.challenges.tasks.send_onboarding_task_reminder_emails",
12461234
"schedule": crontab(day_of_week="mon", hour=6, minute=0),
@@ -1293,10 +1281,6 @@ def get_private_ip():
12931281
"task": "grandchallenge.emails.tasks.cleanup_sent_raw_emails",
12941282
"schedule": timedelta(hours=1),
12951283
},
1296-
"cleanup_celery_backend": {
1297-
"task": "grandchallenge.core.tasks.cleanup_celery_backend",
1298-
"schedule": timedelta(hours=1),
1299-
},
13001284
"logout_privileged_users": {
13011285
"task": "grandchallenge.browser_sessions.tasks.logout_privileged_users",
13021286
"schedule": timedelta(hours=1),

app/grandchallenge/cases/tasks.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -583,16 +583,6 @@ def handle_dicom_import_error(
583583
)
584584

585585

586-
@acks_late_micro_short_task(
587-
name=f"{__name__}.handle_health_imaging_import_job_event",
588-
retry_on=(LockNotAcquiredException, RetryStep),
589-
)
590-
@transaction.atomic
591-
def handle_health_imaging_import_job_event_celery(*, event):
592-
# TODO: 4408 Remove, this is still here to handle existing tasks on SQS
593-
return handle_health_imaging_import_job_event(event=event)
594-
595-
596586
@lambda_task(retry_on=(LockNotAcquiredException, RetryStep))
597587
def handle_health_imaging_import_job_event(*, event: dict):
598588
job_name = event["jobName"]

app/grandchallenge/codebuild/tasks.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,6 @@ def create_codebuild_build(*, pk):
3535
Build.objects.create(webhook_message=ghwm, algorithm_image=algorithm_image)
3636

3737

38-
@acks_late_micro_short_task(name=f"{__name__}.handle_completed_build_event")
39-
@transaction.atomic
40-
def handle_completed_build_event_celery(*, build_arn, build_status):
41-
# TODO: 4408 Remove, this is still here to handle existing tasks on SQS
42-
return handle_completed_build_event(
43-
build_arn=build_arn, build_status=build_status
44-
)
45-
46-
4738
@lambda_task
4839
def handle_completed_build_event(*, build_arn: str, build_status: str):
4940
from grandchallenge.codebuild.models import Build

app/grandchallenge/core/tasks.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
from datetime import timedelta
22

33
import boto3
4-
from billiard.exceptions import (
5-
SoftTimeLimitExceeded as CelerySoftTimeLimitExceeded,
6-
)
74
from django.conf import settings
85
from django.contrib.sites.models import Site
96
from django.db import transaction
@@ -25,8 +22,14 @@
2522
from grandchallenge.workstations.models import Session
2623

2724

28-
@acks_late_micro_short_task
25+
@acks_late_micro_short_task(name=f"{__name__}.cleanup_celery_backend")
2926
@transaction.atomic
27+
def cleanup_celery_backend_celery():
28+
# TODO: 4408 Remove, this is still here to handle existing tasks on SQS
29+
return cleanup_celery_backend()
30+
31+
32+
@lambda_task
3033
def cleanup_celery_backend():
3134
"""Cleanup the Celery backend."""
3235
TaskResult.objects.filter(date_created__lt=now() - timedelta(days=7)).only(
@@ -37,23 +40,6 @@ def cleanup_celery_backend():
3740
CLOUDWATCH_METRICS_LIMIT = 1000
3841

3942

40-
@acks_late_micro_short_task(
41-
name=f"{__name__}.put_cloudwatch_metrics",
42-
ignore_result=True,
43-
singleton=True,
44-
# No need to retry here as the periodic task call this again
45-
ignore_errors=(
46-
LockError,
47-
CelerySoftTimeLimitExceeded,
48-
SoftTimeLimitExceeded,
49-
),
50-
)
51-
@transaction.atomic
52-
def put_cloudwatch_metrics_celery():
53-
# TODO: 4408 Remove, this is still here to handle existing tasks on SQS
54-
return put_cloudwatch_metrics()
55-
56-
5743
@lambda_task(
5844
singleton=True,
5945
# No need to retry here as the periodic task call this again

app/grandchallenge/direct_messages/tasks.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.contrib.auth import get_user_model
22
from django.contrib.sites.models import Site
33
from django.db.models import Count, F, Q
4+
from lambda_tasks.decorators import lambda_task
45

56
from grandchallenge.core.celery import acks_late_micro_short_task
67
from grandchallenge.profiles.models import NotificationEmailOptions
@@ -47,7 +48,15 @@ def get_new_senders(*, user):
4748
return sorted(list(new_senders), key=lambda s: s.pk)
4849

4950

50-
@acks_late_micro_short_task
51+
@acks_late_micro_short_task(
52+
name=f"{__name__}.send_new_unread_direct_messages_emails"
53+
)
54+
def send_new_unread_direct_messages_emails_celery():
55+
# TODO: 4408 Remove, this is still here to handle existing tasks on SQS
56+
return send_new_unread_direct_messages_emails()
57+
58+
59+
@lambda_task
5160
def send_new_unread_direct_messages_emails():
5261
site = Site.objects.get_current()
5362

app/grandchallenge/discussion_forums/models.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from django.contrib.auth.models import AnonymousUser
66
from django.core.exceptions import ObjectDoesNotExist, ValidationError
77
from django.db import models
8-
from django.db.transaction import on_commit
98
from django_extensions.db.fields import AutoSlugField
109
from guardian.shortcuts import assign_perm, remove_perm
1110
from guardian.utils import get_anonymous_user
@@ -123,14 +122,10 @@ def save(self, *args, **kwargs):
123122
if adding:
124123
self.assign_permissions()
125124
self.last_post_on = self.created
126-
on_commit(
127-
create_forum_notifications.signature(
128-
kwargs={
129-
"object_pk": self.pk,
130-
"app_label": self._meta.app_label,
131-
"model_name": self._meta.object_name,
132-
}
133-
).apply_async
125+
create_forum_notifications.execute_on_commit(
126+
object_pk=self.pk,
127+
app_label=self._meta.app_label,
128+
model_name=self._meta.object_name,
134129
)
135130

136131
if self.has_changed("is_locked"):
@@ -264,14 +259,10 @@ def save(self, *args, **kwargs):
264259
self.assign_permissions()
265260
self.topic.mark_as_read(user=self.creator)
266261
if not self.is_alone:
267-
on_commit(
268-
create_forum_notifications.signature(
269-
kwargs={
270-
"object_pk": self.pk,
271-
"app_label": self._meta.app_label,
272-
"model_name": self._meta.object_name,
273-
}
274-
).apply_async
262+
create_forum_notifications.execute_on_commit(
263+
object_pk=self.pk,
264+
app_label=self._meta.app_label,
265+
model_name=self._meta.object_name,
275266
)
276267

277268
self.topic.last_post = self

app/grandchallenge/discussion_forums/tasks.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
from uuid import UUID
2+
13
from actstream.actions import follow
24
from celery.utils.log import get_task_logger
35
from django.apps import apps
46
from django.db import transaction
7+
from lambda_tasks.decorators import lambda_task
58

69
from grandchallenge.core.celery import acks_late_micro_short_task
710
from grandchallenge.core.exceptions import LockNotAcquiredException
@@ -13,9 +16,20 @@
1316
logger = get_task_logger(__name__)
1417

1518

16-
@acks_late_micro_short_task(retry_on=(LockNotAcquiredException,))
19+
@acks_late_micro_short_task(
20+
name=f"{__name__}.create_forum_notifications",
21+
retry_on=(LockNotAcquiredException,),
22+
)
1723
@transaction.atomic
18-
def create_forum_notifications(*, object_pk, app_label, model_name):
24+
def create_forum_notifications_celery(**kwargs):
25+
# TODO: 4408 Remove, this is still here to handle existing tasks on SQS
26+
return create_forum_notifications(**kwargs)
27+
28+
29+
@lambda_task(retry_on=(LockNotAcquiredException,))
30+
def create_forum_notifications(
31+
*, object_pk: str | UUID, app_label: str, model_name: str
32+
):
1933
from grandchallenge.discussion_forums.models import (
2034
ForumPost,
2135
ForumTopic,

app/grandchallenge/notifications/tasks.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.contrib.sites.models import Site
22
from django.db.models import Count, F, Q
3+
from lambda_tasks.decorators import lambda_task
34

45
from grandchallenge.core.celery import acks_late_micro_short_task
56
from grandchallenge.profiles.models import (
@@ -8,7 +9,13 @@
89
)
910

1011

11-
@acks_late_micro_short_task
12+
@acks_late_micro_short_task(name=f"{__name__}.send_unread_notification_emails")
13+
def send_unread_notification_emails_celery():
14+
# TODO: 4408 Remove, this is still here to handle existing tasks on SQS
15+
return send_unread_notification_emails()
16+
17+
18+
@lambda_task
1219
def send_unread_notification_emails():
1320
site = Site.objects.get_current()
1421

app/grandchallenge/statistics/tasks.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django.core.cache import cache
55
from django.db.models import Count, Sum
66
from django.utils.timezone import now
7+
from lambda_tasks.decorators import lambda_task
78

89
from grandchallenge.algorithms.models import (
910
Algorithm,
@@ -29,7 +30,13 @@
2930
from grandchallenge.workstations.models import Session, WorkstationImage
3031

3132

32-
@acks_late_micro_short_task
33+
@acks_late_micro_short_task(name=f"{__name__}.update_site_statistics_cache")
34+
def update_site_statistics_cache_celery():
35+
# TODO: 4408 Remove, this is still here to handle existing tasks on SQS
36+
return update_site_statistics_cache()
37+
38+
39+
@lambda_task
3340
def update_site_statistics_cache():
3441
public_challenges = Challenge.objects.filter(hidden=False)
3542

app/tests/challenges_tests/test_models.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ def test_participants_follow_forum(group):
9494
def test_non_posters_notified(
9595
group, settings, django_capture_on_commit_callbacks
9696
):
97-
settings.CELERY_TASK_ALWAYS_EAGER = True
98-
settings.CELERY_TASK_EAGER_PROPAGATES = True
97+
settings.LAMBDA_TASKS_EAGER = True
9998

10099
p = UserFactory()
101100
u = UserFactory()

0 commit comments

Comments
 (0)