-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathserver.sh
More file actions
executable file
·174 lines (151 loc) · 5.08 KB
/
server.sh
File metadata and controls
executable file
·174 lines (151 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env bash
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# server.sh: A script to manage the Gerrit MCP server.
# --- Configuration ---
# Get the directory where the script is located.
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# The command to run your server.
# IMPORTANT: This should not include redirection (>) or backgrounding (&).
# The script handles that automatically.
SERVER_COMMAND=".venv/bin/uvicorn gerrit_mcp_server.main:app --host localhost --port 6322"
# The file to store the Process ID (PID) of the running server.
PID_FILE="$SCRIPT_DIR/server.pid"
# The file to store the server's logs.
LOG_FILE="$SCRIPT_DIR/server.log"
# --- Functions ---
# Function to check if the server is currently running.
# It checks for the PID file and then verifies if the process is active.
is_running() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
# Check if a process with this PID exists.
# The >/dev/null 2>&1 suppresses output from the ps command.
if ps -p $PID > /dev/null 2>&1; then
return 0 # 0 means true (is running)
fi
fi
return 1 # 1 means false (is not running)
}
# Function to start the server.
start_server() {
if is_running; then
echo "Server is already running with PID $(cat "$PID_FILE")."
exit 1
fi
echo "Starting server..."
# Use setsid to run the server in a new session, creating a new process group.
# This prevents the stop command from killing the script that called it (e.g., the test runner).
$SERVER_COMMAND > "$LOG_FILE" 2>&1 & disown
# '$!' is a special variable that holds the PID of the last command run in the background.
echo $! > "$PID_FILE"
# Give it a moment to ensure it started correctly.
sleep 1
if is_running; then
echo "Server started successfully with PID $(cat "$PID_FILE")."
echo "Logs are being written to $LOG_FILE."
else
echo "Error: Server failed to start. Check '$LOG_FILE' for details."
# Clean up the stale PID file if the server failed to start.
rm -f "$PID_FILE"
exit 1
fi
}
# Function to stop the server.
stop_server() {
if ! is_running; then
echo "Server is not running."
if [ -f "$PID_FILE" ]; then
echo "Cleaning up stale PID file: $PID_FILE"
rm -f "$PID_FILE"
fi
exit 0
fi
PID=$(cat "$PID_FILE")
PGID=$(ps -o pgid= -p "$PID" | grep -o '[0-9]*')
echo "Stopping server process group with PGID $PGID..."
# Kill the entire process group gracefully
if kill -s SIGTERM -- -$PGID; then
# Wait for the process group to actually terminate
for i in {1..5}; do
if ! ps -p $PID > /dev/null 2>&1; then
echo "Server stopped successfully."
rm -f "$PID_FILE"
exit 0
fi
sleep 1
done
echo "Warning: Server did not stop gracefully. Forcing shutdown (kill -9)."
kill -s SIGKILL -- -$PGID
rm -f "$PID_FILE"
echo "Server shutdown forced."
else
echo "Error: Failed to send stop signal to process group $PGID."
exit 1
fi
}
# Function to check the status of the server.
check_status() {
if is_running; then
echo "Server is RUNNING with PID $(cat "$PID_FILE")."
else
echo "Server is STOPPED."
# according to LSB init script guidelines, return code 3 indicates the service is not running
exit 3
fi
}
# Function to tail the server logs.
tail_logs() {
if [ ! -f "$LOG_FILE" ]; then
echo "Log file not found: $LOG_FILE"
echo "Has the server been started at least once?"
exit 1
fi
echo "Tailing logs from $LOG_FILE... (Press Ctrl+C to stop)"
# 'tail -f' follows the file, showing new lines as they are added.
tail -f "$LOG_FILE"
}
# Function to restart the server.
restart_server() {
echo "Restarting server..."
# Stop the server; the stop command handles cases where it's already stopped.
"$0" stop
# Start the server again.
"$0" start
}
# --- Main Script Logic ---
# Check the first command-line argument ($1) to decide what to do.
case "$1" in
start)
start_server
;;
stop)
stop_server
;;
restart)
restart_server
;;
status)
check_status
;;
logs)
tail_logs
;;
*)
# If the argument is not recognized, show a usage message.
echo "Usage: $0 {start|stop|restart|status|logs}"
exit 1
;;
esac
exit 0