Blame view

scripts/service_ctl.sh 31.8 KB
d1d356f8   tangwang   脚本优化
1
2
3
  #!/bin/bash
  #
  # Unified service lifecycle controller for saas-search.
7913e2fb   tangwang   服务管理和监控
4
  # Supports: up / down / start / stop / restart / status / monitor / monitor-start / monitor-stop / monitor-status
d1d356f8   tangwang   脚本优化
5
6
7
8
9
10
  #
  
  set -euo pipefail
  
  PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
  LOG_DIR="${PROJECT_ROOT}/logs"
28e57bb1   tangwang   日志体系优化
11
  LOG_RETENTION_DAYS="${LOG_RETENTION_DAYS:-30}"
d1d356f8   tangwang   脚本优化
12
13
14
  
  mkdir -p "${LOG_DIR}"
  
c7e80cc2   tangwang   新的 .env 管理机制如下:
15
16
17
  # shellcheck source=scripts/lib/load_env.sh
  source "${PROJECT_ROOT}/scripts/lib/load_env.sh"
  
7b8d9e1a   tangwang   评估框架的启动脚本
18
  CORE_SERVICES=("backend" "indexer" "frontend" "eval-web")
9f33fe3c   tangwang   fix suggestion re...
19
20
  # reranker-fine 暂时不用,因此暂时从OPTIONAL_SERVICES中删除
  OPTIONAL_SERVICES=("tei" "cnclip" "embedding" "embedding-image" "translator" "reranker")
7913e2fb   tangwang   服务管理和监控
21
  FULL_SERVICES=("${OPTIONAL_SERVICES[@]}" "${CORE_SERVICES[@]}")
9f33fe3c   tangwang   fix suggestion re...
22
  STOP_ORDER_SERVICES=("frontend" "eval-web" "indexer" "backend" "reranker" "translator" "embedding-image" "embedding" "cnclip" "tei")
0ba0e0fc   tangwang   1. rerank漏斗配置优化
23
  declare -Ag SERVICE_ENABLED_CACHE=()
d1d356f8   tangwang   脚本优化
24
25
  
  all_services() {
7913e2fb   tangwang   服务管理和监控
26
    echo "${FULL_SERVICES[@]}"
d1d356f8   tangwang   脚本优化
27
28
  }
  
daa2690b   tangwang   漏斗参数调优&呈现优化
29
30
31
32
33
34
35
36
  config_python_bin() {
    if [ -x "${PROJECT_ROOT}/.venv/bin/python" ]; then
      echo "${PROJECT_ROOT}/.venv/bin/python"
    else
      echo "${PYTHON:-python3}"
    fi
  }
  
0ba0e0fc   tangwang   1. rerank漏斗配置优化
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  service_enabled_by_config() {
    local service="$1"
    case "${service}" in
      reranker|reranker-fine|translator)
        ;;
      *)
        return 0
        ;;
    esac
  
    if [ -n "${SERVICE_ENABLED_CACHE[${service}]+x}" ]; then
      [ "${SERVICE_ENABLED_CACHE[${service}]}" = "1" ]
      return
    fi
  
    local pybin
    pybin="$(config_python_bin)"
  
    local enabled
    if ! enabled="$(
      SERVICE_NAME="${service}" \
      PYTHONPATH="${PROJECT_ROOT}${PYTHONPATH:+:${PYTHONPATH}}" \
      "${pybin}" - <<'PY'
  from config.loader import get_app_config
  import os
  
  service = os.environ["SERVICE_NAME"]
  cfg = get_app_config()
  
  enabled = True
  if service == "reranker":
      enabled = bool(cfg.search.rerank.enabled)
  elif service == "reranker-fine":
      enabled = bool(cfg.search.fine_rank.enabled)
  elif service == "translator":
      capabilities = dict(cfg.services.translation.capabilities or {})
      enabled = any(bool((value or {}).get("enabled", True)) for value in capabilities.values())
  
  print("1" if enabled else "0")
  PY
    )"; then
      echo "[warn] failed to read config state for ${service}; defaulting to enabled" >&2
      enabled="1"
    fi
  
    SERVICE_ENABLED_CACHE["${service}"]="${enabled}"
    [ "${enabled}" = "1" ]
  }
  
  filter_disabled_targets() {
    local targets="$1"
    local verbose="${2:-quiet}"
    local out=""
    local svc
  
    for svc in ${targets}; do
      if service_enabled_by_config "${svc}"; then
        out="${out} ${svc}"
      elif [ "${verbose}" = "verbose" ]; then
        echo "[skip] ${svc} disabled by config" >&2
      fi
    done
  
    echo "${out# }"
  }
  
daa2690b   tangwang   漏斗参数调优&呈现优化
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  reranker_instance_for_service() {
    local service="$1"
    case "${service}" in
      reranker) echo "default" ;;
      reranker-fine) echo "fine" ;;
      *) echo "" ;;
    esac
  }
  
  get_reranker_instance_port() {
    local instance="$1"
    local pybin
    pybin="$(config_python_bin)"
    RERANK_INSTANCE="${instance}" PYTHONPATH="${PROJECT_ROOT}${PYTHONPATH:+:${PYTHONPATH}}" "${pybin}" - <<'PY'
  from config.loader import get_app_config
  import os
  
  cfg = get_app_config().services.rerank
  name = (os.getenv("RERANK_INSTANCE") or cfg.default_instance).strip() or cfg.default_instance
  print(cfg.get_instance(name).port)
  PY
  }
  
d1d356f8   tangwang   脚本优化
126
127
128
129
130
131
  get_port() {
    local service="$1"
    case "${service}" in
      backend) echo "${API_PORT:-6002}" ;;
      indexer) echo "${INDEXER_PORT:-6004}" ;;
      frontend) echo "${FRONTEND_PORT:-6003}" ;;
af03fdef   tangwang   embedding模块代码整理
132
      embedding) echo "${EMBEDDING_TEXT_PORT:-6005}" ;;
7214c2e7   tangwang   mplemented**
133
      embedding-image) echo "${EMBEDDING_IMAGE_PORT:-6008}" ;;
af7ee060   tangwang   service_ctl 简化为“显...
134
      translator) echo "${TRANSLATION_PORT:-6006}" ;;
daa2690b   tangwang   漏斗参数调优&呈现优化
135
136
137
138
139
140
141
142
143
144
145
146
147
148
      reranker)
        if [ -n "${RERANKER_PORT:-}" ]; then
          echo "${RERANKER_PORT}"
        else
          get_reranker_instance_port "default"
        fi
        ;;
      reranker-fine)
        if [ -n "${RERANKER_FINE_PORT:-}" ]; then
          echo "${RERANKER_FINE_PORT}"
        else
          get_reranker_instance_port "fine"
        fi
        ;;
