Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Celery retry config #1049

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions common/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@
# this should be automactically configured via ^^ config_from_object
# but it isn't so here it's configured here
app.conf.task_routes = settings.CELERY_ROUTES

# this means the celery task only sends the acknowledgement (removes from task queue)
# after it has completed
# app.conf.task_ack_late = True # What's the impact on all of our tasks?
10 changes: 9 additions & 1 deletion notifications/tasks.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import logging

from celery import shared_task
from django.conf import settings
from django.db.transaction import atomic

logger = logging.getLogger(__name__)


@shared_task
@shared_task(
default_retry_delay=settings.NOTIFICATIONS_DEFAULT_RETRY_DELAY,
max_retries=settings.NOTIFICATIONS_MAX_RETRIES,
retry_backoff=True,
retry_backoff_max=settings.NOTIFICATIONS_RETRY_BACKOFF_MAX,
retry_jitter=True,
autoretry_for=(Exception,),
)
@atomic
def send_emails_task(notification_pk: int):
"""Task for emailing all users signed up to receive packaging updates and
Expand Down
9 changes: 8 additions & 1 deletion publishing/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
logger = logging.getLogger(__name__)


@app.task
@app.task(
default_retry_delay=settings.ENVELOPE_GENERATION_DEFAULT_RETRY_DELAY,
max_retries=settings.ENVELOPE_GENERATION_MAX_RETRIES,
retry_backoff=True,
retry_backoff_max=settings.ENVELOPE_GENERATION_RETRY_BACKOFF_MAX,
retry_jitter=True,
autoretry_for=(Exception,),
)
def create_xml_envelope_file(
packaged_work_basket_id: int,
notify_when_done: bool = True,
Expand Down
2 changes: 1 addition & 1 deletion publishing/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_create_and_upload_envelope(
create_xml_envelope_file.apply(
(packaged_work_basket.pk, True),
)
mock_save.assert_called_once()
mock_save.assert_called()

assert expected_bucket in s3_bucket_names()

Expand Down
44 changes: 41 additions & 3 deletions settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,16 +437,54 @@
# Settings about retrying uploads if the api cannot be contacted.
# Names correspond to celery settings for retrying tasks:
# https://docs.celeryq.dev/en/stable/userguide/tasks.html#automatic-retry-for-known-exceptions
CELERY_MAX_RETRIES = int(
os.environ.get("CELERY_MAX_RETRIES", "3"),
)
CELERY_RETRY_BACKOFF_MAX = int(
os.environ.get("CELERY_RETRY_BACKOFF_MAX", "600"),
)
CELERY_DEFAULT_RETRY_DELAY = int(
os.environ.get("CELERY_DEFAULT_RETRY_DELAY", "8"),
)

CROWN_DEPENDENCIES_API_MAX_RETRIES = int(
os.environ.get("CROWN_DEPENDENCIES_API_MAX_RETRIES", "3"),
os.environ.get("CROWN_DEPENDENCIES_API_MAX_RETRIES", CELERY_MAX_RETRIES),
)
CROWN_DEPENDENCIES_API_RETRY_BACKOFF_MAX = int(
os.environ.get("CROWN_DEPENDENCIES_API_RETRY_BACKOFF_MAX", "600"),
os.environ.get(
"CROWN_DEPENDENCIES_API_RETRY_BACKOFF_MAX",
CELERY_RETRY_BACKOFF_MAX,
),
)
CROWN_DEPENDENCIES_API_DEFAULT_RETRY_DELAY = int(
os.environ.get("CROWN_DEPENDENCIES_API_DEFAULT_RETRY_DELAY", "8"),
os.environ.get(
"CROWN_DEPENDENCIES_API_DEFAULT_RETRY_DELAY",
CELERY_DEFAULT_RETRY_DELAY,
),
)

ENVELOPE_GENERATION_MAX_RETRIES = int(
os.environ.get("ENVELOPE_GENERATION_MAX_RETRIES", CELERY_MAX_RETRIES),
)
ENVELOPE_GENERATION_RETRY_BACKOFF_MAX = int(
os.environ.get("ENVELOPE_GENERATION_RETRY_BACKOFF_MAX", CELERY_RETRY_BACKOFF_MAX),
)
ENVELOPE_GENERATION_DEFAULT_RETRY_DELAY = int(
os.environ.get(
"ENVELOPE_GENERATION_DEFAULT_RETRY_DELAY",
CELERY_DEFAULT_RETRY_DELAY,
),
)

NOTIFICATIONS_MAX_RETRIES = int(
os.environ.get("NOTIFICATIONS_MAX_RETRIES", CELERY_MAX_RETRIES),
)
NOTIFICATIONS_RETRY_BACKOFF_MAX = int(
os.environ.get("NOTIFICATIONS_RETRY_BACKOFF_MAX", CELERY_RETRY_BACKOFF_MAX),
)
NOTIFICATIONS_DEFAULT_RETRY_DELAY = int(
os.environ.get("NOTIFICATIONS_DEFAULT_RETRY_DELAY", CELERY_DEFAULT_RETRY_DELAY),
)

# SQLite AWS settings
SQLITE_STORAGE_BUCKET_NAME = os.environ.get("SQLITE_STORAGE_BUCKET_NAME", "sqlite")
Expand Down
Loading