diff --git a/.gitignore b/.gitignore index 0fb6c51..06dd747 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,10 @@ gateway/config.json # worker 配置(含鉴权密码,不入 git) worker_config.json +# worker 运行期文件(PID / 日志) +worker.pid +worker.log + # SegFormer 模型权重(~323MB,体积过大,不入 git,见 OFFLINE_ASSETS.md) hairline/models/face-parsing/model.safetensors diff --git a/start.sh b/start.sh index 6c1ac08..68e3039 100755 --- a/start.sh +++ b/start.sh @@ -1,4 +1,67 @@ -#!/bin/bash -# worker 启动脚本:监听 0.0.0.0:8187(防火墙仅放行网关 IP)。 +#!/usr/bin/env bash +# worker 服务手动开关(接口1 四庭七眼测量)。 +# +# 用法: +# ./start.sh # = start,后台启动 +# ./start.sh start +# ./start.sh stop +# ./start.sh restart +# ./start.sh status +# HOST=127.0.0.1 PORT=8187 ./start.sh # 用环境变量覆盖监听地址 +# +# 后台运行:日志写 worker.log,PID 写 worker.pid(均已 gitignore)。 +# 开发时若想前台+热重载,用 ./run_worker.sh。 +set -uo pipefail cd "$(dirname "$0")" -exec ./venv/bin/uvicorn app:app --host 0.0.0.0 --port 8187 + +HOST="${HOST:-0.0.0.0}" +PORT="${PORT:-8187}" +PIDFILE="worker.pid" +LOGFILE="worker.log" +UVICORN="./venv/bin/uvicorn" + +is_running() { + [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null +} + +start() { + if is_running; then + echo "已在运行 (PID $(cat "$PIDFILE")),监听 ${HOST}:${PORT}" + return 0 + fi + rm -f "$PIDFILE" + if [ ! -x "$UVICORN" ]; then + echo "找不到 $UVICORN,请先创建 venv 并安装依赖" >&2 + exit 1 + fi + nohup "$UVICORN" app:app --host "$HOST" --port "$PORT" >> "$LOGFILE" 2>&1 & + echo $! > "$PIDFILE" + echo "已启动 (PID $!),监听 ${HOST}:${PORT}" + echo "日志: tail -f $LOGFILE 就绪后 /health 返回 200" +} + +stop() { + if is_running; then + kill "$(cat "$PIDFILE")" && rm -f "$PIDFILE" + echo "已停止" + else + echo "未在运行" + rm -f "$PIDFILE" + fi +} + +status() { + if is_running; then + echo "运行中 (PID $(cat "$PIDFILE")),监听 ${HOST}:${PORT}" + else + echo "未运行" + fi +} + +case "${1:-start}" in + start) start ;; + stop) stop ;; + restart) stop; sleep 1; start ;; + status) status ;; + *) echo "用法: $0 {start|stop|restart|status}"; exit 1 ;; +esac