-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsgi.py
107 lines (82 loc) · 2.58 KB
/
wsgi.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
# this module primary focus is for use inside the docker container of spectacles.
from gevent import monkey
monkey.patch_all()
import click
from flask.cli import with_appcontext
import logging
import os
import time
from subprocess import run, PIPE, STDOUT
from pathlib import Path
from gevent.pywsgi import WSGIServer
from set_version import VERSION
from spectacles.helpers.app_logger import AppLogger
from spectacles.helpers.background_class import BackgroundTasks
from spectacles.webapp.run import create_app
__version__ = VERSION
app = create_app(version=__version__)
logging.setLoggerClass(AppLogger)
logger = logging.getLogger("spectacles")
current_dir = os.path.dirname(os.path.realpath(__file__))
@click.command()
@with_appcontext
def runserver():
# Check if this is a first run of the container
if not os.path.exists("/app/data/db/INIT_COMPLETED"):
init_database = "python3 db_migrate.py -i"
migrate_database = "python3 db_migrate.py -m"
update_database = "python3 db_migrate.py -u"
run(
init_database, # nosec
stdout=PIPE,
stderr=STDOUT,
universal_newlines=True,
shell=True,
cwd=current_dir,
)
run(
migrate_database, # nosec
stdout=PIPE,
stderr=STDOUT,
universal_newlines=True,
shell=True,
cwd=current_dir,
)
run(
update_database, # nosec
stdout=PIPE,
stderr=STDOUT,
universal_newlines=True,
shell=True,
cwd=current_dir,
)
Path("/app/data/db/INIT_COMPLETED").touch()
if os.path.exists(app.config["SPECTACLES_WEB_TLS_CERT_PATH"]) and os.path.exists(
app.config["SPECTACLES_WEB_TLS_KEY_PATH"]
):
http_server = WSGIServer(
("", 5050),
app,
certfile=app.config["SPECTACLES_WEB_TLS_CERT_PATH"],
keyfile=app.config["SPECTACLES_WEB_TLS_KEY_PATH"],
log=logger,
)
else:
http_server = WSGIServer(("", 5050), app, log=logger)
http_server.serve_forever()
@click.command()
@with_appcontext
def runbackground():
starttime = time.time()
bg = BackgroundTasks(app=app)
while True:
bg.run()
time.sleep(
float(app.config["SPECTACLES_BACKGROUND_UPDATE"])
- (
(time.time() - starttime)
% float(app.config["SPECTACLES_BACKGROUND_UPDATE"])
)
)
app.cli.add_command(runserver)
app.cli.add_command(runbackground)