Browse Source

sendalerts reuses database connection.

pull/114/merge
Pēteris Caune 8 years ago
parent
commit
09e5129bbe
1 changed files with 13 additions and 20 deletions
  1. +13
    -20
      hc/api/management/commands/sendalerts.py

+ 13
- 20
hc/api/management/commands/sendalerts.py 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):
use_threads = options["use_threads"]
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")
def handle(self, use_threads=True, loop=True, *args, **options):
self.stdout.write("sendalerts is now running\n")
ticks = 0
i, sent = 0, 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:
formatted = timezone.now().isoformat()
self.stdout.write("-- MARK %s --" % formatted)
i += 1
if i % 60 == 0:
timestamp = timezone.now().isoformat()
self.stdout.write("-- MARK %s --\n" % timestamp)
connection.close()
return "Sent %d alert(s)" % sent

Loading…
Cancel
Save