Compare commits

..

2 Commits

Author SHA1 Message Date
Bram van den Heuvel e94fc2d4d3 Allow NBSR for stderr 2023-10-02 00:36:12 +02:00
Bram van den Heuvel 6a606a61f5 Fix bugs + clean code 2023-10-01 20:49:47 +02:00
7 changed files with 25 additions and 12 deletions

View File

@ -7,10 +7,6 @@ WORKDIR /usr/src/app
COPY requirements.txt ./ COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt RUN pip install --no-cache-dir -r requirements.txt
# # Prepare MC server
# COPY server.jar ./
# RUN java -jar server.jar --nogui
COPY src/ ./src/ COPY src/ ./src/
COPY main.py ./ COPY main.py ./

View File

@ -38,4 +38,6 @@ services:
- <your whitelist>:/usr/src/app/whitelist.json - <your whitelist>:/usr/src/app/whitelist.json
- <your world folder>:/usr/src/app/world - <your world folder>:/usr/src/app/world
- <your server jar file>:/usr/src/app/server.jar - <your server jar file>:/usr/src/app/server.jar
ports:
- 25565:25565
``` ```

View File

@ -39,11 +39,11 @@ matrix:
# When multiple RegEx strings apply, all are included. # When multiple RegEx strings apply, all are included.
alternative_platforms: alternative_platforms:
Discord: Discord:
match: "@_?discord_\d+:.+" match: "@_?discord_\\d+:.+"
text: D text: D
color: aqua color: aqua
WhatsApp: WhatsApp:
match: "@whatsapp_\d+:.+" match: "@whatsapp_\\d+:.+"
text: W text: W
color: green color: green

View File

@ -6,7 +6,7 @@ from typing import Any, List, Optional
import yaml import yaml
with open('config.yaml', 'r') as open_file: with open('config.yaml', 'r') as open_file:
SETTINGS = yaml.load(open_file) SETTINGS = yaml.load(open_file, Loader=yaml.Loader)
def at(keys : List[str]) -> Optional[Any]: def at(keys : List[str]) -> Optional[Any]:
""" """

View File

@ -10,7 +10,7 @@ from typing import Union
import asyncio import asyncio
import json import json
import re import re
from src.nbsr import NonBlockingStreamReader as NBSR from src.nbsr import NonBlockingStreamReader as NBSR, ProcessHasTerminated
import src.config as config import src.config as config
import src.build_server as build import src.build_server as build
@ -23,15 +23,24 @@ p = Popen(config.RUN_COMMAND,
stdin = PIPE, stdout = PIPE, stderr = PIPE, shell = False) stdin = PIPE, stdout = PIPE, stderr = PIPE, shell = False)
# wrap p.stdout with a NonBlockingStreamReader object: # wrap p.stdout with a NonBlockingStreamReader object:
nbsr = NBSR(p.stdout) nbsr = NBSR(p.stdout)
err_nbsr = NBSR(p.stderr)
async def start(client, mc_channel): async def start(client, mc_channel):
""" """
Start reading from the Minecraft subprocess. Start reading from the Minecraft subprocess.
""" """
# Provide 3 seconds to prepare the server
await asyncio.sleep(3) await asyncio.sleep(3)
while True: while True:
try:
output = nbsr.readline(0.1) output = nbsr.readline(0.1)
except ProcessHasTerminated:
print('==================================\nERROR LOGS :')
while True:
output = err_nbsr.readline(0.1)
print(output)
# 0.1 secs to let the shell output the result # 0.1 secs to let the shell output the result
if not output: if not output:
await asyncio.sleep(1) await asyncio.sleep(1)
@ -81,7 +90,7 @@ def process_message(sentence : str) -> Union[str, None]:
r"\[[\d:]+\] \[Server thread\/INFO\]: Done \(\d+.?\d*s\)! For help, type \"help\"", r"\[[\d:]+\] \[Server thread\/INFO\]: Done \(\d+.?\d*s\)! For help, type \"help\"",
sentence): sentence):
server_live = True server_live = True
return f"The Minecraft server is live. The server is reacable at <code>{config.SERVER_IP}</code>.", f"The minecraft server is live. The server is reacable at <code>{config.SERVER_IP}</code>." return f"The Minecraft server is live. The server is reachable at <code>{config.SERVER_IP}</code>.", f"The minecraft server is live. The server is reachable at <code>{config.SERVER_IP}</code>."
if re.fullmatch( if re.fullmatch(
r"\[[\d:]+\] \[Server thread\/INFO\]: Stopping server", r"\[[\d:]+\] \[Server thread\/INFO\]: Stopping server",

View File

@ -46,6 +46,11 @@ class NonBlockingStreamReader:
return self._q.get(block = timeout is not None, return self._q.get(block = timeout is not None,
timeout = timeout) timeout = timeout)
except Empty: except Empty:
if self._t.is_alive():
return None return None
else:
raise ProcessHasTerminated
class UnexpectedEndOfStream(Exception): pass class UnexpectedEndOfStream(Exception): pass
class ProcessHasTerminated(Exception): pass

1
whitelist.json Normal file
View File

@ -0,0 +1 @@
[]