@@ -379,26 +379,50 @@ def is_remote_monitor_running(
379379 assert isinstance (result , subprocess .CompletedProcess )
380380 return bool (result .stdout .strip ())
381381
382- def wait_for_remote_monitor_start (
382+ def _wait_for_remote_monitor_state (
383383 self ,
384- timeout : int ,
385- polling_interval : int ,
384+ expected_running : bool ,
385+ polling_interval : float ,
386+ timeout : Optional [float ] = None ,
386387 execution_mode : Optional [str ] = None ,
387388 experiment_output_dir : Optional [str ] = None ,
389+ waiting_message : Optional [str ] = None ,
388390 ) -> None :
389- """Poll until a matching ``remote_monitor.py`` is running on the node ."""
390- start = time .time ()
391- while time . time () - start < timeout :
392- if self .is_remote_monitor_running (
391+ """Wait for a matching remote monitor to be running or stopped ."""
392+ start = time .monotonic ()
393+ while True :
394+ is_running = self .is_remote_monitor_running (
393395 execution_mode = execution_mode ,
394396 experiment_output_dir = experiment_output_dir ,
395- ):
397+ )
398+ if is_running == expected_running :
396399 return
400+ if timeout is not None and time .monotonic () - start >= timeout :
401+ mode_desc = execution_mode or "any"
402+ state_desc = "start" if expected_running else "exit"
403+ raise RuntimeError (
404+ f"remote_monitor.py ({ mode_desc } ) did not { state_desc } "
405+ f"within { timeout } s. Check remote_monitor.out under the "
406+ "experiment output dir."
407+ )
408+ if waiting_message :
409+ print (waiting_message )
397410 time .sleep (polling_interval )
398- mode_desc = execution_mode or "any"
399- raise RuntimeError (
400- f"remote_monitor.py ({ mode_desc } ) did not start within "
401- f"{ timeout } s. Check remote_monitor.out under the experiment output dir."
411+
412+ def wait_for_remote_monitor_start (
413+ self ,
414+ timeout : int ,
415+ polling_interval : int ,
416+ execution_mode : Optional [str ] = None ,
417+ experiment_output_dir : Optional [str ] = None ,
418+ ) -> None :
419+ """Poll until a matching ``remote_monitor.py`` is running on the node."""
420+ self ._wait_for_remote_monitor_state (
421+ expected_running = True ,
422+ polling_interval = polling_interval ,
423+ timeout = timeout ,
424+ execution_mode = execution_mode ,
425+ experiment_output_dir = experiment_output_dir ,
402426 )
403427
404428 def wait_for_remote_monitor_process_exit (
@@ -408,15 +432,16 @@ def wait_for_remote_monitor_process_exit(
408432 experiment_output_dir : Optional [str ] = None ,
409433 ) -> None :
410434 """Poll until matching ``remote_monitor.py`` processes have exited."""
411- while self .is_remote_monitor_running (
435+ self ._wait_for_remote_monitor_state (
436+ expected_running = False ,
437+ polling_interval = polling_interval ,
412438 execution_mode = execution_mode ,
413439 experiment_output_dir = experiment_output_dir ,
414- ):
415- print (
440+ waiting_message = (
416441 "Waiting for remote monitor to exit "
417442 f"(checking again in { polling_interval } s)..."
418- )
419- time . sleep ( polling_interval )
443+ ),
444+ )
420445
421446 def signal_ingest_monitor_stop (self , experiment_output_dir : str ) -> None :
422447 """Ask an ingest-mode ``remote_monitor.py`` to shut down gracefully."""
@@ -429,7 +454,6 @@ def signal_ingest_monitor_stop(self, experiment_output_dir: str) -> None:
429454 cmd_dir = "" ,
430455 nohup = False ,
431456 popen = False ,
432- ignore_errors = True ,
433457 )
434458
435459 def cleanup_ingest_monitor_stop_file (self , experiment_output_dir : str ) -> None :
@@ -508,16 +532,17 @@ def wait_for_remote_monitor_to_finish(
508532 time .sleep (minimum_experiment_running_time )
509533 print ("Done waiting for remote monitor to finish. Will start polling" )
510534
511- while self .is_remote_monitor_running (
535+ self ._wait_for_remote_monitor_state (
536+ expected_running = False ,
537+ polling_interval = polling_interval ,
512538 execution_mode = execution_mode ,
513539 experiment_output_dir = experiment_output_dir ,
514- ):
515- print (
540+ waiting_message = (
516541 "Remote monitor is still running. Will check again in {} seconds" .format (
517542 polling_interval
518543 )
519- )
520- time . sleep ( polling_interval )
544+ ),
545+ )
521546
522547 def is_healthy (self ) -> bool :
523548 """
0 commit comments