07cf5a93   tangwang   START_EMBEDDING=...
149
      tei) echo "${TEI_PORT:-8080}" ;;
d1d356f8   tangwang   脚本优化
150
      cnclip) echo "${CNCLIP_PORT:-51000}" ;;
7b8d9e1a   tangwang   评估框架的启动脚本
151
      eval-web) echo "${EVAL_WEB_PORT:-6010}" ;;
d1d356f8   tangwang   脚本优化
152
153
154
155
156
157
      *) echo "" ;;
    esac
  }
  
  pid_file() {
    local service="$1"
af7ee060   tangwang   service_ctl 简化为“显...
158
    echo "${LOG_DIR}/${service}.pid"
d1d356f8   tangwang   脚本优化
159
160
161
162
163
164
165
  }
  
  log_file() {
    local service="$1"
    echo "${LOG_DIR}/${service}.log"
  }
  
28e57bb1   tangwang   日志体系优化
166
167
168
169
170
171
172
173
174
175
  prepare_daily_log_target() {
    local service="$1"
    local day
    local today_file
    day="$(date +%F)"
    today_file="${LOG_DIR}/${service}-${day}.log"
    touch "${today_file}"
    ln -sfn "$(basename "${today_file}")" "$(log_file "${service}")"
  }
  
d1d356f8   tangwang   脚本优化
176
177
178
179
180
181
  service_start_cmd() {
    local service="$1"
    case "${service}" in
      backend) echo "./scripts/start_backend.sh" ;;
      indexer) echo "./scripts/start_indexer.sh" ;;
      frontend) echo "./scripts/start_frontend.sh" ;;
7214c2e7   tangwang   mplemented**
182
183
      embedding) echo "./scripts/start_embedding_text_service.sh" ;;
      embedding-image) echo "./scripts/start_embedding_image_service.sh" ;;
d1d356f8   tangwang   脚本优化
184
185
      translator) echo "./scripts/start_translator.sh" ;;
      reranker) echo "./scripts/start_reranker.sh" ;;
daa2690b   tangwang   漏斗参数调优&呈现优化
186
      reranker-fine) echo "./scripts/start_reranker.sh" ;;
07cf5a93   tangwang   START_EMBEDDING=...
187
      tei) echo "./scripts/start_tei_service.sh" ;;
d1d356f8   tangwang   脚本优化
188
      cnclip) echo "./scripts/start_cnclip_service.sh" ;;
7b8d9e1a   tangwang   评估框架的启动脚本
189
      eval-web) echo "./scripts/start_eval_web.sh" ;;
d1d356f8   tangwang   脚本优化
190
191
192
193
      *) return 1 ;;
    esac
  }
  
af7ee060   tangwang   service_ctl 简化为“显...
194
195
196
  service_exists() {
    local service="$1"
    case "${service}" in
7b8d9e1a   tangwang   评估框架的启动脚本
197
      backend|indexer|frontend|eval-web|embedding|embedding-image|translator|reranker|reranker-fine|tei|cnclip) return 0 ;;
af7ee060   tangwang   service_ctl 简化为“显...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
      *) return 1 ;;
    esac
  }
  
  validate_targets() {
    local targets="$1"
    for svc in ${targets}; do
      if ! service_exists "${svc}"; then
        echo "[error] unknown service: ${svc}" >&2
        return 1
      fi
    done
  }
  
7913e2fb   tangwang   服务管理和监控
212
213
214
  health_path_for_service() {
    local service="$1"
    case "${service}" in
daa2690b   tangwang   漏斗参数调优&呈现优化
215
      backend|indexer|embedding|embedding-image|translator|reranker|reranker-fine|tei) echo "/health" ;;
dba57642   tangwang   bayes调参计划
216
      eval-web) echo "/api/history" ;;
7913e2fb   tangwang   服务管理和监控
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
      *) echo "" ;;
    esac
  }
  
  monitor_log_file() {
    echo "${LOG_DIR}/service-monitor.log"
  }
  
  monitor_pid_file() {
    echo "${LOG_DIR}/service-monitor.pid"
  }
  
  monitor_targets_file() {
    echo "${LOG_DIR}/service-monitor.targets"
  }
  
a7bb846c   tangwang   monitor
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  sync_monitor_daemon_state() {
    local pf
    pf="$(monitor_pid_file)"
    local tf
    tf="$(monitor_targets_file)"
  
    if [ ! -f "${pf}" ]; then
      return 1
    fi
  
    local pid
    pid="$(cat "${pf}" 2>/dev/null || true)"
    if [ -n "${pid}" ] && kill -0 "${pid}" 2>/dev/null; then
      return 0
    fi
  
    rm -f "${pf}" "${tf}"
    return 1
  }
  
7913e2fb   tangwang   服务管理和监控
253
254
255
256
257
258
  monitor_current_targets() {
    if [ -f "$(monitor_targets_file)" ]; then
      cat "$(monitor_targets_file)" 2>/dev/null || true
    fi
  }
  
a7bb846c   tangwang   monitor
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
  merge_targets() {
    local base="${1:-}"
    local extra="${2:-}"
    local merged=""
    merged="$(normalize_targets "${base} ${extra}")"
    if [ -n "${merged}" ]; then
      merged="$(apply_target_order monitor "${merged}")"
    fi
    echo "${merged}"
  }
  
  subtract_targets() {
    local base="${1:-}"
    local remove="${2:-}"
    local out=""
    local svc
    declare -A removed=()
  
    for svc in ${remove}; do
      removed["${svc}"]=1
    done
  
    for svc in ${base}; do
      if [ "${removed[${svc}]:-0}" != "1" ]; then
        out="${out} ${svc}"
      fi
    done
  
    out="${out# }"
    if [ -n "${out}" ]; then
      out="$(normalize_targets "${out}")"
      out="$(apply_target_order monitor "${out}")"
    fi
    echo "${out}"
  }
  
