Files
hair/run_worker.sh
T
2026-06-16 21:16:44 +08:00

55 lines
1.9 KiB
Bash
Executable File
Raw 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.
#!/usr/bin/env bash
# 启动 GPU worker(接口1 四庭七眼 / 接口2 发际线等)。
# 默认:后台运行,绑定 0.0.0.0:8187,日志写到 worker.logPID 写到 worker.pid。
# 每次开机手动执行一次即可。停止用 ./stop_worker.sh。
#
# 可用环境变量覆盖:
# HOST=127.0.0.1 PORT=9000 ./run_worker.sh
# FOREGROUND=1 ./run_worker.sh # 前台运行(调试用,Ctrl+C 退出)
#
# 鉴权密码读 worker_config.json 的 accept_passwords
# 也可用 WORKER_ACCEPT_PASSWORDS=逗号分隔 覆盖。
set -euo pipefail
cd "$(dirname "$0")"
VENV_UVICORN="./venv/bin/uvicorn"
PID_FILE="worker.pid"
LOG_FILE="worker.log"
HOST="${HOST:-0.0.0.0}"
PORT="${PORT:-8187}"
# ---- 临时取消上传图片限制(分辨率 + 文件大小)----
# 0 = 不限制。要恢复默认(600/800/1MB),删掉下面三行即可。
export MIN_SHORT_SIDE="${MIN_SHORT_SIDE:-0}"
export MIN_LONG_SIDE="${MIN_LONG_SIDE:-0}"
export MAX_FILE_BYTES="${MAX_FILE_BYTES:-0}"
# ------------------------------------------------
if [ ! -x "$VENV_UVICORN" ]; then
echo "找不到 $VENV_UVICORN,请先创建 venv 并安装依赖(见 requirements.txt" >&2
exit 1
fi
# 已在运行则不重复启动
if [ -f "$PID_FILE" ] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null; then
echo "worker 已在运行 (PID $(cat "$PID_FILE")),端口 ${PORT}。如需重启请先 ./stop_worker.sh" >&2
exit 0
fi
if [ "${FOREGROUND:-0}" = "1" ]; then
echo ">> 前台运行 worker: ${HOST}:${PORT}"
exec "$VENV_UVICORN" app:app --host "$HOST" --port "$PORT"
fi
echo ">> 后台启动 worker: ${HOST}:${PORT}"
nohup "$VENV_UVICORN" app:app --host "$HOST" --port "$PORT" >> "$LOG_FILE" 2>&1 &
echo $! > "$PID_FILE"
sleep 1
if kill -0 "$(cat "$PID_FILE")" 2>/dev/null; then
echo ">> 已启动,PID $(cat "$PID_FILE"),日志:$LOG_FILE"
else
echo ">> 启动失败,请查看 $LOG_FILE" >&2
rm -f "$PID_FILE"
exit 1
fi