Skip to content

Commit

Permalink
👌 [open-zaak/open-zaak#1203] PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Apr 23, 2024
1 parent b959509 commit 01217ec
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 59 deletions.
57 changes: 1 addition & 56 deletions src/nrc/api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,14 @@
from django.utils.translation import gettext_lazy as _

import requests
from celery.exceptions import Ignore, Retry
from celery.utils.time import get_exponential_backoff_interval
from notifications_api_common.models import NotificationsConfig
from vine.utils import wraps
from notifications_api_common.autoretry import add_autoretry_behaviour

from nrc.celery import app
from nrc.datamodel.models import Abonnement, NotificatieResponse

logger = logging.getLogger(__name__)


def add_autoretry_behaviour(task, **options):
"""
Adapted from celery to use admin configurable autoretry settings
"""
autoretry_for = tuple(
options.get("autoretry_for", getattr(task, "autoretry_for", ()))
)
retry_kwargs = options.get("retry_kwargs", getattr(task, "retry_kwargs", {}))
retry_jitter = options.get("retry_jitter", getattr(task, "retry_jitter", True))

if autoretry_for and not hasattr(task, "_orig_run"):

@wraps(task.run)
def run(*args, **kwargs):
config = NotificationsConfig.get_solo()
max_retries = config.notification_delivery_max_retries
retry_backoff = config.notification_delivery_retry_backoff
retry_backoff_max = config.notification_delivery_retry_backoff_max

task.max_retries = max_retries

try:
return task._orig_run(*args, **kwargs)
except Ignore:
# If Ignore signal occurs task shouldn't be retried,
# even if it suits autoretry_for list
raise
except Retry:
raise
except autoretry_for as exc:
if retry_backoff:
retry_kwargs["countdown"] = get_exponential_backoff_interval(
factor=retry_backoff,
retries=task.request.retries,
maximum=retry_backoff_max,
full_jitter=retry_jitter,
)
# Override max_retries
if hasattr(task, "override_max_retries"):
retry_kwargs["max_retries"] = getattr(
task, "override_max_retries", max_retries
)

ret = task.retry(exc=exc, **retry_kwargs)
# Stop propagation
if hasattr(task, "override_max_retries"):
delattr(task, "override_max_retries")
raise ret

task._orig_run, task.run = task.run, run


class NotificationException(Exception):
pass

Expand Down
4 changes: 2 additions & 2 deletions src/nrc/api/tests/test_notificatie.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ def test_notificatie_send_empty_kenmerk_value(self, mock_task):
)


@patch("nrc.api.tasks.get_exponential_backoff_interval")
@patch("nrc.api.tasks.NotificationsConfig.get_solo")
@patch("notifications_api_common.autoretry.get_exponential_backoff_interval")
@patch("notifications_api_common.autoretry.NotificationsConfig.get_solo")
@patch("nrc.api.serializers.deliver_message.retry")
class NotificatieRetryTests(TestCase):
def test_notificatie_retry_use_global_config(
Expand Down
2 changes: 1 addition & 1 deletion src/nrc/tests/commands/test_setup_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_setup_configuration(self, m):
},
)

call_command("setup_configuration", stdout=stdout)
call_command("setup_configuration", stdout=stdout, no_color=True)

with self.subTest("Command output"):
command_output = stdout.getvalue().splitlines()
Expand Down

0 comments on commit 01217ec

Please sign in to comment.