7913e2fb   tangwang   服务管理和监控
295
296
297
298
299
300
301
  monitor_log_event() {
    local service="$1"
    local level="$2"
    local message="$3"
    local ts line
    ts="$(date '+%F %T')"
    line="[${ts}] [${level}] [${service}] ${message}"
a7bb846c   tangwang   monitor
302
303
304
305
306
    if [ -t 1 ]; then
      echo "${line}" | tee -a "$(monitor_log_file)"
    else
      echo "${line}" >> "$(monitor_log_file)"
    fi
7913e2fb   tangwang   服务管理和监控
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
  }
  
  require_positive_int() {
    local name="$1"
    local value="$2"
    if ! [[ "${value}" =~ ^[0-9]+$ ]] || [ "${value}" -le 0 ]; then
      echo "[error] invalid ${name}=${value}, must be a positive integer" >&2
      return 1
    fi
  }
  
  service_healthy_now() {
    local service="$1"
    local port
    local path
  
    if [ "${service}" = "tei" ]; then
      port="$(get_port "${service}")"
      is_running_tei_container &&
        curl -sf "http://127.0.0.1:${port}/health" >/dev/null 2>&1
      return
    fi
  
    if [ "${service}" = "cnclip" ]; then
      is_running_by_pid "${service}" || is_running_by_port "${service}"
      return
    fi
  
    port="$(get_port "${service}")"
    path="$(health_path_for_service "${service}")"
a7bb846c   tangwang   monitor
337
    if [ -z "${port}" ]; then
7913e2fb   tangwang   服务管理和监控
338
339
      return 1
    fi
a7bb846c   tangwang   monitor
340
341
342
343
    if [ -z "${path}" ]; then
      is_running_by_pid "${service}" || is_running_by_port "${service}"
      return
    fi
7913e2fb   tangwang   服务管理和监控
344
345
346
347
348
349
    if ! is_running_by_port "${service}"; then
      return 1
    fi
    curl -sf "http://127.0.0.1:${port}${path}" >/dev/null 2>&1
  }
  
d1d356f8   tangwang   脚本优化
350
351
352
353
354
355
  wait_for_health() {
    local service="$1"
    local max_retries="${2:-30}"
    local interval_sec="${3:-1}"
    local port
    port="$(get_port "${service}")"
7913e2fb   tangwang   服务管理和监控
356
357
358
359
360
    local path
    path="$(health_path_for_service "${service}")"
    if [ -z "${path}" ]; then
      return 0
    fi
d1d356f8   tangwang   脚本优化
361
362
363
364
365
366
367
368
369
370
371
372
  
    local i=0
    while [ "${i}" -lt "${max_retries}" ]; do
      if curl -sf "http://127.0.0.1:${port}${path}" >/dev/null 2>&1; then
        return 0
      fi
      i=$((i + 1))
      sleep "${interval_sec}"
    done
    return 1
  }
  
9f5994b4   tangwang   reranker
373
374
375
376
377
378
  wait_for_stable_health() {
    local service="$1"
    local checks="${2:-3}"
    local interval_sec="${3:-1}"
    local port
    port="$(get_port "${service}")"
7913e2fb   tangwang   服务管理和监控
379
380
381
382
383
    local path
    path="$(health_path_for_service "${service}")"
    if [ -z "${path}" ]; then
      return 0
    fi
9f5994b4   tangwang   reranker
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
  
    local i=0
    while [ "${i}" -lt "${checks}" ]; do
      if ! is_running_by_port "${service}"; then
        return 1
      fi
      if ! curl -sf "http://127.0.0.1:${port}${path}" >/dev/null 2>&1; then
        return 1
      fi
      i=$((i + 1))
      sleep "${interval_sec}"
    done
    return 0
  }
  
