|
| 1 | +import subprocess |
| 2 | +import os |
| 3 | +import signal |
| 4 | +import threading |
| 5 | +import logging |
| 6 | +import argparse |
| 7 | +import check_requirements |
| 8 | + |
| 9 | + |
| 10 | +# Set up logging |
| 11 | +logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') |
| 12 | + |
| 13 | +def run_server_script(args): |
| 14 | + # Start the server.py script as a new process group |
| 15 | + process = subprocess.Popen( |
| 16 | + ['python3', 'server.py', '--rpc-port', args.rpc_port], |
| 17 | + preexec_fn=os.setsid, # Create a new session and set the process group ID |
| 18 | + stdout=subprocess.PIPE, |
| 19 | + stderr=subprocess.STDOUT # Capture stderr and redirect it to stdout |
| 20 | + ) |
| 21 | + return process |
| 22 | + |
| 23 | +def run_start_script(args): |
| 24 | + # Start the start.sh script in its own directory as a new process group |
| 25 | + process = subprocess.Popen( |
| 26 | + ['bash', 'start.sh', '-t', args.team_name, '--rpc-port', args.rpc_port, '--rpc-type', 'grpc'], |
| 27 | + cwd='scripts/proxy', # Corrected directory to where start.sh is located |
| 28 | + preexec_fn=os.setsid, # Create a new session and set the process group ID |
| 29 | + stdout=subprocess.PIPE, |
| 30 | + stderr=subprocess.STDOUT # Capture stderr and redirect it to stdout |
| 31 | + ) |
| 32 | + return process |
| 33 | + |
| 34 | +def stream_output(process, prefix): |
| 35 | + # Stream output from the process and log it with a prefix |
| 36 | + for line in iter(process.stdout.readline, b''): |
| 37 | + logging.debug(f'{prefix} {line.decode().strip()}') |
| 38 | + process.stdout.close() |
| 39 | + |
| 40 | +def kill_process_group(process): |
| 41 | + try: |
| 42 | + os.killpg(os.getpgid(process.pid), signal.SIGTERM) # Send SIGTERM to the process group |
| 43 | + except ProcessLookupError: |
| 44 | + pass # The process might have already exited |
| 45 | + |
| 46 | +if __name__ == "__main__": |
| 47 | + # Set up argument parsing |
| 48 | + parser = argparse.ArgumentParser(description='Run server and team scripts.') |
| 49 | + parser.add_argument('-t', '--team_name', required=False, help='The name of the team', default='CLS') |
| 50 | + parser.add_argument('--rpc-port', required=False, help='The port of the server', default='50051') |
| 51 | + args = parser.parse_args() |
| 52 | + |
| 53 | + try: |
| 54 | + # Check Python requirements |
| 55 | + logging.debug("Checking Python requirements...") |
| 56 | + check_requirements.check_requirements() |
| 57 | + |
| 58 | + # Run the server.py script first |
| 59 | + server_process = run_server_script(args) |
| 60 | + logging.debug(f"Started server.py process with PID: {server_process.pid}") |
| 61 | + |
| 62 | + # Run the start.sh script after server.py with the given arguments |
| 63 | + start_process = run_start_script(args) |
| 64 | + logging.debug(f"Started start.sh process with PID: {start_process.pid} with team name {args=}") |
| 65 | + |
| 66 | + # Monitor both processes and log their outputs |
| 67 | + server_thread = threading.Thread(target=stream_output, args=(server_process, 'server:')) |
| 68 | + start_thread = threading.Thread(target=stream_output, args=(start_process, 'team:')) |
| 69 | + |
| 70 | + server_thread.start() |
| 71 | + start_thread.start() |
| 72 | + |
| 73 | + # Wait for both threads to finish |
| 74 | + server_thread.join() |
| 75 | + start_thread.join() |
| 76 | + |
| 77 | + except KeyboardInterrupt: |
| 78 | + logging.debug("Interrupted! Killing all processes.") |
| 79 | + kill_process_group(server_process) |
| 80 | + kill_process_group(start_process) |
| 81 | + |
| 82 | + finally: |
| 83 | + # Ensure all processes are killed on exit |
| 84 | + kill_process_group(server_process) |
| 85 | + kill_process_group(start_process) |
0 commit comments