report-detect/start.sh

274 lines
6.7 KiB
Bash

#!/bin/bash
# OCR Backend Startup Script
# 用于启动完整的OCR处理系统
set -e
# 配置
APP_NAME="report-detect-backend"
APP_VERSION="1.0.0"
JAR_FILE="target/${APP_NAME}-${APP_VERSION}.jar"
PYTHON_DIR="./python_api"
LOG_DIR="./logs"
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 创建日志目录
mkdir -p "$LOG_DIR"
echo_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
echo_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
echo_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 检查依赖
check_dependencies() {
echo_info "Checking dependencies..."
# Java
if ! command -v java &> /dev/null; then
echo_error "Java not found. Please install Java 8+"
exit 1
fi
# Python
if ! command -v python3 &> /dev/null; then
echo_error "Python 3 not found. Please install Python 3.8+"
exit 1
fi
# RabbitMQ
if ! command -v rabbitmq-server &> /dev/null; then
echo_warn "RabbitMQ not found. Please install RabbitMQ or ensure it's running on a remote host"
fi
echo_info "All dependencies OK"
}
# 启动RabbitMQ (如果是本地)
start_rabbitmq() {
if command -v rabbitmq-server &> /dev/null; then
echo_info "Starting RabbitMQ..."
sudo systemctl start rabbitmq-server || echo_warn "Failed to start RabbitMQ (may require sudo)"
fi
}
# 启动Flask API
start_flask() {
echo_info "Starting Flask OCR API..."
cd "$PYTHON_DIR"
nohup python3 ocr_api_server.py > "$LOG_DIR/flask.log" 2>&1 &
FLASK_PID=$!
echo $FLASK_PID > "$LOG_DIR/flask.pid"
echo_info "Flask API started (PID: $FLASK_PID)"
echo_info "Waiting for Flask to be ready..."
# 等待Flask启动
for i in {1..30}; do
if curl -s http://localhost:8081/health > /dev/null 2>&1; then
echo_info "Flask API is ready!"
cd ..
return 0
fi
sleep 2
done
echo_error "Flask API failed to start within 60 seconds"
cat "$LOG_DIR/flask.log"
cd ..
return 1
}
# 启动RabbitMQ消费者
start_consumer() {
echo_info "Starting RabbitMQ Consumer..."
cd "$PYTHON_DIR"
# 设置环境变量
export FLASK_HOST=127.0.0.1
export FLASK_PORT=8081
export RABBITMQ_HOST=localhost
export RABBITMQ_PORT=5672
nohup python3 ocr_task_consumer.py > "$LOG_DIR/consumer.log" 2>&1 &
CONSUMER_PID=$!
echo $CONSUMER_PID > "$LOG_DIR/consumer.pid"
echo_info "RabbitMQ Consumer started (PID: $CONSUMER_PID)"
cd ..
}
# 启动Java应用
start_java_app() {
echo_info "Starting Java Spring Boot Application..."
if [ ! -f "$JAR_FILE" ]; then
echo_error "JAR file not found: $JAR_FILE"
echo_info "Please run: mvn clean package"
exit 1
fi
nohup java -jar "$JAR_FILE" > "$LOG_DIR/app.log" 2>&1 &
APP_PID=$!
echo $APP_PID > "$LOG_DIR/app.pid"
echo_info "Java Application started (PID: $APP_PID)"
echo_info "Waiting for application to be ready..."
# 等待应用启动
for i in {1..60}; do
if curl -s http://localhost:8080/report-detect-api/actuator/health > /dev/null 2>&1; then
echo_info "Application is ready!"
return 0
fi
sleep 2
done
echo_warn "Application may still be starting... check logs"
}
# 停止所有服务
stop_all() {
echo_info "Stopping all services..."
# 停止Java应用
if [ -f "$LOG_DIR/app.pid" ]; then
PID=$(cat "$LOG_DIR/app.pid")
if kill -0 $PID 2>/dev/null; then
echo_info "Stopping Java Application (PID: $PID)"
kill $PID
fi
rm -f "$LOG_DIR/app.pid"
fi
# 停止消费者
if [ -f "$LOG_DIR/consumer.pid" ]; then
PID=$(cat "$LOG_DIR/consumer.pid")
if kill -0 $PID 2>/dev/null; then
echo_info "Stopping RabbitMQ Consumer (PID: $PID)"
kill $PID
fi
rm -f "$LOG_DIR/consumer.pid"
fi
# 停止Flask
if [ -f "$LOG_DIR/flask.pid" ]; then
PID=$(cat "$LOG_DIR/flask.pid")
if kill -0 $PID 2>/dev/null; then
echo_info "Stopping Flask API (PID: $PID)"
kill $PID
fi
rm -f "$LOG_DIR/flask.pid"
fi
echo_info "All services stopped"
}
# 显示状态
show_status() {
echo_info "Service Status:"
echo ""
# Java应用
if [ -f "$LOG_DIR/app.pid" ]; then
PID=$(cat "$LOG_DIR/app.pid")
if kill -0 $PID 2>/dev/null; then
echo -e " ${GREEN}${NC} Java Application (PID: $PID)"
else
echo -e " ${RED}${NC} Java Application (PID file exists but process not running)"
fi
else
echo -e " ${RED}${NC} Java Application (not running)"
fi
# Flask
if [ -f "$LOG_DIR/flask.pid" ]; then
PID=$(cat "$LOG_DIR/flask.pid")
if kill -0 $PID 2>/dev/null; then
echo -e " ${GREEN}${NC} Flask API (PID: $PID)"
else
echo -e " ${RED}${NC} Flask API (PID file exists but process not running)"
fi
else
echo -e " ${RED}${NC} Flask API (not running)"
fi
# 消费者
if [ -f "$LOG_DIR/consumer.pid" ]; then
PID=$(cat "$LOG_DIR/consumer.pid")
if kill -0 $PID 2>/dev/null; then
echo -e " ${GREEN}${NC} RabbitMQ Consumer (PID: $PID)"
else
echo -e " ${RED}${NC} RabbitMQ Consumer (PID file exists but process not running)"
fi
else
echo -e " ${RED}${NC} RabbitMQ Consumer (not running)"
fi
echo ""
}
# 主函数
main() {
case "$1" in
start)
check_dependencies
start_rabbitmq
start_flask
start_consumer
start_java_app
echo ""
echo_info "All services started successfully!"
echo_info "Logs are in $LOG_DIR/"
echo ""
show_status
;;
stop)
stop_all
;;
restart)
stop_all
sleep 3
main start
;;
status)
show_status
;;
logs)
if [ -f "$LOG_DIR/app.log" ]; then
tail -f "$LOG_DIR/app.log"
else
echo_error "No log files found"
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status|logs}"
echo ""
echo "Commands:"
echo " start - Start all services"
echo " stop - Stop all services"
echo " restart - Restart all services"
echo " status - Show service status"
echo " logs - Tail application logs"
exit 1
;;
esac
}
main "$@"