Skip to content

Commit

Permalink
--no-threads flag for sendalerts command
Browse files Browse the repository at this point in the history
  • Loading branch information
cuu508 committed Mar 28, 2017
1 parent c073cb0 commit 9dcb167
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
21 changes: 17 additions & 4 deletions hc/api/management/commands/sendalerts.py
Expand Up @@ -35,7 +35,15 @@ def add_arguments(self, parser):
help='Do not keep running indefinitely in a 2 second wait loop',
)

def handle_one(self):
parser.add_argument(
'--no-threads',
action='store_false',
dest='use_threads',
default=False,
help='Send alerts synchronously, without using threads',
)

def handle_one(self, use_threads=True):
""" Process a single check. """

now = timezone.now()
Expand Down Expand Up @@ -65,15 +73,20 @@ def handle_one(self):
if num_updated == 1:
# Send notifications only if status update succeeded
# (no other sendalerts process got there first)
notify_on_thread(check.id, self.stdout)
if use_threads:
notify_on_thread(check.id, self.stdout)
else:
notify(check.id, self.stdout)

return True

return False

def handle(self, *args, **options):
use_threads = options["use_threads"]
if not options["loop"]:
x = 0
while self.handle_one():
while self.handle_one(use_threads):
# returns True when there are more alerts to send.
x += 1
return "Sent %d alert(s)" % x
Expand All @@ -83,7 +96,7 @@ def handle(self, *args, **options):
ticks = 0
while True:

while self.handle_one():
while self.handle_one(use_threads):
ticks = 0

ticks += 1
Expand Down
19 changes: 16 additions & 3 deletions hc/api/tests/test_sendalerts.py
@@ -1,6 +1,7 @@
from datetime import timedelta
from mock import patch

from django.core.management import call_command
from django.utils import timezone
from hc.api.management.commands.sendalerts import Command
from hc.api.models import Check
Expand Down Expand Up @@ -35,7 +36,7 @@ def test_it_notifies_when_check_goes_down(self, mock_notify):
check.refresh_from_db()
self.assertEqual(check.status, "down")

# It should call `notify`
# It should call `notify_on_thread`
self.assertTrue(mock_notify.called)

@patch("hc.api.management.commands.sendalerts.notify_on_thread")
Expand All @@ -54,7 +55,7 @@ def test_it_notifies_when_check_goes_up(self, mock_notify):
check.refresh_from_db()
self.assertEqual(check.status, "up")

# It should call `notify`
# It should call `notify_on_thread`
self.assertTrue(mock_notify.called)

# alert_after now should be set
Expand All @@ -78,5 +79,17 @@ def test_it_updates_alert_after(self, mock_notify):
# alert_after should have been increased
self.assertTrue(check.alert_after > check.last_ping)

# notify should *not* have been called
# notify_on_thread should *not* have been called
self.assertFalse(mock_notify.called)

@patch("hc.api.management.commands.sendalerts.notify")
def test_it_works_synchronously(self, mock_notify):
check = Check(user=self.alice, status="up")
check.last_ping = timezone.now() - timedelta(days=2)
check.alert_after = check.get_alert_after()
check.save()

call_command("sendalerts", loop=False, use_threads=False)

# It should call `notify` instead of `notify_on_thread`
self.assertTrue(mock_notify.called)

0 comments on commit 9dcb167

Please sign in to comment.