-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_service.sh
More file actions
executable file
·289 lines (255 loc) · 7.77 KB
/
Copy pathrun_service.sh
File metadata and controls
executable file
·289 lines (255 loc) · 7.77 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/bin/bash
# ArbitAI FastAPI Service Runner
# This script handles the complete startup process for the ArbitAI service
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
VENV_NAME="ArbitAI_Env"
PYTHON_VERSION="python3"
SERVICE_NAME="ArbitAI FastAPI Service"
LOG_FILE="arbit_ai_service.log"
PID_FILE="arbit_ai_service.pid"
# Function to print colored messages
print_message() {
echo -e "${2}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}"
}
print_success() {
print_message "$1" "$GREEN"
}
print_error() {
print_message "$1" "$RED"
}
print_warning() {
print_message "$1" "$YELLOW"
}
print_info() {
print_message "$1" "$BLUE"
}
# Function to check if virtual environment exists
check_venv() {
if [ ! -d "$VENV_NAME" ]; then
print_error "Virtual environment '$VENV_NAME' not found!"
print_info "Please run: python -m venv $VENV_NAME"
exit 1
fi
print_success "Virtual environment found"
}
# Function to activate virtual environment
activate_venv() {
print_info "Activating virtual environment..."
source "$VENV_NAME/bin/activate" || {
print_error "Failed to activate virtual environment"
exit 1
}
print_success "Virtual environment activated"
}
# Function to install/update dependencies
install_dependencies() {
print_info "Installing/updating dependencies..."
pip install --upgrade pip
pip install -r requirements.txt || {
print_error "Failed to install dependencies"
exit 1
}
print_success "Dependencies installed successfully"
}
# Function to check environment variables
check_env_vars() {
print_info "Checking environment variables..."
if [ ! -f ".env" ]; then
print_warning ".env file not found, checking .env.example..."
if [ -f ".env.example" ]; then
print_warning "Copying .env.example to .env"
cp .env.example .env
print_warning "Please edit .env with your actual API keys"
else
print_error "No .env or .env.example file found"
exit 1
fi
fi
# Load environment variables
export $(cat .env | grep -v '^#' | xargs) 2>/dev/null || true
if [ -z "$GROQ_API_KEY" ]; then
print_error "GROQ_API_KEY not set in .env file"
print_info "Please add your Groq API key to the .env file"
exit 1
fi
print_success "Environment variables validated"
}
# Function to check if service is already running
check_running() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p $PID > /dev/null 2>&1; then
print_warning "Service is already running with PID $PID"
print_info "To stop it, run: ./run_service.sh stop"
exit 1
else
print_warning "Stale PID file found, removing..."
rm -f "$PID_FILE"
fi
fi
}
# Function to start the service
start_service() {
print_info "Starting $SERVICE_NAME..."
# Run pre-flight checks
check_venv
activate_venv
check_env_vars
check_running
# Install dependencies if needed
if [ "$1" = "--install-deps" ] || [ "$1" = "-i" ]; then
install_dependencies
fi
# Start the service
print_info "Launching FastAPI service..."
# Background mode
if [ "$2" = "--background" ] || [ "$2" = "-b" ] || [ "$1" = "--background" ] || [ "$1" = "-b" ]; then
nohup python -m uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload > "$LOG_FILE" 2>&1 &
echo $! > "$PID_FILE"
print_success "Service started in background with PID $(cat $PID_FILE)"
print_info "Logs: tail -f $LOG_FILE"
else
# Foreground mode
python -m uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
fi
}
# Function to stop the service
stop_service() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p $PID > /dev/null 2>&1; then
print_info "Stopping service with PID $PID..."
kill $PID
rm -f "$PID_FILE"
print_success "Service stopped successfully"
else
print_warning "Service not running, removing stale PID file"
rm -f "$PID_FILE"
fi
else
print_warning "Service doesn't appear to be running"
fi
}
# Function to restart the service
restart_service() {
print_info "Restarting $SERVICE_NAME..."
stop_service
sleep 2
start_service "$@"
}
# Function to show service status
show_status() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p $PID > /dev/null 2>&1; then
print_success "Service is running with PID $PID"
print_info "API URL: http://localhost:8000"
print_info "Docs URL: http://localhost:8000/docs"
print_info "Health Check: http://localhost:8000/health"
else
print_error "Service is not running (stale PID file)"
rm -f "$PID_FILE"
fi
else
print_warning "Service is not running"
fi
}
# Function to show logs
show_logs() {
if [ -f "$LOG_FILE" ]; then
if [ "$1" = "--follow" ] || [ "$1" = "-f" ]; then
tail -f "$LOG_FILE"
else
tail -n 50 "$LOG_FILE"
fi
else
print_warning "Log file not found"
fi
}
# Function to setup the environment
setup_env() {
print_info "Setting up ArbitAI development environment..."
# Create virtual environment if it doesn't exist
if [ ! -d "$VENV_NAME" ]; then
print_info "Creating virtual environment..."
$PYTHON_VERSION -m venv "$VENV_NAME"
print_success "Virtual environment created"
fi
# Activate and install dependencies
activate_venv
install_dependencies
# Setup .env file
if [ ! -f ".env" ]; then
if [ -f ".env.example" ]; then
cp .env.example .env
print_success ".env file created from .env.example"
fi
fi
print_success "Environment setup complete!"
print_warning "Don't forget to edit .env with your API keys"
}
# Function to show help
show_help() {
echo -e "${BLUE}$SERVICE_NAME - Control Script${NC}"
echo ""
echo "Usage: $0 [COMMAND] [OPTIONS]"
echo ""
echo "Commands:"
echo " start Start the service in foreground"
echo " start -b|--background Start the service in background"
echo " start -i|--install-deps Install dependencies before starting"
echo " stop Stop the running service"
echo " restart Restart the service"
echo " status Show service status"
echo " logs Show recent logs"
echo " logs -f|--follow Follow logs in real-time"
echo " setup Setup the development environment"
echo " help Show this help message"
echo ""
echo "Examples:"
echo " $0 setup # Initial setup"
echo " $0 start # Start in foreground"
echo " $0 start -i -b # Install deps and start in background"
echo " $0 logs -f # Follow logs"
echo ""
}
# Main script logic
case "$1" in
"start")
start_service "$2" "$3"
;;
"stop")
stop_service
;;
"restart")
restart_service "$2" "$3"
;;
"status")
show_status
;;
"logs")
show_logs "$2"
;;
"setup")
setup_env
;;
"help"|"--help"|"-h")
show_help
;;
"")
print_warning "No command specified. Use 'help' to see available commands."
show_help
;;
*)
print_error "Unknown command: $1"
show_help
exit 1
;;
esac