diff --git a/run.py b/run.py index 0435c8c..7f74b2a 100644 --- a/run.py +++ b/run.py @@ -6,6 +6,21 @@ from server.server import GameServer from server.config import ServerConfig +async def _run_tasks_with_optional_timeout(tasks): + """Await tasks, optionally honoring RUN_SECONDS env var to cancel after a timeout.""" + timeout_s = os.environ.get("RUN_SECONDS") + if not timeout_s: + await asyncio.gather(*tasks) + return + try: + await asyncio.wait_for(asyncio.gather(*tasks), timeout=float(timeout_s)) + except asyncio.TimeoutError: + logging.info("Timeout reached (RUN_SECONDS=%s); stopping server tasks...", timeout_s) + for t in tasks: + t.cancel() + await asyncio.gather(*tasks, return_exceptions=True) + + async def run_in_memory(): from server.transport import InMemoryTransport cfg = ServerConfig()