-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.py
121 lines (89 loc) · 2.57 KB
/
worker.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
from flask import Flask, request
from django.apps import apps
from django.conf import settings
from threading import Thread, Event
import os
from time import sleep
import logging
from datetime import datetime, timedelta
import pytz
if 'DJANGO_SETTINGS_MODULE' not in os.environ:
os.environ['DJANGO_SETTINGS_MODULE'] = "bcfdata.settings"
apps.populate(settings.INSTALLED_APPS)
from data.models import Sailing
from data.utils import (get_actual_departures, get_current_conditions,
get_ferry_locations, get_sailing_detail)
app = Flask(__name__)
last_run = datetime.utcfromtimestamp(0)
last_detail_run = datetime.utcfromtimestamp(0)
tz = pytz.timezone(settings.DISPLAY_TIME_ZONE)
interval = 600
def shutdown_flask():
func = request.environ.get('werkzeug.server.shutdown')
func()
def periodic_update():
global last_run
while e.is_set() is False:
td = datetime.now() - last_run
if td.seconds > 600:
print("Querying departures...")
get_actual_departures()
print("Querying conditions...")
get_current_conditions()
print("Querying ferry locations...")
get_ferry_locations()
last_run = datetime.now()
else:
print("Sleeping ({} seconds to go)".format(interval - td.seconds))
sleep(10)
print("Shutdown worker")
def periodic_detail_update():
global last_detail_run
while e.is_set() is False:
td = datetime.now() - last_detail_run
if td.seconds > 3600:
get_sailing_detail()
last_detail_run = datetime.now()
else:
print("Sleeping ({} seconds to go)".format(3600 - td.seconds))
sleep(10)
print("Shutdown worker")
@app.route("/status")
def status():
return str(last_run)
@app.route("/departures")
def departures():
get_actual_departures()
return "OK"
@app.route("/conditions")
def conditions():
get_current_conditions()
return "OK"
@app.route("/locations")
def locations():
get_ferry_locations()
return "OK"
@app.route("/update")
def update():
get_actual_departures()
get_current_conditions()
get_ferry_locations()
return "OK"
@app.route("/shutdown")
def shutdown():
e.set()
shutdown_flask()
return "OK"
@app.route("/")
def index():
return str(Sailing.objects.count())
e = Event()
t = Thread(target=periodic_update)
d = Thread(target=periodic_detail_update)
a = Thread(target=app.run, kwargs={
"host": "0.0.0.0", "port": 6124
})
if __name__=='__main__':
t.start()
# d.start()
a.start()