forked from GithubBackups/healthchecks
sendalerts concurrently
This commit is contained in:
parent
c1840a92bd
commit
de203275c6
0
hc/api/management/__init__.py
Normal file
0
hc/api/management/__init__.py
Normal file
0
hc/api/management/commands/__init__.py
Normal file
0
hc/api/management/commands/__init__.py
Normal file
@ -2,12 +2,14 @@ import logging
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
|
from django.db import connection
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from hc.api.models import Check
|
from hc.api.models import Check
|
||||||
|
|
||||||
|
executor = ThreadPoolExecutor(max_workers=10)
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -16,26 +18,31 @@ def _stdout(message):
|
|||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
|
||||||
def handle_one():
|
def handle_many():
|
||||||
""" Send an alert for a single check.
|
""" Send alerts for many checks simultaneously. """
|
||||||
|
|
||||||
Return True if an appropriate check was selected and processed.
|
|
||||||
Return False if no checks need to be processed.
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
query = Check.objects.filter(user__isnull=False)
|
query = Check.objects.filter(user__isnull=False)
|
||||||
|
|
||||||
now = timezone.now()
|
now = timezone.now()
|
||||||
going_down = Q(alert_after__lt=now, status="up")
|
going_down = Q(alert_after__lt=now, status="up")
|
||||||
going_up = Q(alert_after__gt=now, status="down")
|
going_up = Q(alert_after__gt=now, status="down")
|
||||||
query = query.filter(going_down | going_up)
|
query = query.filter(going_down | going_up)
|
||||||
|
checks = list(query.iterator())
|
||||||
try:
|
if not checks:
|
||||||
check = query[0]
|
|
||||||
except IndexError:
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
for future in [executor.submit(handle_one, check) for check in checks]:
|
||||||
|
future.result()
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def handle_one(check):
|
||||||
|
""" Send an alert for a single check.
|
||||||
|
|
||||||
|
Return True if an appropriate check was selected and processed.
|
||||||
|
Return False if no checks need to be processed.
|
||||||
|
|
||||||
|
"""
|
||||||
check.status = check.get_status()
|
check.status = check.get_status()
|
||||||
|
|
||||||
tmpl = "\nSending alert, status=%s, code=%s\n"
|
tmpl = "\nSending alert, status=%s, code=%s\n"
|
||||||
@ -54,6 +61,7 @@ def handle_one():
|
|||||||
check.status = "paused"
|
check.status = "paused"
|
||||||
finally:
|
finally:
|
||||||
check.save()
|
check.save()
|
||||||
|
connection.close()
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -65,10 +73,10 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
ticks = 0
|
ticks = 0
|
||||||
while True:
|
while True:
|
||||||
success = True
|
if handle_many():
|
||||||
while success:
|
ticks = 0
|
||||||
success = handle_one()
|
else:
|
||||||
ticks = 0 if success else ticks + 1
|
ticks += 1
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
_stdout(".")
|
_stdout(".")
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
Django==1.8.2
|
|
||||||
django_compressor
|
|
||||||
django-appconf
|
django-appconf
|
||||||
django-ses-backend
|
django-ses-backend
|
||||||
psycopg2==2.6
|
Django==1.8.2
|
||||||
|
django_compressor
|
||||||
djmail
|
djmail
|
||||||
|
futures
|
||||||
premailer
|
premailer
|
||||||
|
psycopg2==2.6
|
||||||
pygments
|
pygments
|
||||||
requests
|
requests
|
||||||
|
Loading…
x
Reference in New Issue
Block a user