7913e2fb   tangwang   服务管理和监控
399
400
401
402
403
404
  monitor_services() {
    local targets="$1"
    local interval_sec="${MONITOR_INTERVAL_SEC:-10}"
    local fail_threshold="${MONITOR_FAIL_THRESHOLD:-3}"
    local restart_cooldown_sec="${MONITOR_RESTART_COOLDOWN_SEC:-30}"
    local max_restarts_per_hour="${MONITOR_MAX_RESTARTS_PER_HOUR:-6}"
32e9b30c   tangwang   scripts/ 根目录主要保留启...
405
    local wechat_alert_py="${PROJECT_ROOT}/scripts/ops/wechat_alert.py"
7913e2fb   tangwang   服务管理和监控
406
407
408
409
410
411
412
413
  
    require_positive_int "MONITOR_INTERVAL_SEC" "${interval_sec}"
    require_positive_int "MONITOR_FAIL_THRESHOLD" "${fail_threshold}"
    require_positive_int "MONITOR_RESTART_COOLDOWN_SEC" "${restart_cooldown_sec}"
    require_positive_int "MONITOR_MAX_RESTARTS_PER_HOUR" "${max_restarts_per_hour}"
  
    touch "$(monitor_log_file)"
  
a7bb846c   tangwang   monitor
414
415
416
417
418
419
420
421
422
423
424
    if [ "${MONITOR_DAEMON:-0}" = "1" ]; then
      echo "$$" > "$(monitor_pid_file)"
      echo "${targets}" > "$(monitor_targets_file)"
      trap '
        current_pid="$(cat "$(monitor_pid_file)" 2>/dev/null || true)"
        if [ "${current_pid}" = "$$" ]; then
          rm -f "$(monitor_pid_file)" "$(monitor_targets_file)"
        fi
      ' EXIT
    fi
  
7913e2fb   tangwang   服务管理和监控
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
    declare -A fail_streak=()
    declare -A last_restart_epoch=()
    declare -A restart_history=()
  
    monitor_log_event "monitor" "info" "started targets=[${targets}] interval=${interval_sec}s fail_threshold=${fail_threshold} cooldown=${restart_cooldown_sec}s max_restarts_per_hour=${max_restarts_per_hour}"
    trap 'monitor_log_event "monitor" "info" "received stop signal, exiting"; exit 0' INT TERM
  
    while true; do
      local svc
      for svc in ${targets}; do
        if service_healthy_now "${svc}"; then
          if [ "${fail_streak[${svc}]:-0}" -gt 0 ]; then
            monitor_log_event "${svc}" "info" "health recovered after ${fail_streak[${svc}]} consecutive failures"
          fi
          fail_streak["${svc}"]=0
          continue
        fi
  
        fail_streak["${svc}"]=$(( ${fail_streak[${svc}]:-0} + 1 ))
        monitor_log_event "${svc}" "warn" "health check failed (${fail_streak[${svc}]}/${fail_threshold})"
  
        if [ "${fail_streak[${svc}]}" -lt "${fail_threshold}" ]; then
          continue
        fi
  
        local now
        now="$(date +%s)"
        local last
        last="${last_restart_epoch[${svc}]:-0}"
        if [ $((now - last)) -lt "${restart_cooldown_sec}" ]; then
          monitor_log_event "${svc}" "warn" "restart suppressed by cooldown (${restart_cooldown_sec}s)"
          continue
        fi
  
        local t
        local recent_history=""
        local recent_count=0
        for t in ${restart_history[${svc}]:-}; do
          if [ $((now - t)) -lt 3600 ]; then
            recent_history="${recent_history} ${t}"
            recent_count=$((recent_count + 1))
          fi
        done
        restart_history["${svc}"]="${recent_history# }"
  
        if [ "${recent_count}" -ge "${max_restarts_per_hour}" ]; then
          monitor_log_event "${svc}" "error" "restart suppressed by hourly cap (${max_restarts_per_hour}/hour)"
2260eed2   tangwang   推送报警到微信群webhook
472
          if [ -x "${wechat_alert_py}" ] || [ -f "${wechat_alert_py}" ]; then
dba57642   tangwang   bayes调参计划
473
            "$(config_python_bin)" "${wechat_alert_py}" \
2260eed2   tangwang   推送报警到微信群webhook
474
475
476
477
              --service "${svc}" \
              --level "error" \
              --message "监控检测到服务连续多次健康检查失败,且已达到每小时最大重启次数上限(${max_restarts_per_hour} 次/小时),请及时排查。"
          fi
7913e2fb   tangwang   服务管理和监控
478
479
480
481
          continue
        fi
  
        monitor_log_event "${svc}" "error" "triggering restart after ${fail_streak[${svc}]} consecutive failures"
2260eed2   tangwang   推送报警到微信群webhook
482
        if [ -x "${wechat_alert_py}" ] || [ -f "${wechat_alert_py}" ]; then
dba57642   tangwang   bayes调参计划
483
          "$(config_python_bin)" "${wechat_alert_py}" \
2260eed2   tangwang   推送报警到微信群webhook
484
485
486
487
            --service "${svc}" \
            --level "error" \
            --message "监控检测到服务连续 ${fail_streak[${svc}]} 次健康检查失败,正在尝试自动重启。"
        fi
7913e2fb   tangwang   服务管理和监控
488
489
490
491
492
493
494
495
496
        if stop_one "${svc}" && start_one "${svc}"; then
          fail_streak["${svc}"]=0
          last_restart_epoch["${svc}"]="${now}"
          restart_history["${svc}"]="${restart_history[${svc}]:-} ${now}"
          monitor_log_event "${svc}" "info" "restart succeeded"
        else
          last_restart_epoch["${svc}"]="${now}"
          restart_history["${svc}"]="${restart_history[${svc}]:-} ${now}"
          monitor_log_event "${svc}" "error" "restart failed, inspect $(log_file "${svc}")"
2260eed2   tangwang   推送报警到微信群webhook
497
          if [ -x "${wechat_alert_py}" ] || [ -f "${wechat_alert_py}" ]; then
dba57642   tangwang   bayes调参计划
498
            "$(config_python_bin)" "${wechat_alert_py}" \
2260eed2   tangwang   推送报警到微信群webhook
499
500
501
502
              --service "${svc}" \
              --level "error" \
              --message "监控检测到服务连续 ${fail_streak[${svc}]} 次健康检查失败,自动重启尝试失败,请尽快登录服务器查看日志:$(log_file "${svc}")."
          fi
7913e2fb   tangwang   服务管理和监控
503
504
505
506
507
508
509
        fi
      done
      sleep "${interval_sec}"
    done
  }
  
  is_monitor_daemon_running() {
a7bb846c   tangwang   monitor
510
    sync_monitor_daemon_state
7913e2fb   tangwang   服务管理和监控
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
  }
  
  stop_monitor_daemon() {
    local pf
    pf="$(monitor_pid_file)"
    local tf
    tf="$(monitor_targets_file)"
  
    if ! is_monitor_daemon_running; then
      rm -f "${pf}"
      return 0
    fi
  
    local pid
    pid="$(cat "${pf}" 2>/dev/null || true)"
    if [ -n "${pid}" ] && kill -0 "${pid}" 2>/dev/null; then
      echo "[stop] monitor daemon pid=${pid}"
      kill -TERM "${pid}" 2>/dev/null || true
      sleep 1
      if kill -0 "${pid}" 2>/dev/null; then
        kill -KILL "${pid}" 2>/dev/null || true
      fi
    fi
    rm -f "${pf}" "${tf}"
  }
  
  start_monitor_daemon() {
    local targets="$1"
0ba0e0fc   tangwang   1. rerank漏斗配置优化
539
540
541
542
543
544
545
546
547
548
    if [ -z "${targets}" ]; then
      if is_monitor_daemon_running; then
        echo "[info] no enabled services to monitor; stopping monitor daemon"
        stop_monitor_daemon
      else
        echo "[info] no enabled services to monitor"
      fi
      return 0
    fi
  
7913e2fb   tangwang   服务管理和监控
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
    local pf
    pf="$(monitor_pid_file)"
    local tf
    tf="$(monitor_targets_file)"
  
    local current_targets
    current_targets="$(monitor_current_targets)"
    if is_monitor_daemon_running; then
      if [ "${current_targets}" = "${targets}" ]; then
        echo "[skip] monitor daemon already running (targets=[${targets}])"
        return 0
      fi
      echo "[info] monitor daemon targets changed: [${current_targets}] -> [${targets}]"
      stop_monitor_daemon
    fi
  
    echo "${targets}" > "${tf}"
a7bb846c   tangwang   monitor
566
    MONITOR_DAEMON=1 nohup "${PROJECT_ROOT}/scripts/service_ctl.sh" monitor ${targets} >> "$(monitor_log_file)" 2>&1 &
7913e2fb   tangwang   服务管理和监控
567
568
    local pid=$!
    echo "${pid}" > "${pf}"
a7bb846c   tangwang   monitor
569
570
571
572
573
574
    sleep 1
    if ! kill -0 "${pid}" 2>/dev/null; then
      rm -f "${pf}" "${tf}"
      echo "[error] monitor daemon failed to stay alive, inspect $(monitor_log_file)" >&2
      return 1
    fi
7913e2fb   tangwang   服务管理和监控
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
    echo "[ok] monitor daemon started (pid=${pid}, targets=[${targets}], log=$(monitor_log_file))"
  }
  
  monitor_daemon_status() {
    local running="no"
    local pid="-"
    local targets="-"
  
    if is_monitor_daemon_running; then
      running="yes"
      pid="$(cat "$(monitor_pid_file)" 2>/dev/null || echo "-")"
      targets="$(monitor_current_targets)"
      [ -z "${targets}" ] && targets="-"
    fi
  
    printf "%-14s running=%-3s pid=%-8s targets=%s\n" "service-monitor" "${running}" "${pid}" "${targets}"
  }
  
d1d356f8   tangwang   脚本优化
593
594
595
596
597
598
599
600
601
  is_running_by_pid() {
    local service="$1"
    local pf
    pf="$(pid_file "${service}")"
    if [ ! -f "${pf}" ]; then
      return 1
    fi
    local pid
    pid="$(cat "${pf}" 2>/dev/null || true)"
a7bb846c   tangwang   monitor
602
603
604
605
606
    if [ -n "${pid}" ] && kill -0 "${pid}" 2>/dev/null; then
      return 0
    fi
    rm -f "${pf}"
    return 1
d1d356f8   tangwang   脚本优化
607
608
609
610
611
612
  }
  
  is_running_by_port() {
    local service="$1"
    local port
    port="$(get_port "${service}")"
dba57642   tangwang   bayes调参计划
613
614
615
616
617
618
619
    [ -n "${port}" ] && lsof -nP -iTCP:"${port}" -sTCP:LISTEN -t >/dev/null 2>&1
  }
  
  list_listen_pids_by_port() {
    local port="$1"
    [ -n "${port}" ] || return 0
    lsof -nP -iTCP:"${port}" -sTCP:LISTEN -t 2>/dev/null || true
d1d356f8   tangwang   脚本优化
620
621
  }
  
