Commit d2fe5711 authored by Stephan Feurer's avatar Stephan Feurer
Browse files

Add email notification

parent 3fe7e80f
# Generated by Django 3.2.4 on 2021-06-24 13:13
# Generated by Django 3.2.4 on 2021-06-25 08:38
from django.db import migrations, models
import django.db.models.deletion
......@@ -9,17 +9,18 @@ class Migration(migrations.Migration):
initial = True
dependencies = [
('django_celery_beat', '0016_auto_20210624_1313'),
('django_celery_beat', '0016_auto_20210625_0828'),
]
operations = [
migrations.CreateModel(
name='Deltas',
name='Delta',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('timestamp', models.DateTimeField(auto_now=True)),
('timestamp', models.DateTimeField(editable=False)),
('success', models.BooleanField(blank=True, null=True)),
('latest_check', models.DateTimeField(auto_now=True)),
('latest_check', models.DateTimeField()),
('return_value', models.CharField(blank=True, max_length=100)),
('task', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='django_celery_beat.periodictask')),
],
),
......
# Generated by Django 3.2.4 on 2021-06-24 13:23
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('cynomys', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='deltas',
name='return_value',
field=models.CharField(blank=True, max_length=100),
),
]
# Generated by Django 3.2.4 on 2021-06-24 17:57
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('django_celery_beat', '0016_auto_20210624_1313'),
('cynomys', '0002_deltas_return_value'),
]
operations = [
migrations.CreateModel(
name='Delta',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('timestamp', models.DateTimeField(editable=False)),
('success', models.BooleanField(blank=True, null=True)),
('latest_check', models.DateTimeField()),
('return_value', models.CharField(blank=True, max_length=100)),
('task', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='django_celery_beat.periodictask')),
],
),
migrations.DeleteModel(
name='Deltas',
),
]
......@@ -2,10 +2,13 @@ from celery import shared_task
import subprocess
from django.utils import timezone
import requests
import logging
from django_celery_beat.models import PeriodicTask
from django.contrib.auth.models import User
from cynomys.models import Delta
from django.core.mail import send_mail
logger = logging.getLogger()
@shared_task
......@@ -55,6 +58,42 @@ def check_http(host, timeout, httpcode, **kwargs):
#@shared_task
# def l4_check():
# pass
@shared_task
def notify_email(delta_id, attempts):
attempts -= 1
receivers = []
for user in User.objects.all():
receivers.append(user.email)
delta = Delta.objects.filter(id=delta_id).last()
task_status = "up" if delta.success else "down"
logger.debug("sending email; task: {}, to: {}".format(delta.task, receivers))
num = send_mail(
"[cynomys] New Status: {}, Task: {}".format(task_status, delta.task),
"Task: {}\nSuccess: {}\nReturn Value: {}\nTimestamp: {}\nLatest Check: {}".format(
delta.task,
delta.success,
delta.return_value,
delta.timestamp.strftime("%d.%m.%Y, %H:%M:%S"),
delta.latest_check.strftime("%d.%m.%Y, %H:%M:%S")
),
None,
receivers,
fail_silently=False
)
# Email was sent, exit function
if num > 0:
logger.info("sent {} email to {}".format(num, receivers))
return
# Email was not sent, retry
logger.error("failed to send email to {}, attempts left: {}".format(receivers, attempts))
if attempts > 0:
notify_email(receivers, delta_id, attempts)
def check_delta(responsebool, return_value, parent_task):
"""Gets data from a check and compares it to the current state in the database. If a delta occurs, sends notifications"""
......@@ -75,6 +114,7 @@ def check_delta(responsebool, return_value, parent_task):
new_delta.save()
print("Status Changed for " + parent_task.name + " to a Success! Sending Notification")
#TODO put notification task
notify_email.delay(new_delta.pk, 3)
else:
delta = Delta.objects.filter(task=parent_task).last()
......@@ -93,3 +133,4 @@ def check_delta(responsebool, return_value, parent_task):
new_delta.save()
print("Status Changed for " + parent_task.name + " to a Failure! Sending Notification")
#TODO put notification task
notify_email.delay(new_delta.pk, 3)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment