Skip to content

Commit

Permalink
WebHost: Guard each Room via file-lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Berserker66 committed Jun 29, 2021
1 parent 95e0f55 commit 4d4af9d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion MultiServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ async def process_client_cmd(ctx: Context, client: Client, args: dict):
if ctx.compatibility == 0 and args['version'] != version_tuple:
errors.add('IncompatibleVersion')
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)}])
else:
ctx.client_ids[client.team, client.slot] = args["uuid"]
Expand Down
9 changes: 6 additions & 3 deletions WebHostLib/autolauncher.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@

class CommonLocker():
"""Uses a file lock to signal that something is already running"""

def __init__(self, lockname: str):
lock_folder = "file_locks"
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.lockfile = f"./{self.lockname}.lck"
self.lockfile = os.path.join(self.lock_folder, f"{self.lockname}.lck")


class AlreadyRunningException(Exception):
Expand Down
5 changes: 4 additions & 1 deletion WebHostLib/customserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def get_save(self) -> dict:
def get_random_port():
return random.randint(49152, 65535)


def run_server_process(room_id, ponyconfig: dict):
# establish DB connection for multidata and multisave
db.bind(**ponyconfig)
Expand Down Expand Up @@ -144,7 +145,9 @@ async def main():
await ctx.shutdown_task
logging.info("Shutting down")

asyncio.run(main())
from .autolauncher import Locker
with Locker(room_id):
asyncio.run(main())


from WebHostLib import LOGS_FOLDER

0 comments on commit 4d4af9d

Please sign in to comment.