07cf5a93   tangwang   START_EMBEDDING=...
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
  is_running_tei_container() {
    local tei_name="${TEI_CONTAINER_NAME:-saas-search-tei}"
    local cid
    cid="$(docker ps -q -f name=^/${tei_name}$ 2>/dev/null || true)"
    [ -n "${cid}" ]
  }
  
  get_cnclip_flow_device() {
    local flow_file="${PROJECT_ROOT}/third-party/clip-as-service/server/torch-flow-temp.yml"
    if [ ! -f "${flow_file}" ]; then
      return 1
    fi
    sed -n "s/^[[:space:]]*device:[[:space:]]*'\\([^']*\\)'.*/\\1/p" "${flow_file}" | head -n 1
  }
  
a7bb846c   tangwang   monitor
637
638
639
  start_health_retries_for_service() {
    local service="$1"
    case "${service}" in
daa2690b   tangwang   漏斗参数调优&呈现优化
640
      reranker|reranker-fine) echo 90 ;;
a7bb846c   tangwang   monitor
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
      *) echo 30 ;;
    esac
  }
  
  wait_for_startup_health() {
    local service="$1"
    local pid="$2"
    local lf="$3"
    local retries
    retries="$(start_health_retries_for_service "${service}")"
  
    if wait_for_health "${service}" "${retries}"; then
      if wait_for_stable_health "${service}" 5 1; then
        echo "[ok] ${service} healthy (pid=${pid}, log=${lf})"
        return 0
      fi
      echo "[error] ${service} became unavailable right after startup, inspect ${lf}" >&2
      return 1
    fi
  
    echo "[error] ${service} health check timeout, inspect ${lf}" >&2
    return 1
  }
  
d1d356f8   tangwang   脚本优化
665
666
667
  start_one() {
    local service="$1"
    cd "${PROJECT_ROOT}"
0ba0e0fc   tangwang   1. rerank漏斗配置优化
668
669
670
671
    if ! service_enabled_by_config "${service}"; then
      echo "[skip] ${service} disabled by config"
      return 0
    fi
d1d356f8   tangwang   脚本优化
672
    local cmd
7fbca0d7   tangwang   启动脚本优化
673
674
675
676
    if ! cmd="$(service_start_cmd "${service}")"; then
      echo "[error] unknown service: ${service}" >&2
      return 1
    fi
d1d356f8   tangwang   脚本优化
677
678
679
    local pf lf
    pf="$(pid_file "${service}")"
    lf="$(log_file "${service}")"
28e57bb1   tangwang   日志体系优化
680
    prepare_daily_log_target "${service}"
d1d356f8   tangwang   脚本优化
681
  
07cf5a93   tangwang   START_EMBEDDING=...
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
    if [ "${service}" != "tei" ]; then
      if is_running_by_pid "${service}" || is_running_by_port "${service}"; then
        if [ "${service}" = "cnclip" ]; then
          local expected_device="${CNCLIP_DEVICE:-cuda}"
          expected_device="$(echo "${expected_device}" | tr '[:upper:]' '[:lower:]')"
          if [[ "${expected_device}" != "cuda" && "${expected_device}" != "cpu" ]]; then
            echo "[error] invalid CNCLIP_DEVICE=${CNCLIP_DEVICE}; use cuda/cpu" >&2
            return 1
          fi
          local actual_device
          actual_device="$(get_cnclip_flow_device 2>/dev/null || true)"
          if [ -n "${actual_device}" ] && [ "${actual_device}" != "${expected_device}" ]; then
            echo "[error] cnclip already running with device=${actual_device}, expected=${expected_device}" >&2
            echo "[error] run: ./scripts/service_ctl.sh stop cnclip && CNCLIP_DEVICE=${expected_device} ./scripts/service_ctl.sh start cnclip" >&2
            return 1
          fi
        fi
        echo "[skip] ${service} already running"
        return 0
      fi
d1d356f8   tangwang   脚本优化
702
703
704
    fi
  
    case "${service}" in
7fbca0d7   tangwang   启动脚本优化
705
      cnclip|tei)
d1d356f8   tangwang   脚本优化
706
        echo "[start] ${service} (managed by native script)"
cc11ae04   tangwang   cnclip
707
        if [ "${service}" = "cnclip" ]; then
c90f80ed   tangwang   相关性优化
708
          if ! CNCLIP_DEVICE="${CNCLIP_DEVICE:-cuda}" "${cmd}" >> "${lf}" 2>&1; then
af7ee060   tangwang   service_ctl 简化为“显...
709
710
711
            echo "[error] ${service} start script failed, inspect ${lf}" >&2
            return 1
          fi
cc11ae04   tangwang   cnclip
712
        else
c90f80ed   tangwang   相关性优化
713
          if ! "${cmd}" >> "${lf}" 2>&1; then
af7ee060   tangwang   service_ctl 简化为“显...
714
715
716
            echo "[error] ${service} start script failed, inspect ${lf}" >&2
            return 1
          fi
cc11ae04   tangwang   cnclip
717
        fi
07cf5a93   tangwang   START_EMBEDDING=...
718
719
720
721
722
723
724
725
        if [ "${service}" = "tei" ]; then
          if is_running_tei_container; then
            echo "[ok] ${service} started (log=${lf})"
          else
            echo "[error] ${service} failed to start, inspect ${lf}" >&2
            return 1
          fi
        elif is_running_by_pid "${service}" || is_running_by_port "${service}"; then
d1d356f8   tangwang   脚本优化
726
727
          echo "[ok] ${service} started (log=${lf})"
        else
07cf5a93   tangwang   START_EMBEDDING=...
728
729
          echo "[error] ${service} failed to start, inspect ${lf}" >&2
          return 1
d1d356f8   tangwang   脚本优化
730
731
        fi
        ;;
7b8d9e1a   tangwang   评估框架的启动脚本
732
      backend|indexer|frontend|eval-web|embedding|embedding-image|translator|reranker|reranker-fine)
9f5994b4   tangwang   reranker
733
        echo "[start] ${service}"
daa2690b   tangwang   漏斗参数调优&呈现优化
734
735
736
737
738
739
740
        local rerank_instance=""
        rerank_instance="$(reranker_instance_for_service "${service}")"
        if [ -n "${rerank_instance}" ]; then
          nohup env RERANK_INSTANCE="${rerank_instance}" "${cmd}" >> "${lf}" 2>&1 &
        else
          nohup "${cmd}" >> "${lf}" 2>&1 &
        fi
9f5994b4   tangwang   reranker
741
742
        local pid=$!
        echo "${pid}" > "${pf}"
a7bb846c   tangwang   monitor
743
        wait_for_startup_health "${service}" "${pid}" "${lf}"
9f5994b4   tangwang   reranker
744
        ;;
d1d356f8   tangwang   脚本优化
745
746
747
748
749
750
      *)
        echo "[warn] ${service} unsupported start path"
        ;;
    esac
  }
  
af7ee060   tangwang   service_ctl 简化为“显...
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
  cleanup_reranker_orphans() {
    local engine_pids
    engine_pids="$(pgrep -f 'VLLM::EngineCore' 2>/dev/null || true)"
    if [ -z "${engine_pids}" ]; then
      return 0
    fi
  
    echo "[stop] reranker orphan engines=${engine_pids}"
    for pid in ${engine_pids}; do
      kill -TERM "${pid}" 2>/dev/null || true
    done
    sleep 1
    engine_pids="$(pgrep -f 'VLLM::EngineCore' 2>/dev/null || true)"
    for pid in ${engine_pids}; do
      kill -KILL "${pid}" 2>/dev/null || true
    done
  }
  
d1d356f8   tangwang   脚本优化
769
770
771
  stop_one() {
    local service="$1"
    cd "${PROJECT_ROOT}"
d1d356f8   tangwang   脚本优化
772
773
774
775
776
    if [ "${service}" = "cnclip" ]; then
      echo "[stop] cnclip (managed by native script)"
      bash -lc "./scripts/stop_cnclip_service.sh" || true
      return 0
    fi
07cf5a93   tangwang   START_EMBEDDING=...
777
778
779
780
781
    if [ "${service}" = "tei" ]; then
      echo "[stop] tei (managed by native script)"
      bash -lc "./scripts/stop_tei_service.sh" || true
      return 0
    fi
d1d356f8   tangwang   脚本优化
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
  
    local pf
    pf="$(pid_file "${service}")"
  
    if [ -f "${pf}" ]; then
      local pid
      pid="$(cat "${pf}" 2>/dev/null || true)"
      if [ -n "${pid}" ] && kill -0 "${pid}" 2>/dev/null; then
        echo "[stop] ${service} pid=${pid}"
        kill -TERM "${pid}" 2>/dev/null || true
        sleep 1
        if kill -0 "${pid}" 2>/dev/null; then
          kill -KILL "${pid}" 2>/dev/null || true
        fi
      fi
      rm -f "${pf}"
    fi
  
    local port
    port="$(get_port "${service}")"
    if [ -n "${port}" ]; then
      local pids
dba57642   tangwang   bayes调参计划
804
      pids="$(list_listen_pids_by_port "${port}")"
d1d356f8   tangwang   脚本优化
805
806
807
808
809
810
      if [ -n "${pids}" ]; then
        echo "[stop] ${service} port=${port} pids=${pids}"
        for pid in ${pids}; do
          kill -TERM "${pid}" 2>/dev/null || true
        done
        sleep 1
dba57642   tangwang   bayes调参计划
811
        pids="$(list_listen_pids_by_port "${port}")"
d1d356f8   tangwang   脚本优化
812
813
814
815
816
        for pid in ${pids}; do
          kill -KILL "${pid}" 2>/dev/null || true
        done
      fi
    fi
af7ee060   tangwang   service_ctl 简化为“显...
817
  
daa2690b   tangwang   漏斗参数调优&呈现优化
818
    if [[ "${service}" == reranker* ]] && ! service_is_running "reranker" && ! service_is_running "reranker-fine"; then
af7ee060   tangwang   service_ctl 简化为“显...
819
820
      cleanup_reranker_orphans
    fi
d1d356f8   tangwang   脚本优化
821
822
823
824
825
826
827
828
  }
  
  status_one() {
    local service="$1"
    local port
    port="$(get_port "${service}")"
    local running="no"
    local pid_info="-"
c6da6bca   tangwang   add status.sh
829
830
    local health="down"
    local health_body=""
985752f5   tangwang   1. 前端调试功能
831
    local curl_timeout_opts=(--connect-timeout 8 --max-time 8)
d1d356f8   tangwang   脚本优化
832
  
07cf5a93   tangwang   START_EMBEDDING=...
833
834
835
836
837
838
839
    if [ "${service}" = "tei" ]; then
      local cid
      local tei_name="${TEI_CONTAINER_NAME:-saas-search-tei}"
      cid="$(docker ps -q -f name=^/${tei_name}$ 2>/dev/null || true)"
      if [ -n "${cid}" ]; then
        running="yes"
        pid_info="${cid:0:12}"
c6da6bca   tangwang   add status.sh
840
841
842
843
        # TEI: container 级别 running 后再尝试 HTTP /health
        local path
        path="$(health_path_for_service "${service}")"
        if [ -n "${port}" ] && [ -n "${path}" ]; then
985752f5   tangwang   1. 前端调试功能
844
          if health_body="$(curl -fsS "${curl_timeout_opts[@]}" "http://127.0.0.1:${port}${path}" 2>/dev/null)"; then
c6da6bca   tangwang   add status.sh
845
846
847
848
849
850
851
852
853
854
            health="ok"
          else
            health="fail"
          fi
        fi
      fi
      if [ -n "${health_body}" ]; then
        printf "%-10s running=%-3s port=%-6s pid=%s health=%-4s body=%s\n" "${service}" "${running}" "${port:--}" "${pid_info}" "${health}" "${health_body}"
      else
        printf "%-10s running=%-3s port=%-6s pid=%s health=%-4s\n" "${service}" "${running}" "${port:--}" "${pid_info}" "${health}"
07cf5a93   tangwang   START_EMBEDDING=...
855
      fi
07cf5a93   tangwang   START_EMBEDDING=...
856
857
858
      return
    fi
  
d1d356f8   tangwang   脚本优化
859
860
861
862
863
    if is_running_by_pid "${service}"; then
      running="yes"
      pid_info="$(cat "$(pid_file "${service}")" 2>/dev/null || echo "-")"
    elif is_running_by_port "${service}"; then
      running="yes"
dba57642   tangwang   bayes调参计划
864
      pid_info="$(list_listen_pids_by_port "${port}" | tr '\n' ',' | sed 's/,$//' || echo "-")"
d1d356f8   tangwang   脚本优化
865
866
    fi
  
c6da6bca   tangwang   add status.sh
867
868
869
870
    if [ "${running}" = "yes" ]; then
      local path
      path="$(health_path_for_service "${service}")"
      if [ -n "${port}" ] && [ -n "${path}" ]; then
985752f5   tangwang   1. 前端调试功能
871
        if health_body="$(curl -fsS "${curl_timeout_opts[@]}" "http://127.0.0.1:${port}${path}" 2>/dev/null)"; then
c6da6bca   tangwang   add status.sh
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
          health="ok"
        else
          health="fail"
        fi
      else
        # 没有 HTTP 健康检查端点(如 cnclip),运行即可视为 ok
        health="ok"
      fi
    fi
  
    if [ -n "${health_body}" ]; then
      printf "%-10s running=%-3s port=%-6s pid=%s health=%-4s body=%s\n" "${service}" "${running}" "${port:--}" "${pid_info}" "${health}" "${health_body}"
    else
      printf "%-10s running=%-3s port=%-6s pid=%s health=%-4s\n" "${service}" "${running}" "${port:--}" "${pid_info}" "${health}"
    fi
d1d356f8   tangwang   脚本优化
887
888
  }
  
7913e2fb   tangwang   服务管理和监控
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
  service_is_running() {
    local service="$1"
    case "${service}" in
      tei)
        is_running_tei_container
        ;;
      cnclip)
        is_running_by_pid "${service}" || is_running_by_port "${service}"
        ;;
      *)
        is_running_by_pid "${service}" || is_running_by_port "${service}"
        ;;
    esac
  }
  
  expand_target_token() {
    local token="$1"
    case "${token}" in
      all)
        echo "$(all_services)"
        ;;
      *)
        echo "${token}"
        ;;
    esac
  }
  
  normalize_targets() {
    local raw="$1"
    declare -A seen=()
    local out=""
    local token svc
    for token in ${raw}; do
      for svc in $(expand_target_token "${token}"); do
        if [ -z "${seen[${svc}]:-}" ]; then
          seen["${svc}"]=1
          out="${out} ${svc}"
        fi
      done
    done
    echo "${out# }"
  }
  
  sort_targets_by_order() {
    local targets="$1"
    shift || true
    local out=""
    local svc
    declare -A want=()
    for svc in ${targets}; do
      want["${svc}"]=1
    done
  
    for svc in "$@"; do
      if [ "${want[${svc}]:-0}" = "1" ]; then
        out="${out} ${svc}"
        unset "want[${svc}]"
      fi
    done
  
    for svc in ${targets}; do
      if [ "${want[${svc}]:-0}" = "1" ]; then
        out="${out} ${svc}"
        unset "want[${svc}]"
      fi
    done
    echo "${out# }"
  }
  
  apply_target_order() {
    local action="$1"
    local targets="$2"
    case "${action}" in
      stop|down)
        sort_targets_by_order "${targets}" "${STOP_ORDER_SERVICES[@]}"
        ;;
      *)
        sort_targets_by_order "${targets}" "${FULL_SERVICES[@]}"
        ;;
    esac
  }
  
d1d356f8   tangwang   脚本优化
971
972
973
974
975
976
977
978
979
980
  resolve_targets() {
    local scope="$1"
    shift || true
  
    if [ "$#" -gt 0 ]; then
      echo "$*"
      return
    fi
  
    case "${scope}" in
7913e2fb   tangwang   服务管理和监控
981
982
      monitor-stop|monitor-status)
        echo ""
d1d356f8   tangwang   脚本优化
983
        ;;
7913e2fb   tangwang   服务管理和监控
984
      status)
d1d356f8   tangwang   脚本优化
985
986
        echo "$(all_services)"
        ;;
d1d356f8   tangwang   脚本优化
987
988
989
990
991
992
993
994
995
      *)
        echo ""
        ;;
    esac
  }
  
  usage() {
    cat <<'EOF'
  Usage:
7913e2fb   tangwang   服务管理和监控
996
997
    ./scripts/service_ctl.sh up [all|service...]
    ./scripts/service_ctl.sh down [service...]
d1d356f8   tangwang   脚本优化
998
999
1000
1001
    ./scripts/service_ctl.sh start [service...]
    ./scripts/service_ctl.sh stop [service...]
    ./scripts/service_ctl.sh restart [service...]
    ./scripts/service_ctl.sh status [service...]
7913e2fb   tangwang   服务管理和监控
1002
1003
1004
1005
    ./scripts/service_ctl.sh monitor [service...]
    ./scripts/service_ctl.sh monitor-start [service...]
    ./scripts/service_ctl.sh monitor-stop
    ./scripts/service_ctl.sh monitor-status
d1d356f8   tangwang   脚本优化
1006
1007
  
  Default target set (when no service provided):
d1d356f8   tangwang   脚本优化
1008
    status  -> all known services
7913e2fb   tangwang   服务管理和监控
1009
1010
1011
1012
    up/start/stop/restart/down/monitor/monitor-start -> must specify services or all
  
  Special targets:
    all      -> all known services
d1d356f8   tangwang   脚本优化
1013
  
7913e2fb   tangwang   服务管理和监控
1014
1015
  Examples:
    ./scripts/service_ctl.sh up all
7b8d9e1a   tangwang   评估框架的启动脚本
1016
1017
    ./scripts/service_ctl.sh up tei cnclip embedding embedding-image translator reranker reranker-fine backend indexer frontend eval-web
    ./scripts/service_ctl.sh up backend indexer frontend eval-web
7913e2fb   tangwang   服务管理和监控
1018
1019
1020
    ./scripts/service_ctl.sh restart
    ./scripts/service_ctl.sh monitor-start all
    ./scripts/service_ctl.sh monitor-status
28e57bb1   tangwang   日志体系优化
1021
1022
1023
  
  Log retention:
    LOG_RETENTION_DAYS=30 ./scripts/service_ctl.sh start
7913e2fb   tangwang   服务管理和监控
1024
1025
1026
1027
1028
1029
  
  Monitor tuning:
    MONITOR_INTERVAL_SEC=10
    MONITOR_FAIL_THRESHOLD=3
    MONITOR_RESTART_COOLDOWN_SEC=30
    MONITOR_MAX_RESTARTS_PER_HOUR=6
d1d356f8   tangwang   脚本优化
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
  EOF
  }
  
  main() {
    if [ "$#" -lt 1 ]; then
      usage
      exit 1
    fi
  
    local action="$1"
    shift || true
  
c7e80cc2   tangwang   新的 .env 管理机制如下:
1042
    load_env_file "${PROJECT_ROOT}/.env"
7913e2fb   tangwang   服务管理和监控
1043
    local targets=""
0ba0e0fc   tangwang   1. rerank漏斗配置优化
1044
    local effective_targets=""
7913e2fb   tangwang   服务管理和监控
1045
1046
    local monitor_was_running=0
    local monitor_prev_targets=""
a7bb846c   tangwang   monitor
1047
    local auto_monitor_on_start="${SERVICE_CTL_AUTO_MONITOR_ON_START:-1}"
7913e2fb   tangwang   服务管理和监控
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
  
    case "${action}" in
      monitor-stop|monitor-status)
        ;;
      *)
        targets="$(resolve_targets "${action}" "$@")"
        if [ -z "${targets}" ]; then
          usage
          exit 1
        fi
        targets="$(normalize_targets "${targets}")"
        targets="$(apply_target_order "${action}" "${targets}")"
        if [ -z "${targets}" ]; then
          echo "[error] empty targets after expansion" >&2
          exit 1
        fi
        validate_targets "${targets}"
        ;;
    esac
