Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass verbosity of tomato-daemon to child processes #77

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/tomato/daemon/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")],
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 20 additions & 7 deletions src/tomato/daemon/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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")],
)
Expand Down Expand Up @@ -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}")

Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 11 additions & 1 deletion tests/test_01_tomato.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down