WebHost: Guard each Room via file-lock

This commit is contained in:
Fabian Dill 2021-06-29 03:11:48 +02:00
parent 95e0f551e8
commit 4d4af9d74e
3 changed files with 11 additions and 5 deletions

View File

@ -1034,7 +1034,7 @@ async def process_client_cmd(ctx: Context, client: Client, args: dict):
if ctx.compatibility == 0 and args['version'] != version_tuple: if ctx.compatibility == 0 and args['version'] != version_tuple:
errors.add('IncompatibleVersion') errors.add('IncompatibleVersion')
if errors: if errors:
logging.info(f"A client connection was refused due to: {errors}") logging.info(f"A client connection was refused due to: {errors}, the sent connect information was {args}.")
await ctx.send_msgs(client, [{"cmd": "ConnectionRefused", "errors": list(errors)}]) await ctx.send_msgs(client, [{"cmd": "ConnectionRefused", "errors": list(errors)}])
else: else:
ctx.client_ids[client.team, client.slot] = args["uuid"] ctx.client_ids[client.team, client.slot] = args["uuid"]

View File

@ -15,10 +15,13 @@ from Utils import restricted_loads
class CommonLocker(): class CommonLocker():
"""Uses a file lock to signal that something is already running""" """Uses a file lock to signal that something is already running"""
lock_folder = "file_locks"
def __init__(self, lockname: str): def __init__(self, lockname: str, folder=None):
if folder:
self.lock_folder = folder
os.makedirs(self.lock_folder, exist_ok=True)
self.lockname = lockname self.lockname = lockname
self.lockfile = f"./{self.lockname}.lck" self.lockfile = os.path.join(self.lock_folder, f"{self.lockname}.lck")
class AlreadyRunningException(Exception): class AlreadyRunningException(Exception):

View File

@ -104,6 +104,7 @@ class WebHostContext(Context):
def get_random_port(): def get_random_port():
return random.randint(49152, 65535) return random.randint(49152, 65535)
def run_server_process(room_id, ponyconfig: dict): def run_server_process(room_id, ponyconfig: dict):
# establish DB connection for multidata and multisave # establish DB connection for multidata and multisave
db.bind(**ponyconfig) db.bind(**ponyconfig)
@ -144,7 +145,9 @@ def run_server_process(room_id, ponyconfig: dict):
await ctx.shutdown_task await ctx.shutdown_task
logging.info("Shutting down") logging.info("Shutting down")
asyncio.run(main()) from .autolauncher import Locker
with Locker(room_id):
asyncio.run(main())
from WebHostLib import LOGS_FOLDER from WebHostLib import LOGS_FOLDER