-
Notifications
You must be signed in to change notification settings - Fork 5
/
jobs.py
69 lines (63 loc) · 2.54 KB
/
jobs.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
import datetime
import io
import subprocess
from database import database
from objects import Admin
from tokens import BACKUP_CHANNEL
def half_hourly(context):
now = datetime.datetime.now()
# check if an admin in timeout exists
if context.job.context["admins"]:
if context.job.context["date"] != now.strftime("%d.%m"):
rotation = True
context.job.context["date"] = now.strftime("%d.%m")
else:
rotation = False
# key is the user_id
for key in list(context.job.context["admins"].keys()):
# create admin object for better access
admin = Admin(**context.job.context["admins"][key])
# if we passed 00, we need to set rotation to false cause we rotated
if rotation:
if admin.rotation:
admin.rotation = False
# if we still have rotation, its not our time yet, so we pass on the admin
if admin.rotation:
continue
dt1 = datetime.datetime.strptime(admin.until, "%H:%M")
td = datetime.timedelta(
hours=dt1.hour - now.hour, minutes=dt1.minute - now.minute
)
# if the total seconds are more then 0, its not our time yet
if int(td.total_seconds()) > 0:
continue
database.end_timeout(key, admin.groups)
del context.job.context["admins"][key]
day = now.strftime("%a").lower()
timeoff_admins = database.get_timeoff(day, float(now.strftime("%H.%M")))
for admin in timeoff_admins:
dt1 = datetime.datetime.strptime(admin.days[day]["until"], "%H:%M")
td = datetime.timedelta(
hours=dt1.hour - now.hour, minutes=dt1.minute - now.minute
)
if td.total_seconds() < 0:
rotation = True
else:
rotation = False
groups = {}
for group_id in admin.days[day]["groups"]:
groups[group_id] = admin.groups[group_id]
context_dict = {
"rotation": rotation,
"until": admin.days[day]["until"],
"groups": groups,
}
context.job.context["admins"][admin.id] = context_dict
database.start_timeout(groups.keys(), admin.id)
def backup_job(context):
run = subprocess.run(
["mongodump", "-dreportbot", "--gzip", "--archive"], capture_output=True
)
output = io.BytesIO(run.stdout)
time = datetime.datetime.now().strftime("%d-%m-%Y")
context.bot.send_document(BACKUP_CHANNEL, output, filename=f"{time}.archive.gz")