Skip to content

Commit

Permalink
update stop logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ableytner committed Oct 6, 2024
1 parent 544d015 commit f5f1178
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
25 changes: 18 additions & 7 deletions mcserverwrapper/src/server/base_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@ def stop(self):
self.execute_command("/stop")

def kill(self):
"""Kill the server process, but DATA MAY BE LOST"""
"""Kill the server process, but UNSAVED DATA WILL BE LOST AND SAVES POSSIBLY CORRUPTED"""

logger.log("Killing server process")
if sys.platform == "win32":
self._child.kill(signal.CTRL_C_EVENT)
os.system(f"taskkill /pid {self._child.pid} /f")
else:
self._child.kill(signal.SIGTERM)
self._child.kill(signal.SIGKILL)

if self.get_child_status(30) is None:
logger.log("Server did not stop")
logger.log("We're running out of options, maybe try manually killing the server?")

def execute_command(self, command: str) -> None:
"""Send a given command to the server"""
Expand Down Expand Up @@ -150,12 +154,19 @@ def _wait_for_startup(self):
response = info_getter.ping_address_with_return("127.0.0.1", self._port)
sleep(1)

def _stopping(self):
def _ensure_stop(self):
logger.log("Stopping server")
status = self.get_child_status(20)
status = self.get_child_status(30)
if status is None:
logger.log("Server did not stop within 20 seconds")
self.kill()
logger.log("Server did not stop within 30 seconds")
if sys.platform == "win32":
self._child.kill(signal.CTRL_C_EVENT)
else:
self._child.kill(signal.SIGTERM)

status = self.get_child_status(60)
if status is None:
logger.log("Server did not stop within 90 seconds")

def _format_output(self, raw_text: bytes) -> str:
# remove line breaks
Expand Down
2 changes: 1 addition & 1 deletion mcserverwrapper/src/server/forge_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def execute_command(self, command: str):
super().execute_command(command)

if command == "/stop":
self._stopping()
self._ensure_stop()

@classmethod
def _check_jar(cls, jar_file: str) -> McVersion | None:
Expand Down
2 changes: 1 addition & 1 deletion mcserverwrapper/src/server/paper_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def execute_command(self, command: str):
super().execute_command(command)

if command == "stop":
self._stopping()
self._ensure_stop()

@staticmethod
def _check_jar(jar_file: str) -> McVersion | None:
Expand Down
2 changes: 1 addition & 1 deletion mcserverwrapper/src/server/vanilla_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def execute_command(self, command: str):
super().execute_command(command)

if command == "/stop":
self._stopping()
self._ensure_stop()

@classmethod
def _check_jar(cls, jar_file: str) -> McVersion | None:
Expand Down

0 comments on commit f5f1178

Please sign in to comment.