visionA/local-tool/visiona-local/shutdown_notify.go
jim800121chen 8cd5751ce3 feat(local-tool): M8 重構 — Wails 控制台 + 瀏覽器 Web UI(R5 決策)
依 R5 五輪決策把 visionA-local 從「Wails 內嵌 Next.js」重構為「Wails
本機伺服器控制台 + 瀏覽器 Web UI」模式(類比 Docker Desktop / Ollama)。

程式碼變動
  - M8-1 砍 yt-dlp 全套(後端 resolver / URL handler / 前端 URL tab /
    Makefile vendor / installer / bootstrap / CI workflow,-555 行)
  - M8-2 砍 Mock 模式全套(driver/mock、mock_camera、Settings runtimeMode、
    VISIONA_MOCK 環境變數,-528 行)
  - M8-3 ffmpeg 從 GPL 切換到 LGPL 混合方案:Windows/Linux 用 BtbN 現成
    LGPL binary,macOS 自 build minimal decoder-only 進 git
    (vendor/ffmpeg/macos/ffmpeg 5.7MB + ffprobe 5.6MB,比 GPL 版省 85% 空間)
  - M8-4 Wails Server Controller:state machine、log ring buffer 2000 行、
    preferences.json atomic write、boot-id、Gin SkipPaths、shutdown 7+1 秒、
    notify_*.go 三平台 OS 通知、watchServer 改 Error state 不 os.Exit
  - M8-4b 啟動階段管線 R5-E:6 階段進度 event、20s soft / 60s hard timeout、
    stage 5/6 skip 規則、sentinel file、RestartStartupSequence 5 步驟
  - M8-5 Wails 控制台 vanilla HTML/JS/CSS(9 檔 ~2012 行)取代 M7-B splash:
    state 視覺、log panel、startup progress panel、Stage 6 manual CTA
    pulse、shutdown modal、Settings、Dark Mode、i18n 中英雙語
  - M8-6 上傳影片副檔名擴充(mp4/avi/mov/mpeg/mpg)
  - M8-7 Web UI Server Offline Overlay(role=alertdialog + focus trap +
    wsEverConnected 容錯 + Page Visibility)
  - M8-8 CORS middleware(127.0.0.1/localhost only + suffix attack 防護)+
    ws/origin.go 獨立 WebSocket CheckOrigin 避 package cycle
  - MAJ-4 server:shutdown-imminent WebSocket broadcast 機制
    (/ws/system endpoint + notifyShutdownImminent helper)
  - M8-9 Boot-ID + 瀏覽器 tab 自動重連(sessionStorage loop guard)

品質
  - ~105+ 新 unit test + race detector (-count=2) 全綠
  - 10 個 milestone 全部通過 Reviewer 審查
  - 三方 v2 + v2.1 文件(PRD / Design Spec / TDD)+ 交叉互審紀錄
    收錄在 .autoflow/

交付前待處理(M8-10)
  - 重跑 make payload-macos 把舊 GPL 77MB binary 換成新 LGPL
  - 三平台 end-to-end build 驗證

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 17:57:54 +08:00

59 lines
2.1 KiB
Go
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.

package main
// shutdown_notify.go — MAJ-4 補丁
//
// 對應 TDD v2/server-lifecycle.md §2.3Minor 4與 v2/web-ui-offline-overlay.md §3.2a。
//
// 在 Wails OnBeforeClose / Restart 實際對 server 下 SIGTERM 之前,先打一個 POST 到
// server 的 /api/system/shutdown-notify讓 server 透過 /ws/system WebSocket 廣播
// server:shutdown-imminent 給所有已連線的瀏覽器 tab觸發 Offline Overlay 立即顯示。
//
// 設計原則best-effort不阻塞 shutdown 流程。
// - 1 秒 timeoutserver 沒起來、卡住、network error 都視為失敗,不重試
// - 失敗完全忽略log 也不寫,避免 shutdown 期間噴 stderr
// - port <= 0 時直接 returnserver 根本沒起來)
import (
"context"
"fmt"
"net/http"
"time"
)
// notifyShutdownImminentTimeout 控制 HTTP request 的總 timeout。
// server 端 ShutdownNotify handler 會 sleep 100 ms 後才回,留 900 ms buffer 給網路與 scheduler。
// 變數化以便測試注入。
var notifyShutdownImminentTimeout = 1 * time.Second
// notifyShutdownImminent 通知 server 廣播 shutdown-imminent 給所有 WebSocket client。
//
// 參數:
//
// ctx — caller context會被 timeout 包裹,避免 ctx 卡住整個 shutdown
// port — server 正在聽的 port<= 0 代表 server 還沒起來,直接 skip
// reason — "quit" 或 "restart",對應 Offline Overlay 的 reason 映射
//
// 錯誤容忍:全部錯誤靜默處理,絕不阻塞呼叫端。
func notifyShutdownImminent(ctx context.Context, port int, reason string) {
if port <= 0 {
return
}
if ctx == nil {
ctx = context.Background()
}
reqCtx, cancel := context.WithTimeout(ctx, notifyShutdownImminentTimeout)
defer cancel()
url := fmt.Sprintf("http://127.0.0.1:%d/api/system/shutdown-notify?reason=%s", port, reason)
req, err := http.NewRequestWithContext(reqCtx, http.MethodPost, url, nil)
if err != nil {
return
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
// 不需讀 body關閉即可釋放底層連線
_ = resp.Body.Close()
}