From 673e5ec57ca9525927a3772643f4f82f2dfa5431 Mon Sep 17 00:00:00 2001 From: Peter Kraus Date: Sun, 14 Apr 2024 19:40:06 +0200 Subject: [PATCH] Pass verbosity of tomato-daemon (#77) --- src/tomato/daemon/driver.py | 20 +++++++++++++------- src/tomato/daemon/job.py | 27 ++++++++++++++++++++------- tests/conftest.py | 2 +- tests/test_01_tomato.py | 12 +++++++++++- 4 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/tomato/daemon/driver.py b/src/tomato/daemon/driver.py index b80ff0ca..a2377faa 100644 --- a/src/tomato/daemon/driver.py +++ b/src/tomato/daemon/driver.py @@ -53,6 +53,12 @@ def tomato_driver() -> None: default=1234, type=int, ) + parser.add_argument( + "--verbosity", + help="Verbosity of the tomato-driver.", + default=logging.INFO, + type=int, + ) parser.add_argument( "driver", type=str, @@ -64,7 +70,7 @@ def tomato_driver() -> None: logfile = f"drivers_{args.port}.log" logger = logging.getLogger(f"{__name__}.tomato_drivers({args.driver!r})") logging.basicConfig( - level=logging.DEBUG, + level=args.verbosity, format="%(asctime)s - %(levelname)8s - %(name)-30s - %(message)s", handlers=[logging.FileHandler(logfile, mode="a")], ) @@ -181,8 +187,8 @@ def tomato_driver() -> None: logger.critical(f"driver {args.driver!r} is quitting") -def spawn_tomato_driver(port: int, driver: str, req): - cmd = ["tomato-driver", "--port", str(port), driver] +def spawn_tomato_driver(port: int, driver: str, req: zmq.Socket, verbosity: int): + cmd = ["tomato-driver", "--port", str(port), "--verbosity", str(verbosity), driver] if psutil.WINDOWS: cfs = subprocess.CREATE_NO_WINDOW cfs |= subprocess.CREATE_NEW_PROCESS_GROUP @@ -244,23 +250,23 @@ def manager(port: int, timeout: int = 1000): for driver in drivers_needed: if driver not in daemon.drvs: logger.debug(f"spawning driver {driver!r}") - spawn_tomato_driver(daemon.port, driver, req) + spawn_tomato_driver(daemon.port, driver, req, daemon.verbosity) action_counter += 1 else: drv = daemon.drvs[driver] if drv.pid is not None and not psutil.pid_exists(drv.pid): logger.warning(f"respawning crashed driver {driver!r}") - spawn_tomato_driver(daemon.port, driver, req) + spawn_tomato_driver(daemon.port, driver, req, daemon.verbosity) action_counter += 1 elif drv.pid is None and drv.spawned_at is None: logger.debug(f"spawning driver {driver!r}") - spawn_tomato_driver(daemon.port, driver, req) + spawn_tomato_driver(daemon.port, driver, req, daemon.verbosity) action_counter += 1 elif drv.pid is None: tspawn = datetime.fromisoformat(drv.spawned_at) if (datetime.now(timezone.utc) - tspawn).seconds > 10: logger.warning(f"respawning late driver {driver!r}") - spawn_tomato_driver(daemon.port, driver, req) + spawn_tomato_driver(daemon.port, driver, req, daemon.verbosity) action_counter += 1 logger.debug("tick") time.sleep(1 if action_counter > 0 else 0.1) diff --git a/src/tomato/daemon/job.py b/src/tomato/daemon/job.py index 231a8bcd..b458b588 100644 --- a/src/tomato/daemon/job.py +++ b/src/tomato/daemon/job.py @@ -197,7 +197,14 @@ def action_queued_jobs(daemon, matched, req): with jpath.open("w", encoding="UTF-8") as of: json.dump(jobargs, of, indent=1) - cmd = ["tomato-job", "--port", str(daemon.port), str(jpath)] + cmd = [ + "tomato-job", + "--port", + str(daemon.port), + "--verbosity", + str(daemon.verbosity), + str(jpath), + ] if psutil.WINDOWS: cfs = subprocess.CREATE_NO_WINDOW cfs |= subprocess.CREATE_NEW_PROCESS_GROUP @@ -303,6 +310,12 @@ def tomato_job() -> None: default=10, type=int, ) + parser.add_argument( + "--verbosity", + help="Verbosity of the tomato-job.", + default=logging.INFO, + type=int, + ) parser.add_argument( "jobfile", type=Path, @@ -320,7 +333,7 @@ def tomato_job() -> None: logpath = jobpath / f"job-{jobid}.log" logging.basicConfig( - level=logging.DEBUG, + level=args.verbosity, format="%(asctime)s - %(levelname)8s - %(name)-30s - %(message)s", handlers=[logging.FileHandler(logpath, mode="a")], ) @@ -407,11 +420,11 @@ def job_thread( Stores the data for that Component as a `pickle` of a :class:`xr.Dataset`. """ sender = f"{__name__}.job_thread" - logging.basicConfig( - level=logging.DEBUG, - format="%(asctime)s - %(levelname)8s - %(name)-30s - %(message)s", - handlers=[logging.FileHandler(logpath, mode="a")], - ) + # logging.basicConfig( + # level=logging.DEBUG, + # format="%(asctime)s - %(levelname)8s - %(name)-30s - %(message)s", + # handlers=[logging.FileHandler(logpath, mode="a")], + # ) logger = logging.getLogger(sender) logger.debug(f"in job thread of {component.role!r}") diff --git a/tests/conftest.py b/tests/conftest.py index 2ea2c0bd..5835ec02 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -30,7 +30,7 @@ def start_tomato_daemon(tmpdir: str, port: int = 12345): # setup_stuff os.chdir(tmpdir) subprocess.run(["tomato", "init", "-p", f"{port}", "-A", ".", "-D", "."]) - subprocess.run(["tomato", "start", "-p", f"{port}", "-A", ".", "-L", ".", "-vv"]) + subprocess.run(["tomato", "start", "-p", f"{port}", "-A", ".", "-L", ".", "-VV"]) yield # teardown_stuff diff --git a/tests/test_01_tomato.py b/tests/test_01_tomato.py index baffa521..0d3341f2 100644 --- a/tests/test_01_tomato.py +++ b/tests/test_01_tomato.py @@ -3,6 +3,7 @@ from pathlib import Path import yaml import zmq +import subprocess from tomato import tomato from .utils import wait_until_tomato_running, wait_until_tomato_stopped @@ -155,7 +156,16 @@ def test_tomato_log_verbosity_0(datadir, stop_tomato_daemon): assert Path("daemon_12345.log").stat().st_size > 0 -def test_tomato_log_verbosity_default(start_tomato_daemon, stop_tomato_daemon): +def test_tomato_log_verbosity_testing(datadir, start_tomato_daemon, stop_tomato_daemon): + assert wait_until_tomato_running(port=PORT, timeout=5000) + assert Path("daemon_12345.log").exists() + assert Path("daemon_12345.log").stat().st_size > 0 + + +def test_tomato_log_verbosity_default(datadir, stop_tomato_daemon): + os.chdir(datadir) + subprocess.run(["tomato", "init", "-p", f"{PORT}", "-A", ".", "-D", "."]) + subprocess.run(["tomato", "start", "-p", f"{PORT}", "-A", ".", "-L", "."]) assert wait_until_tomato_running(port=PORT, timeout=5000) assert Path("daemon_12345.log").exists() assert Path("daemon_12345.log").stat().st_size > 0