From 7900a201c15114272e497b3cee7e68fcca91ac9b Mon Sep 17 00:00:00 2001 From: Ronald Ip Date: Thu, 9 Mar 2017 19:30:31 +0800 Subject: [PATCH] Add --no-loop option to sendalerts management command. Allow `sendalerts` to be run by an external scheduler, on a one-shot basis, exiting after all alerts have been sent. --- hc/api/management/commands/sendalerts.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/hc/api/management/commands/sendalerts.py b/hc/api/management/commands/sendalerts.py index a72ea43e..2577568d 100644 --- a/hc/api/management/commands/sendalerts.py +++ b/hc/api/management/commands/sendalerts.py @@ -26,6 +26,15 @@ class Command(BaseCommand): help = 'Sends UP/DOWN email alerts' owned = Check.objects.filter(user__isnull=False) + def add_arguments(self, parser): + parser.add_argument( + '--no-loop', + action='store_false', + dest='loop', + default=True, + help='Do not keep running indefinitely in a 2 second wait loop', + ) + def handle_one(self): """ Process a single check. """ @@ -62,6 +71,13 @@ class Command(BaseCommand): return False def handle(self, *args, **options): + if not options["loop"]: + x = 0 + while self.handle_one(): + # returns True when there are more alerts to send. + x += 1 + return "Sent %d alert(s)" % x + self.stdout.write("sendalerts is now running") ticks = 0