-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheduler.py
122 lines (87 loc) · 3.47 KB
/
scheduler.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/user/bin/env python3.6
"""
Contains regular jobs like updating the DB
"""
from time import sleep
import datetime as dt
from apscheduler.schedulers.blocking import BlockingScheduler
import pytz
from unwetter import db, slack, wina, sentry
from unwetter.config import filter_event
sentry.init()
sched = BlockingScheduler(timezone=pytz.UTC)
@sched.scheduled_job("interval", minutes=1)
def update_db():
"""
You should not do actual work in the scheduler.
For now, we do it anyways.
"""
print("Running update job")
new_events = db.update()
if new_events is None:
return
# Filter new_events by SEVERITY_FILTER, URGENCY_FILTER and STATES_FILTER
filtered = []
for event in new_events:
if event["msg_type"] == "Cancel" and any(
item["published"] for item in event["has_changes"]
):
filtered.append(event)
elif not filter_event(event):
continue
elif event["msg_type"] == "Alert":
filtered.append(event)
elif any(
item["changed"] and item["published"] for item in event["has_changes"]
):
filtered.append(event)
elif event["special_type"] == "UpdateAlert":
filtered.append(event)
elif not any(
item["changed"] and item["published"] for item in event["has_changes"]
):
continue
else:
sentry.sentry_sdk.capture_message(f'Event was not filtered: {event["id"]}')
if filtered:
db.publish([event["id"] for event in filtered])
wina.upload_ids([event["id"] for event in filtered])
db.set_breaking_memo(True)
for event in filtered:
print(f'Sending event {event["id"]} to Slack')
slack.post_event(event)
sleep(1)
# @sched.scheduled_job("interval", minutes=5)
def post_clear_warning():
currently_events = db.current_events(all_severities=False)
if currently_events:
db.set_warn_events_memo(True)
print("Active events: warn events memo ON")
elif db.warn_events_memo() and not currently_events:
db.set_warn_events_memo(False)
db.set_breaking_memo(False)
text = """
AKTUALISIERUNG:
Der Deutsche Wetterdienst gibt für NRW zurzeit keine Warnungen der Kategorie 3 (rot) und 4 (violett) mehr aus -
also vor Unwetter oder extremem Unwetter. Damit besteht keine Warnpflicht mehr. Es kann allerdings nach wie vor
markante Wetterlagen geben - alle Informationen dazu auf einen Blick hier:
UWA-Karten: www.wdr.de/k/unwetterkarte
(Keine roten und violetten Unwettergebiete)
https://www.dwd.de/DE/wetter/warnungen/warnWetter_node.html
(Vgl. NRW auf Website des Deutschen Wetterdienstes)
""".strip()
keywords = "Unwetter, UWA, Keine Warnpflicht"
title_wina = "Amtliche Unwetterwarnung des DWD (UWA) - Warnpflicht aufgehoben"
wina.upload_text(title_wina, text, keywords)
title_slack = "Warnpflicht für NRW aufgehoben"
slack.post_text(title_slack, text)
print("No active events: warn_events_memo OFF")
@sched.scheduled_job("cron", hour=1)
def clean_old_events():
cutoff = dt.datetime.now() - dt.timedelta(days=30)
print("Deleting events from before", cutoff)
print("Number of events currently in DB:", db.collection.count())
res = db.collection.delete_many({"sent": {"$lt": cutoff}})
print("Deleted", res.deleted_count, "events")
print("Number of events left in DB:", db.collection.count())
sched.start()