sendalerts reuses database connection.

This commit is contained in:
Pēteris Caune 2017-05-07 15:31:10 +03:00
parent 810089d183
commit 09e5129bbe

View File

@ -2,7 +2,6 @@ import time
from threading import Thread from threading import Thread
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.db import connection
from django.utils import timezone from django.utils import timezone
from hc.api.models import Check from hc.api.models import Check
@ -10,7 +9,7 @@ from hc.api.models import Check
def notify(check_id, stdout): def notify(check_id, stdout):
check = Check.objects.get(id=check_id) check = Check.objects.get(id=check_id)
tmpl = "\nSending alert, status=%s, code=%s\n" tmpl = "Sending alert, status=%s, code=%s\n"
stdout.write(tmpl % (check.status, check.code)) stdout.write(tmpl % (check.status, check.code))
errors = check.send_alert() errors = check.send_alert()
for ch, error in errors: for ch, error in errors:
@ -82,27 +81,21 @@ class Command(BaseCommand):
return False return False
def handle(self, *args, **options): def handle(self, use_threads=True, loop=True, *args, **options):
use_threads = options["use_threads"] self.stdout.write("sendalerts is now running\n")
if not options["loop"]:
x = 0
while self.handle_one(use_threads):
# returns True when there are more alerts to send.
x += 1
return "Sent %d alert(s)" % x
self.stdout.write("sendalerts is now running") i, sent = 0, 0
ticks = 0
while True: while True:
while self.handle_one(use_threads): while self.handle_one(use_threads):
ticks = 0 sent += 1
if not loop:
break
ticks += 1
time.sleep(2) time.sleep(2)
if ticks % 60 == 0: i += 1
formatted = timezone.now().isoformat() if i % 60 == 0:
self.stdout.write("-- MARK %s --" % formatted) timestamp = timezone.now().isoformat()
self.stdout.write("-- MARK %s --\n" % timestamp)
connection.close() return "Sent %d alert(s)" % sent