1
|
|
|
# ==================================================================================== # |
2
|
|
|
# server.py - This file is part of the YFrake package. # |
3
|
|
|
# ------------------------------------------------------------------------------------ # |
4
|
|
|
# # |
5
|
|
|
# MIT License # |
6
|
|
|
# # |
7
|
|
|
# Copyright (c) 2022 Mattias Aabmets # |
8
|
|
|
# # |
9
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy # |
10
|
|
|
# of this software and associated documentation files (the "Software"), to deal # |
11
|
|
|
# in the Software without restriction, including without limitation the rights # |
12
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # |
13
|
|
|
# copies of the Software, and to permit persons to whom the Software is # |
14
|
|
|
# furnished to do so, subject to the following conditions: # |
15
|
|
|
# # |
16
|
|
|
# The above copyright notice and this permission notice shall be included in all # |
17
|
|
|
# copies or substantial portions of the Software. # |
18
|
|
|
# # |
19
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # |
20
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # |
21
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # |
22
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # |
23
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # |
24
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # |
25
|
|
|
# SOFTWARE. # |
26
|
|
|
# # |
27
|
|
|
# ==================================================================================== # |
28
|
|
|
from ..config.config import ConfigSingleton |
29
|
|
|
from pathlib import Path |
30
|
|
|
import subprocess |
31
|
|
|
import psutil |
32
|
|
|
import sys |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
# ==================================================================================== # |
36
|
|
|
class ServerSingleton: |
37
|
|
|
""" |
38
|
|
|
This class contains methods to control |
39
|
|
|
the YFrake server programmatically. |
40
|
|
|
""" |
41
|
|
|
# ------------------------------------------------------------------------------------ # |
42
|
|
|
_err_msg_1 = 'Server is already running! (YFrake)' |
43
|
|
|
_err_msg_2 = 'Cannot stop server which is not running! (YFrake)' |
44
|
|
|
_server: subprocess.Popen = None |
45
|
|
|
_is_running: bool = False |
46
|
|
|
__instance__ = None |
47
|
|
|
|
48
|
|
|
# Singleton pattern |
49
|
|
|
# ------------------------------------------------------------------------------------ # |
50
|
|
|
def __new__(cls): |
51
|
|
|
if not cls.__instance__: |
52
|
|
|
cls.__instance__ = super(ServerSingleton, cls).__new__(cls) |
53
|
|
|
return cls.__instance__ |
54
|
|
|
|
55
|
|
|
# ------------------------------------------------------------------------------------ # |
56
|
|
|
@classmethod |
57
|
|
|
def is_running(cls) -> bool: |
58
|
|
|
if isinstance(cls._server, subprocess.Popen): |
59
|
|
|
return cls._server.poll() is None |
60
|
|
|
return False |
61
|
|
|
|
62
|
|
|
# ------------------------------------------------------------------------------------ # |
63
|
|
|
@classmethod |
64
|
|
|
def start(cls) -> None: |
65
|
|
|
if cls.is_running(): |
66
|
|
|
raise RuntimeError(cls._err_msg_1) |
67
|
|
|
config = ConfigSingleton() |
68
|
|
|
runner_file = get_runner_file_path() |
69
|
|
|
args = [ |
70
|
|
|
sys.executable, runner_file, |
71
|
|
|
'--config-file', str(config.file) |
72
|
|
|
] |
73
|
|
|
cls._server = subprocess.Popen(args) |
74
|
|
|
|
75
|
|
|
# ------------------------------------------------------------------------------------ # |
76
|
|
|
@classmethod |
77
|
|
|
def stop(cls) -> None: |
78
|
|
|
if not cls.is_running(): |
79
|
|
|
raise RuntimeError(cls._err_msg_2) |
80
|
|
|
try: |
81
|
|
|
parent = psutil.Process(cls._server.pid) |
82
|
|
|
children = parent.children(recursive=True) |
83
|
|
|
procs = children + [parent] |
84
|
|
|
for proc in procs: |
85
|
|
|
proc.kill() |
86
|
|
|
except psutil.NoSuchProcess: # pragma: no cover |
87
|
|
|
return |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
# ==================================================================================== # |
91
|
|
|
def get_runner_file_path() -> str: |
92
|
|
|
runner_file = Path(__file__).with_name('runner.py').resolve() |
93
|
|
|
return str(runner_file) |
94
|
|
|
|