Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fcwu
GitHub Repository: fcwu/docker-ubuntu-vnc-desktop
Path: blob/develop/rootfs/usr/local/lib/web/backend/run.py
387 views
1
#!/usr/bin/env python3
2
from __future__ import (
3
absolute_import, division, print_function, with_statement
4
)
5
import os
6
import time
7
import sys
8
import subprocess
9
from vnc.util import ignored
10
11
12
def main():
13
def run_with_reloader(main_func, extra_files=None, interval=3):
14
"""Run the given function in an independent python interpreter."""
15
def find_files(directory="./"):
16
for root, dirs, files in os.walk(directory):
17
for basename in files:
18
if basename.endswith('.py'):
19
filename = os.path.join(root, basename)
20
yield filename
21
22
if os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
23
try:
24
main_func()
25
except KeyboardInterrupt:
26
pass
27
return
28
29
proc = None
30
try:
31
while True:
32
log.info('Restarting with reloader {} {}'.format(
33
sys.executable,
34
' '.join(sys.argv))
35
)
36
args = [sys.executable] + sys.argv
37
new_environ = os.environ.copy()
38
new_environ['WERKZEUG_RUN_MAIN'] = 'true'
39
40
proc = subprocess.Popen(
41
args,
42
env=new_environ,
43
close_fds=True,
44
preexec_fn=os.setsid
45
)
46
mtimes = {}
47
restart = False
48
while not restart:
49
for filename in find_files():
50
try:
51
mtime = os.stat(filename).st_mtime
52
except OSError:
53
continue
54
55
old_time = mtimes.get(filename)
56
if old_time is None:
57
mtimes[filename] = mtime
58
continue
59
elif mtime > old_time:
60
log.info(
61
'Detected change in {}, reloading'.format(
62
filename
63
)
64
)
65
restart = True
66
proc.terminate()
67
break
68
time.sleep(interval)
69
except KeyboardInterrupt:
70
pass
71
finally:
72
with ignored(Exception):
73
proc.terminate()
74
75
def run_server():
76
import socket
77
from gevent.pywsgi import WSGIServer
78
from vnc.app import app
79
80
# websocket conflict: WebSocketHandler
81
if DEBUG:
82
# from werkzeug.debug import DebuggedApplication
83
app.debug = True
84
# app = DebuggedApplication(app, evalex=True)
85
86
try:
87
log.info('Listening on http://localhost:{}'.format(PORT))
88
http_server = WSGIServer(('localhost', PORT), app)
89
http_server.serve_forever()
90
# app.run(host='localhost', port=PORT)
91
except socket.error as e:
92
log.exception(e)
93
except KeyboardInterrupt:
94
pass
95
finally:
96
http_server.stop(timeout=10)
97
log.info('shutdown gracefully')
98
99
PORT = 6079
100
DEBUG = False
101
os.environ['CONFIG'] = 'config.Production'
102
entrypoint = run_server
103
if '--debug' in sys.argv:
104
DEBUG = True
105
os.environ['CONFIG'] = 'config.Development'
106
entrypoint = lambda: run_with_reloader(run_server)
107
108
# logging
109
import logging
110
from log.config import LoggingConfiguration
111
LoggingConfiguration.set(
112
logging.DEBUG if DEBUG else logging.INFO,
113
'/var/log/web.log'
114
)
115
logging.getLogger("werkzeug").setLevel(logging.WARNING)
116
log = logging.getLogger('novnc2')
117
118
entrypoint()
119
120
121
if __name__ == "__main__":
122
main()
123
124