This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
service.py
103 lines (88 loc) · 3.42 KB
/
service.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
"""
Bagelbot script that is designed to run constantly and check to see if role call should be ran and then if a meeting should be generated.
"""
import logging
import sys
import time
from datetime import datetime
from pytz import timezone
from config import ATTENDANCE_TIME, FREQUENCY, MEETING_TIME, S3_BUCKET, TIMEZONE
from check_attendance import check_attendance
from generate_meeting import create_meetings
from utils import (
initialize,
download_shelve_from_s3,
update_everyone_from_slack,
upload_shelve_to_s3,
)
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(message)s")
DATE_FMT = "%m/%d/%Y"
def main():
"""
Initialize the shelf, possibly sync to s3, then check attendance, close
the shelf and maybe sync the shelf again.
Args:
args (ArgumentParser args): Parsed arguments that impact how the check_attandance runs
"""
if S3_BUCKET:
download_shelve_from_s3()
tz = timezone(TIMEZONE)
store, sc = initialize(update_everyone=True)
try:
while True:
# Get current time, and date of our last meeting
now = datetime.now(tz)
logging.info("It's now %s,", now.strftime(DATE_FMT))
last_meeting = store["history"][-1]
logging.info("and the last meeting was on %s.", last_meeting["date"].strftime(DATE_FMT))
# Determine if it's time to check attendance
attendance_time = all(
[
(now.date() - last_meeting["date"]) >= FREQUENCY,
now.hour == ATTENDANCE_TIME["hour"],
now.minute == ATTENDANCE_TIME["minute"],
now.weekday() == ATTENDANCE_TIME["weekday"],
]
)
logging.info("Is it attendance checking time? %s", attendance_time)
# Determine if it's time for a new meeting
meeting_time = all(
[
(now.date() - last_meeting["date"]) >= FREQUENCY,
now.hour == MEETING_TIME["hour"],
now.minute == MEETING_TIME["minute"],
now.weekday() == MEETING_TIME["weekday"],
]
)
logging.info("Is it meeting generating time? %s", meeting_time)
sync = False
if attendance_time:
logging.info("Gonna check that attendance!")
update_everyone_from_slack(store, sc)
check_attendance(store, sc)
sync = True
elif meeting_time:
logging.info("Let's try to generate a meeting!")
update_everyone_from_slack(store, sc)
max_attempts, attempt = 100, 1
success = False
while not success:
success = create_meetings(
store, sc, force_create=True, any_pair=attempt > max_attempts
)
attempt += 1
sync = True
if sync:
logging.info("Syncing to local storage.")
store.sync()
if S3_BUCKET:
logging.info("Uploading to s3.")
upload_shelve_to_s3()
# Go to sleep for a minute and check again
logging.info("Going to sleep for a minute.")
time.sleep(60)
finally:
store.close()
if __name__ == "__main__":
main()