-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcron.py
72 lines (64 loc) · 2.54 KB
/
cron.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from apis.models import Event, EventRegistration,User
import datetime
from django.core.mail import send_mail
from decouple import config
def notassignedtoabsent(self):
for event in Event.objects.all():
end_date=event.end_datetime
hour = int(end_date.hour)
day = int(end_date.day)
year = int(end_date.year)
month = int(end_date.month)
date = datetime.datetime(year,month,day,hour,0,0)
now = datetime.datetime.now()
for i in range(5):
enddateplus1 = date+datetime.timedelta(days=1)
enddateplus3 = date+datetime.timedelta(days=3)
if enddateplus1 < now and enddateplus3 > now :
for a in event.evre.all():
if a.attendance == 0:
print(a)
print('yes')
a.attendance = 2
a.save()
else:
print('NO')
from datetime import datetime
import os
import django
from django.conf import settings
from django.core.management import call_command
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Shelf_Backend.settings")
django.setup()
import subprocess # For executing a shell command
import requests
def backup():
filename = datetime.now().strftime("Backup on %d-%m-%Y.psql")
try:
call_command("dbbackup")
print( f"Backed up successfully: {datetime.now()}")
except:
print( f"Could not be backed up: {datetime.now()}")
def ping_database():
db_name = config('DB_NAME')
command = ['pg_isready', f'-d {db_name}']
if subprocess.call(command) == 0 :
pass
else:
subject = 'Database server is down!!!'
message = 'Hi , Database server for shelf is down.'
email_from = settings.EMAIL_FROM_ADDRESS
recipient_list = ['[email protected]', '[email protected]', '[email protected]', '[email protected]']
send_mail(subject, message, email_from, recipient_list)
call_command('crontab remove')
def ping_webserver():
try :
response = requests.get('http://127.0.0.1/api/ping/')
print(response.status_code)
except :
subject = 'Shelf Web server is down!!!'
message = 'Hi , Webserver for shelf is down.'
email_from = settings.EMAIL_FROM_ADDRESS
recipient_list = ['[email protected]', '[email protected]', '[email protected]', '[email protected]']
send_mail(subject, message, email_from, recipient_list)
call_command('crontab remove')