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

Retries for notifications #474

Closed
wants to merge 5 commits into from
Closed
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
Binary file modified promgen/locale/ja/LC_MESSAGES/django.mo
Binary file not shown.
4 changes: 4 additions & 0 deletions promgen/locale/ja/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ msgstr "Expire"
msgid "Test"
msgstr "テスト"

#: templates/promgen/sender_row.html:45
msgid "Testing"
msgstr "テスト中"

#: templates/promgen/notifier_block.html:19
msgid "Delete notification?"
msgstr "Notificiationを削除しますか?"
Expand Down
12 changes: 11 additions & 1 deletion promgen/notification/linenotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# These sources are released under the terms of the MIT license: see LICENSE

import logging
from http import HTTPStatus

from django import forms

Expand Down Expand Up @@ -45,4 +46,13 @@ def _send(self, token, data):

headers = {"Authorization": "Bearer %s" % token}

util.post(url, data=params, headers=headers).raise_for_status()
util.post_with_retry(
url,
data=params,
headers=headers,
retry_codes=(
HTTPStatus.GATEWAY_TIMEOUT,
HTTPStatus.TOO_MANY_REQUESTS,
HTTPStatus.SERVICE_UNAVAILABLE,
),
).raise_for_status()
12 changes: 11 additions & 1 deletion promgen/notification/slack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# These sources are released under the terms of the MIT license: see LICENSE

import logging
from http import HTTPStatus

from django import forms

Expand Down Expand Up @@ -55,4 +56,13 @@ def _send(self, url, data):
"text": message,
}

util.post(url, json=json, **kwargs).raise_for_status()
util.post_with_retry(
url,
json=json,
retry_codes=(
HTTPStatus.GATEWAY_TIMEOUT,
HTTPStatus.TOO_MANY_REQUESTS,
HTTPStatus.SERVICE_UNAVAILABLE,
),
**kwargs,
).raise_for_status()
11 changes: 10 additions & 1 deletion promgen/notification/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""

import logging
from http import HTTPStatus

from django import forms

Expand Down Expand Up @@ -36,4 +37,12 @@ class NotificationWebhook(NotificationBase):
form = FormWebhook

def _send(self, url, data):
util.post(url, json=data).raise_for_status()
util.post_with_retry(
url,
json=data,
retry_codes=(
HTTPStatus.GATEWAY_TIMEOUT,
HTTPStatus.TOO_MANY_REQUESTS,
HTTPStatus.SERVICE_UNAVAILABLE,
),
).raise_for_status()
7 changes: 6 additions & 1 deletion promgen/templates/promgen/sender_row.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@
<form method="post" action="{% url 'notifier-test' notifier.id %}" style="display: inline">
{% csrf_token %}
<input name="next" type="hidden" value="{{ request.get_full_path }}" />
<button class="btn btn-info btn-xs">{% trans "Test" %}</button>
<button
class="btn btn-info btn-xs"
onclick="this.disabled=true; this.innerHTML='{% trans "Testing" %}...';"
>
{% trans "Test" %}
</button>
</form>
<form method="post" action="{% url 'notifier-delete' notifier.id %}" onsubmit="return confirm('{% trans "Delete notification?" %}')" style="display: inline">
{% csrf_token %}
Expand Down
29 changes: 29 additions & 0 deletions promgen/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# These sources are released under the terms of the MIT license: see LICENSE

import argparse
import time
from http import HTTPStatus
from urllib.parse import urlsplit

import requests
Expand All @@ -27,6 +29,33 @@ def post(url, data=None, json=None, **kwargs):
return requests.post(url, data=data, json=json, **kwargs)


def post_with_retry(
url,
data=None,
json=None,
retries: int = 3,
retry_codes: tuple = (HTTPStatus.TOO_MANY_REQUESTS,),
wait_secs: int = 3,
**kwargs
):
"""POST request with retry capability.

Thin wrapper for our util::post function that implements a retry mechanism for certain status
codes.

:param retries: Number of retries
:param retry_codes: Response status codes that will cause a retry
:param wait_secs: Number of seconds to wait between each retry
"""
for n in range(retries):
resp = post(url, data=data, json=json, **kwargs)

if (resp.status_code in retry_codes) and (n < retries - 1):
time.sleep(wait_secs)
else:
return resp


def get(url, params=None, **kwargs):
headers = kwargs.setdefault("headers", {})
headers["User-Agent"] = USER_AGENT
Expand Down
Loading