Browse Source

Webhooks have timeout

pull/7/head
Pēteris Caune 9 years ago
parent
commit
52c6beccef
2 changed files with 29 additions and 6 deletions
  1. +7
    -2
      hc/api/models.py
  2. +22
    -4
      hc/api/tests/test_notify.py

+ 7
- 2
hc/api/models.py View File

@ -118,8 +118,13 @@ class Channel(models.Model):
emails.alert(self.value, ctx) emails.alert(self.value, ctx)
n.save() n.save()
elif self.kind == "webhook" and check.status == "down": elif self.kind == "webhook" and check.status == "down":
r = requests.get(self.value)
n.status = r.status_code
try:
r = requests.get(self.value, timeout=5)
n.status = r.status_code
except requests.exceptions.Timeout:
# Well, we tried
pass
n.save() n.save()
elif self.kind == "pd": elif self.kind == "pd":
if check.status == "down": if check.status == "down":


+ 22
- 4
hc/api/tests/test_notify.py View File

@ -1,14 +1,15 @@
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.test import TestCase from django.test import TestCase
from mock import patch from mock import patch
from requests.exceptions import ReadTimeout
from hc.api.models import Channel, Check
from hc.api.models import Channel, Check, Notification
class NotifyTestCase(TestCase): class NotifyTestCase(TestCase):
@patch("hc.api.models.requests")
def test_webhook(self, mock_requests):
@patch("hc.api.models.requests.get")
def test_webhook(self, mock_get):
alice = User(username="alice") alice = User(username="alice")
alice.save() alice.save()
@ -21,4 +22,21 @@ class NotifyTestCase(TestCase):
channel.checks.add(check) channel.checks.add(check)
channel.notify(check) channel.notify(check)
mock_requests.get.assert_called_with(u"http://example")
mock_get.assert_called_with(u"http://example", timeout=5)
@patch("hc.api.models.requests.get", side_effect=ReadTimeout)
def test_it_handles_requests_exceptions(self, mock_get):
alice = User(username="alice")
alice.save()
check = Check()
check.status = "down"
check.save()
channel = Channel(user=alice, kind="webhook", value="http://example")
channel.save()
channel.checks.add(check)
channel.notify(check)
assert Notification.objects.count() == 1

Loading…
Cancel
Save