run.py 3.8 KB

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