File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99import sys
1010import importlib .util
1111import json
12+ import queue
1213
1314class CommandExecutor :
1415 """
@@ -43,12 +44,21 @@ def run_multiple_commands(
4344 self .logger .log (f"Running { len (commands )} commands in parallel..." , 1 )
4445 start_time = time .time ()
4546
47+ # Queue to capture exceptions from threads
48+ exceptions_queue = queue .Queue ()
49+
50+ def run_command_thread_target (cmd ):
51+ try :
52+ self .run_command (cmd )
53+ except Exception as e :
54+ exceptions_queue .put (e )
55+
4656 # Initialize a list to keep track of threads
4757 threads = []
4858
4959 # Start a new thread for each command
5060 for cmd in commands :
51- thread = threading .Thread (target = self . run_command , args = (cmd ,))
61+ thread = threading .Thread (target = run_command_thread_target , args = (cmd ,))
5262 thread .start ()
5363 threads .append (thread )
5464
@@ -60,6 +70,11 @@ def run_multiple_commands(
6070 end_time = time .time ()
6171 self .logger .log (f"Total time to run { len (commands )} commands: { end_time - start_time :.2f} seconds" , 1 )
6272
73+ # Exceptions are logged once all threads have completed execution
74+ # The first exception amongst all is logged and raised
75+ if not exceptions_queue .empty ():
76+ raise RuntimeError (exceptions_queue .get ())
77+
6378 def run_command (self , command : list [str ]) -> None :
6479 """
6580 Executes a specified shell command and logs its execution details.
You can’t perform that action at this time.
0 commit comments