Skip to content

Commit 1d409bb

Browse files
committed
add start scripts
1 parent 069bb0d commit 1d409bb

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

start-team.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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)

start-team.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/bash
2+
3+
# Ensure the script exits if any command fails
4+
set -e
5+
# check scripts/proxy directory does not exist, raise error
6+
if [! -d scripts/proxy ]; then
7+
echo "scripts/proxy directory does not exist"
8+
exit 1
9+
fi
10+
11+
team_name="CLS"
12+
rpc_port=50051
13+
14+
# help function
15+
usage() {
16+
echo "Usage: $0 [options]"
17+
echo "Options:"
18+
echo " -t team_name: specify team name"
19+
echo " --rpc-port PORT - specifies rpc port (default: 50051)"
20+
exit 1
21+
}
22+
23+
while [ $# -gt 0 ]
24+
do
25+
case $1 in
26+
-t)
27+
team_name=$2
28+
shift
29+
;;
30+
--rpc-port)
31+
rpc_port=$2
32+
shift
33+
;;
34+
*)
35+
echo 1>&2
36+
echo "invalid option \"${1}\"." 1>&2
37+
echo 1>&2
38+
usage
39+
exit 1
40+
;;
41+
esac
42+
43+
shift 1
44+
done
45+
46+
# Check Python requirements
47+
echo "Checking Python requirements..."
48+
python3 check_requirements.py
49+
50+
# Start server.py in the background
51+
echo "Starting server.py..."
52+
python3 server.py --rpc-port $rpc_port &
53+
server_pid=$!
54+
55+
# Function to kill server and team processes on exit
56+
cleanup() {
57+
echo "Cleaning up..."
58+
kill $server_pid
59+
kill $start_pid
60+
}
61+
62+
# Trap the exit signal to cleanup processes
63+
trap cleanup EXIT
64+
65+
# Wait a moment to ensure the server has started (optional)
66+
sleep 2
67+
68+
# Start start.sh script in the correct directory with arguments
69+
echo "Starting start.sh with team name: $team_name and ..."
70+
cd scripts/proxy
71+
bash start.sh -t "$team_name" --rpc-port $rpc_port --rpc-type grpc &
72+
start_pid=$!
73+
74+
# Wait for both background processes to finish
75+
wait $server_pid
76+
wait $start_pid

0 commit comments

Comments
 (0)