From 023bb2e6fd1d761257da4954ace276a17e51513c Mon Sep 17 00:00:00 2001 From: xsl Date: Sun, 14 Jun 2026 17:47:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(worker):=20start.sh=20=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E5=90=8E=E5=8F=B0=E6=9C=8D=E5=8A=A1=E5=BC=80=E5=85=B3(start/st?= =?UTF-8?q?op/restart/status)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ./start.sh 现支持 start/stop/restart/status 子命令,无参默认 start - 后台运行 + PID 文件(worker.pid) + 日志(worker.log),手动控制开关 - 前台热重载开发仍用 run_worker.sh - gitignore 补 worker.pid / worker.log Co-Authored-By: Claude Opus 4.8 --- .gitignore | 4 ++++ start.sh | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 3 deletions(-) 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