Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Elia Ponzio
cynomys
Commits
d2fe5711
Commit
d2fe5711
authored
Jun 25, 2021
by
Stephan Feurer
Browse files
Add email notification
parent
3fe7e80f
Changes
4
Hide whitespace changes
Inline
Side-by-side
cynomys/migrations/0001_initial.py
View file @
d2fe5711
# Generated by Django 3.2.4 on 2021-06-2
4 13:13
# Generated by Django 3.2.4 on 2021-06-2
5 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_2021062
4_1313
'
),
(
'django_celery_beat'
,
'0016_auto_2021062
5_0828
'
),
]
operations
=
[
migrations
.
CreateModel
(
name
=
'Delta
s
'
,
name
=
'Delta'
,
fields
=
[
(
'id'
,
models
.
BigAutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'timestamp'
,
models
.
DateTimeField
(
auto_now
=
Tru
e
)),
(
'timestamp'
,
models
.
DateTimeField
(
editable
=
Fals
e
)),
(
'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'
)),
],
),
...
...
cynomys/migrations/0002_deltas_return_value.py
deleted
100644 → 0
View file @
3fe7e80f
# 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
),
),
]
cynomys/migrations/0003_auto_20210624_1757.py
deleted
100644 → 0
View file @
3fe7e80f
# 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'
,
),
]
cynomys/tasks.py
View file @
d2fe5711
...
...
@@ -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: {}
\n
Success: {}
\n
Return Value: {}
\n
Timestamp: {}
\n
Latest 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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment