Skip to content

Commit

Permalink
[#2636] stop using human-friendly names for tasks
Browse files Browse the repository at this point in the history
The Celery docs make clear that the name parameter of the
task decorator should be unique and that generally it functions
as an identifier the task. From this we can also infer that
names that change at runtime, due to gettext, are undesirable.
This in fact created issue with triggering certain periodic tasks
in the admin, due to a mismatch between the registered name (English)
and the translated name used at runtime (Dutch).

TLDR: let Celery generate the name, and directly translate the periodic
task names for Beat so that the entries in the Django periodic task
admin are human-readable. This is not ideal as we would like to use
(lazy) gettext here, but Celery doesn't allow that, so this strikes
the balance between human-readability and predictable task discovery.
  • Loading branch information
swrichards authored and joeribekker committed Aug 2, 2024
1 parent a7dd69d commit 7e15aee
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
46 changes: 39 additions & 7 deletions src/open_inwoner/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,24 +674,56 @@
#
CELERY_BROKER_URL = os.getenv("CELERY_BROKER_URL", "redis://localhost:6379/0")
CELERY_TASK_TIME_LIMIT = config("CELERY_TASK_HARD_TIME_LIMIT", default=15 * 60)
# https://docs.celeryq.dev/en/latest/userguide/periodic-tasks.html
CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers:DatabaseScheduler"
# https://docs.celeryq.dev/en/latest/userguide/periodic-tasks.html#beat-entries
CELERY_BEAT_SCHEDULE = {
"Import ZGW data": {
"task": "Import ZGW data",
# Note that the keys here will be used to give human-readable names
# to the periodic task entries, which will be visible to users in the
# admin interface. Unfortunately, we we cannot use gettext here (even
# in lazy mode): Django allows it, Celery does not. We could consider
# doing this registration in one of the Celery hooks, but until this
# becomes a painpoint, it's cleaner to have the schedule easily accessible
# here in the settings file.
"Importeer ZGW data": {
"task": "open_inwoner.openzaak.tasks.import_zgw_data",
"schedule": crontab(minute="0", hour="7", day_of_month="*"),
},
"Rebuild search index": {
"task": "Rebuild search index",
"Zoekindex opnieuw opbouwen": {
"task": "open_inwoner.search.tasks.rebuild_search_index",
"schedule": crontab(minute="0", hour="4", day_of_month="*"),
},
"Retry emails": {
"Probeer emails opnieuw te sturen": {
"task": "django_yubin.tasks.retry_emails",
"schedule": crontab(minute="1", hour="*", day_of_month="*"),
},
"Delete old emails": {
"Verwijder oude emails": {
"task": "django_yubin.tasks.delete_old_emails",
"schedule": crontab(minute="0", hour="6", day_of_month="*"),
},
"Send emails about expiring actions": {
"task": "open_inwoner.accounts.tasks.schedule_user_notifications",
"schedule": crontab(minute="15", hour="9", day_of_month="*"),
"kwargs": {
"notify_about": "actions",
"channel": "email",
},
},
"Send emails about expiring plans": {
"task": "open_inwoner.accounts.tasks.schedule_user_notifications",
"schedule": crontab(minute="5", hour="9", day_of_month="*"),
"kwargs": {
"notify_about": "plans",
"channel": "email",
},
},
"Send emails about messages": {
"task": "open_inwoner.accounts.tasks.schedule_user_notifications",
"schedule": crontab(minute="*/15", hour="*", day_of_month="*"),
"kwargs": {
"notify_about": "messages",
"channel": "email",
},
},
}

# Only ACK when the task has been executed. This prevents tasks from getting lost, with
Expand Down
3 changes: 1 addition & 2 deletions src/open_inwoner/openzaak/tasks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import logging

from django.core.management import call_command
from django.utils.translation import gettext as _

from open_inwoner.celery import app

logger = logging.getLogger(__name__)


@app.task(name=_("Import ZGW data"))
@app.task
def import_zgw_data():
logger.info("starting import_zgw_data() task")

Expand Down
3 changes: 1 addition & 2 deletions src/open_inwoner/search/tasks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import logging

from django.core.management import call_command
from django.utils.translation import gettext as _

from open_inwoner.celery import app

logger = logging.getLogger(__name__)


@app.task(name=_("Rebuild search index"))
@app.task
def rebuild_search_index():
logger.info("starting rebuild_search_index() task")

Expand Down

0 comments on commit 7e15aee

Please sign in to comment.