d1d356f8   tangwang   脚本优化
1067
  
0ba0e0fc   tangwang   1. rerank漏斗配置优化
1068
1069
1070
1071
1072
1073
1074
    effective_targets="${targets}"
    case "${action}" in
      up|start|restart|monitor|monitor-start)
        effective_targets="$(filter_disabled_targets "${targets}" "verbose")"
        ;;
    esac
  
d1d356f8   tangwang   脚本优化
1075
    case "${action}" in
7913e2fb   tangwang   服务管理和监控
1076
      up)
0ba0e0fc   tangwang   1. rerank漏斗配置优化
1077
1078
1079
1080
1081
        if [ -z "${effective_targets}" ]; then
          echo "[info] no enabled services in target set"
          exit 0
        fi
        for svc in ${effective_targets}; do
7913e2fb   tangwang   服务管理和监控
1082
1083
          start_one "${svc}"
        done
0ba0e0fc   tangwang   1. rerank漏斗配置优化
1084
        start_monitor_daemon "${effective_targets}"
7913e2fb   tangwang   服务管理和监控
1085
1086
1087
1088
1089
1090
1091
        ;;
      down)
        stop_monitor_daemon
        for svc in ${targets}; do
          stop_one "${svc}"
        done
        ;;
d1d356f8   tangwang   脚本优化
1092
      start)
0ba0e0fc   tangwang   1. rerank漏斗配置优化
1093
1094
1095
1096
1097
        if [ -z "${effective_targets}" ]; then
          echo "[info] no enabled services in target set"
          exit 0
        fi
        for svc in ${effective_targets}; do
d1d356f8   tangwang   脚本优化
1098
1099
          start_one "${svc}"
        done
a7bb846c   tangwang   monitor
1100
        if [ "${auto_monitor_on_start}" = "1" ]; then
0ba0e0fc   tangwang   1. rerank漏斗配置优化
1101
          start_monitor_daemon "$(merge_targets "$(monitor_current_targets)" "${effective_targets}")"
a7bb846c   tangwang   monitor
1102
        fi
d1d356f8   tangwang   脚本优化
1103
1104
        ;;
      stop)
7913e2fb   tangwang   服务管理和监控
1105
        if is_monitor_daemon_running; then
a7bb846c   tangwang   monitor
1106
1107
1108
1109
1110
1111
1112
1113
1114
          local remaining_targets
          remaining_targets="$(subtract_targets "$(monitor_current_targets)" "${targets}")"
          if [ -n "${remaining_targets}" ]; then
            echo "[info] updating monitor daemon targets -> [${remaining_targets}]"
            start_monitor_daemon "${remaining_targets}"
          else
            echo "[info] stopping monitor daemon before manual stop"
            stop_monitor_daemon
          fi
7913e2fb   tangwang   服务管理和监控
1115
        fi
d1d356f8   tangwang   脚本优化
1116
1117
1118
1119
1120
        for svc in ${targets}; do
          stop_one "${svc}"
        done
        ;;
      restart)
7913e2fb   tangwang   服务管理和监控
1121
1122
1123
1124
1125
1126
1127
1128
1129
        local restart_stop_targets
        restart_stop_targets="$(apply_target_order stop "${targets}")"
        if is_monitor_daemon_running; then
          monitor_was_running=1
          monitor_prev_targets="$(monitor_current_targets)"
          [ -z "${monitor_prev_targets}" ] && monitor_prev_targets="${targets}"
          stop_monitor_daemon
        fi
        for svc in ${restart_stop_targets}; do
d1d356f8   tangwang   脚本优化
1130
1131
          stop_one "${svc}"
        done
0ba0e0fc   tangwang   1. rerank漏斗配置优化
1132
        for svc in ${effective_targets}; do
d1d356f8   tangwang   脚本优化
1133
1134
          start_one "${svc}"
        done
7913e2fb   tangwang   服务管理和监控
1135
1136
        if [ "${monitor_was_running}" -eq 1 ]; then
          monitor_prev_targets="$(normalize_targets "${monitor_prev_targets}")"
0ba0e0fc   tangwang   1. rerank漏斗配置优化
1137
          monitor_prev_targets="$(filter_disabled_targets "${monitor_prev_targets}" "quiet")"
7913e2fb   tangwang   服务管理和监控
1138
          monitor_prev_targets="$(apply_target_order monitor "${monitor_prev_targets}")"
0ba0e0fc   tangwang   1. rerank漏斗配置优化
1139
          [ -z "${monitor_prev_targets}" ] && monitor_prev_targets="${effective_targets}"
7913e2fb   tangwang   服务管理和监控
1140
          start_monitor_daemon "${monitor_prev_targets}"
a7bb846c   tangwang   monitor
1141
        elif [ "${auto_monitor_on_start}" = "1" ]; then
0ba0e0fc   tangwang   1. rerank漏斗配置优化
1142
          start_monitor_daemon "$(merge_targets "$(monitor_current_targets)" "${effective_targets}")"
7913e2fb   tangwang   服务管理和监控
1143
        fi
d1d356f8   tangwang   脚本优化
1144
1145
1146
1147
1148
        ;;
      status)
        for svc in ${targets}; do
          status_one "${svc}"
        done
7913e2fb   tangwang   服务管理和监控
1149
1150
1151
        monitor_daemon_status
        ;;
      monitor)
0ba0e0fc   tangwang   1. rerank漏斗配置优化
1152
1153
1154
1155
1156
        if [ -z "${effective_targets}" ]; then
          echo "[info] no enabled services in target set"
          exit 0
        fi
        monitor_services "${effective_targets}"
7913e2fb   tangwang   服务管理和监控
1157
1158
        ;;
      monitor-start)
0ba0e0fc   tangwang   1. rerank漏斗配置优化
1159
        start_monitor_daemon "${effective_targets}"
7913e2fb   tangwang   服务管理和监控
1160
1161
1162
1163
1164
1165
        ;;
      monitor-stop)
        stop_monitor_daemon
        ;;
      monitor-status)
        monitor_daemon_status
d1d356f8   tangwang   脚本优化
1166
1167
1168
1169
1170
1171
1172
1173
1174
        ;;
      *)
        usage
        exit 1
        ;;
    esac
  }
  
  main "$@"