sendalerts management command prints error messages to stdout.

This commit is contained in:
Pēteris Caune 2016-01-31 19:48:21 +02:00
parent 156f4dbeea
commit c7a651c330
4 changed files with 14 additions and 5 deletions

View File

@ -47,7 +47,9 @@ class Command(BaseCommand):
tmpl = "\nSending alert, status=%s, code=%s\n"
self.stdout.write(tmpl % (check.status, check.code))
check.send_alert()
errors = check.send_alert()
for ch, error in errors:
self.stdout.write("ERROR: %s %s %s\n" % (ch.kind, ch.value, error))
connection.close()
return True

View File

@ -67,8 +67,13 @@ class Check(models.Model):
if self.status not in ("up", "down"):
raise NotImplementedError("Unexpected status: %s" % self.status)
errors = []
for channel in self.channel_set.all():
channel.notify(self)
error = channel.notify(self)
if error not in ("", "no-op"):
errors.append((channel, error))
return errors
def get_status(self):
if self.status in ("new", "paused"):
@ -153,6 +158,8 @@ class Channel(models.Model):
n.error = error
n.save()
return error
def test(self):
return self.transport().test()

View File

@ -44,7 +44,7 @@ class NotifyTestCase(BaseTestCase):
self.channel.notify(self.check)
n = Notification.objects.get()
self.assertEqual(n.error, "A connection to http://example failed")
self.assertEqual(n.error, "Connection failed")
@patch("hc.api.transports.requests.get")
def test_webhooks_ignore_up_events(self, mock_get):

View File

@ -73,7 +73,7 @@ class Webhook(Transport):
# Well, we tried
return "Connection timed out"
except requests.exceptions.ConnectionError:
return "A connection to %s failed" % self.channel.value
return "Connection failed"
class JsonTransport(Transport):
@ -87,7 +87,7 @@ class JsonTransport(Transport):
# Well, we tried
return "Connection timed out"
except requests.exceptions.ConnectionError:
return "A connection to %s failed" % url
return "Connection failed"
class Slack(JsonTransport):