LLMX/scripts/entrypoint.sh
gewuyou 8b17f6cb84 feat(core): 重构项目并添加 Docker 支持
- 重命名应用名称和包名,统一使用 llmx 前缀- 添加生产环境和测试环境的 Docker 配置文件
- 新增环境变量配置,用于 Docker部署
- 更新构建脚本,支持多模块构建
- 优化应用配置,适配 Docker 环境
2025-05-07 20:43:05 +08:00

100 lines
2.6 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
#set -x # 调试模式,可以启用以打印每行脚本的执行情况
# 设置默认的等待时间间隔默认值为2秒
: "${SLEEP_SECOND:=2}"
# 设置默认的超时时间默认值为60秒
: "${TIMEOUT:=60}"
# 带时间戳+emoji的小型日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
}
# 等待服务函数
wait_for() {
local host="$1"
local port="$2"
local timeout="$TIMEOUT"
local start_time
local current_time
local elapsed_time
local attempt=0
start_time=$(date +%s)
log "🔄 开始等待依赖服务 $host:$port 可用 (超时时间: ${timeout}s, 间隔: ${SLEEP_SECOND}s)"
while ! nc -z "$host" "$port" 2>/dev/null; do
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))
attempt=$((attempt + 1))
if [ "$elapsed_time" -ge "$timeout" ]; then
log "🛑 [ERROR] 等待超时!依赖服务 $host:$port${timeout}s 内未启动"
return 1
fi
log "🔄 第 ${attempt} 次检测:$host:$port 未就绪,已等待 ${elapsed_time}s..."
sleep "$SLEEP_SECOND"
done
total_time=$(( $(date +%s) - start_time ))
log "✅ [SUCCESS] 依赖服务 $host:$port 已启动,耗时 ${total_time}s"
return 0
}
# 声明变量
declare DEPENDS
declare CMD
# 解析参数
while getopts "d:c:" arg; do
case "$arg" in
d)
DEPENDS="$OPTARG"
;;
c)
CMD="$OPTARG"
;;
?)
log "🛑 [ERROR] 未知参数"
exit 1
;;
esac
done
# 检查依赖
if [ -n "$DEPENDS" ]; then
log "📦 检测到依赖服务列表: $DEPENDS"
for var in ${DEPENDS//,/ }; do
host=${var%:*}
port=${var#*:}
if [ -z "$host" ] || [ -z "$port" ]; then
log "🛑 [ERROR] 依赖项格式错误: $var,应为 host:port"
exit 1
fi
if ! wait_for "$host" "$port"; then
log "❌ [ERROR] 依赖服务 $host:$port 启动失败,终止执行"
exit 1
fi
done
else
log "⚡️ 未配置依赖服务,跳过依赖检测"
fi
# 执行命令
if [ -n "$CMD" ]; then
log "🚀 准备执行命令: $CMD"
eval "$CMD"
cmd_exit_code=$?
if [ $cmd_exit_code -eq 0 ]; then
log "✅ [SUCCESS] 命令执行完成,退出码: $cmd_exit_code"
else
log "❌ [ERROR] 命令执行失败,退出码: $cmd_exit_code"
exit $cmd_exit_code
fi
else
log "⚠️ [WARNING] 未指定要执行的命令,脚本结束"
fi