Repository navigation
Expand file tree
/
Copy pathrun_server.py
More file actions
65 lines (53 loc) · 1.78 KB
/
Copy pathrun_server.py
File metadata and controls
65 lines (53 loc) · 1.78 KB
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
# -*- coding: utf-8 -*-
"""
Created by: Mistayan
Project: President-Game
IDE: PyCharm
Creation-date: 11/20/22
"""
import logging
import platform
from multiprocessing import Pool
from subprocess import Popen
import coloredlogs
from conf import VENV_PATH, ROOT_LOGGER
from models import GameFinder
from models import PresidentGame
def run_background(game):
with Pool(processes=1, ) as pool:
ROOT_LOGGER.info("starting %s", game.__qualname__)
pool.apply_async(game().run_server()) # Game will find port to run on its own
def run_with_gunicorn(game, port):
print("running with gunicorn")
app = Popen(f"{VENV_PATH} gunicorn --bind 0.0.0.0:{port} wsgi:{game}".split())
app.communicate()
def run_with_waitress(game, port):
print("running with waitress")
print(VENV_PATH)
try:
app = Popen(f"{VENV_PATH} waitress-serve"
f" --host 0.0.0.0 --port {port} --call models:{game}"
"".split())
app.communicate()
except PermissionError:
print("Requires admin rights...")
raise
def auto_run(game):
system = platform.system()
try:
ROOT_LOGGER.info("Scanning for port to run to...")
_, port = GameFinder(target="localhost").availabilities[0]
try:
if system == "Windows":
run_with_waitress(game, port)
elif system in ("POSIX", "LINUX", "Linux", "Darwin"):
run_with_gunicorn(game, port)
except Exception:
print("Failed to run properly, switching to base methods")
run_background(game)
except KeyboardInterrupt as e:
print("Server Shutting down...")
ROOT_LOGGER.critical(e)
if __name__ == '__main__':
coloredlogs.install(level=logging.INFO)
auto_run(PresidentGame)