Files
arcrun-collector/cmd/arcrun-app/supervise.go
T
Leo d787bfc47d t195:燈號不再說謊+加診斷 log(leo:「燈號是真的還是假的?」→ 是假的)
## leo 實撞
丟 PDF 進 youlinhsieh-test1 沒反應;按「立刻同步」後畫面顯示
「正在整理知識卡」,但**卡從來沒產出**。

## 燈號為什麼是假的
舊版 describeStatus 只看「sync-now 訊號檔存不存在」就顯示「同步中…」。
那只代表**排隊了**,不代表有人處理:leo 的 collector 在 11:25 跑完最後一輪,
他 11:38 按同步 ⇒ 訊號檔沒人消化 ⇒ 畫面一直說在整理,實際 13 分鐘沒跑過。
**這比沒有燈號更糟——它在說謊。**

修:燈號要有憑有據——
  · collector 沒在跑 → 明說「同步引擎沒有在跑」並告訴他怎麼辦
  · 有排隊 **且** 引擎活著 → 才是「同步中」
  · 其餘 → 看守中

## 順手補上診斷 log(~/.arcrun-rag/app.log)
startSupervisor 以前只 Stat(config) 就靜默 return,出問題完全查不到
(我這次也是繞了很久才確定它有啟動)。現在每個 return 點都留痕。

## 🔴 但真正擋住產卡的是另一個 401(未修,需 leo 裁)
實測鏈路:掃到 PDF  → /portal/daemon/extract  200 → 寫 kbdb  401
  curl -X POST https://arcrun-kbdb.youlin-hsieh-dev.workers.dev/entries(無 token)→ 401
真兇:workflows/rag-ingest-card.local.yaml 打 __KBDB_BASE__/entries
**完全沒帶 Authorization**,但 kbdb 要 Bearer KBDB_INTERNAL_TOKEN。
⚠️ 與今天修的 t189 不同:那是 extract 端點入口(現已 200),這是 workflow 內部寫 kbdb。

修法命中 D36 金鑰鐵律(「只能拿 key,送出時自動拉 value」)
⇒ 應寫 {{credential.kbdb_internal_token}} 由 resolve_credentials 回填,
   而非讓安裝器 sed 把 token 值塞進定義。**已停下等 leo 裁**。

## CP 對帳
「丟檔→產卡」=  斷(卡產不出來)。本次只修好「燈號誠實」與「可診斷」,
**未送達用戶**(daemon 新版尚未出貨、401 未修)。
2026-08-05 11:45:07 +08:00

147 lines
4.9 KiB
Go
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.
package main
// supervise.go — 看守 collector 子行程(t194
//
// 🔴 沒有這一段,這個 App 就只是個「會顯示設定的空殼」——
// daemon 的本體是 `collector direct`,它才在監看資料夾、萃卡、上傳。
// (寫 Wails 版時我一度漏了它,等於做出一個裝了也不會同步的東西。)
//
// 邊界:**複用既有的 supervisor 套件**(重起退避、狀態機、round 解析都在裡面,
// 含 t191 的 phase:start/done ⇒ 「同步中…」)。這裡只做「找到執行檔、拉起來」。
import (
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
"time"
"arcrun-rag/collector/supervisor"
)
var sup *supervisor.Supervisor
// collectorBinPath 找同綑的 collector 執行檔。
// 規則與 arcrun-tray 一致:跟 App 執行檔同層(build 時會複製進 .app)。
func collectorBinPath() string {
name := "arcrun-collector"
if runtime.GOOS == "windows" {
name += ".exe"
}
exe, err := os.Executable()
if err != nil {
return name
}
if p, err := filepath.EvalSymlinks(exe); err == nil {
exe = p
}
return filepath.Join(filepath.Dir(exe), name)
}
// startSupervisor 在 App 啟動時拉起 collector;沒有 config 就先不啟動
// (使用者還沒連知識庫,啟動只會一直失敗刷 log)。
func startSupervisor() {
bin := collectorBinPath()
cfg := configPath()
// 🔴 t195:這裡以前只 Stat(config) 就靜默 return,出問題完全查不到。
// leo 實撞:App 活著、collector 從沒啟動、畫面卻顯示「正在整理知識卡」。
// ⇒ 每個 return 點都要留下痕跡(寫進 collector.log 旁邊的 app.log)。
if _, err := os.Stat(bin); err != nil {
appLog("啟動同步引擎失敗:找不到 %s(%v)", bin, err)
return
}
if _, err := os.Stat(cfg); err != nil {
appLog("尚未連上知識庫(沒有 %s),同步引擎先不啟動", cfg)
return
}
sup = supervisor.New(bin, cfg)
sup.Start()
appLog("同步引擎已啟動:%s", bin)
}
// appLog 把 App 層的關鍵事件寫進 ~/.arcrun-rag/app.log。
// 沒有這個,「為什麼沒同步」就只能用猜的(leo 這次就卡在這)。
func appLog(format string, args ...any) {
f, err := os.OpenFile(filepath.Join(appDir(), "app.log"),
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return
}
defer f.Close()
fmt.Fprintf(f, "%s %s\n", time.Now().Format(time.RFC3339), fmt.Sprintf(format, args...))
}
// restartWatch 在設定變更後重起看守,讓新設定立刻生效。
func restartWatch() {
if sup == nil {
return
}
sup.Stop()
if _, err := os.Stat(configPath()); err == nil {
sup.Start()
}
}
// stopSupervisor 結束前停掉 collector 子行程。
// 沒有這一步,按「結束 Arcrun」後 collector 會變孤兒繼續跑
// ⇒ 使用者以為關了、其實還在同步,而且下次啟動會有兩個。
func stopSupervisor() {
if sup != nil {
sup.Stop()
}
}
// installSignalHandler 讓 App 對 SIGTERMSIGINT 有反應。
//
// 🔴 leo 實測④:「**用強制結束把它關掉才能測試**,一般托盤程式不會顯示在強制結束列表中」。
//
// 真兇:Wails 的 loop 不理會 TERM,而 collector 是我們自己 Start 的子行程
// ⇒ 主程式被殺時**子行程變孤兒繼續跑**(實測:kill -TERM 後 collector 還在)。
// ⇒ 收到訊號就先停子行程再退出,避免「關了卻還在同步」與「下次啟動有兩個」。
func installSignalHandler() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-ch
stopSupervisor()
// CommandContext 的 kill 是非同步的:Stop() 回來時子行程可能還在收屍。
// 實測若直接 os.Exit(0)collector 會變孤兒繼續跑(leo 撞到的④)。
// ⇒ 等它真的不見,最多 3 秒;逾時就自己補一刀。
waitCollectorGone(3 * time.Second)
os.Exit(0)
}()
}
// waitCollectorGone 等同綑的 collector 子行程真的結束。
// 逾時就直接殺——寧可強制,也不要留一個「使用者以為關了卻還在同步」的孤兒。
func waitCollectorGone(max time.Duration) {
bin := collectorBinPath()
deadline := time.Now().Add(max)
for time.Now().Before(deadline) {
if !processRunning(bin) {
return
}
time.Sleep(120 * time.Millisecond)
}
_ = exec.Command("pkill", "-TERM", "-f", bin).Run()
}
// processRunning 用 pgrep 查有沒有這支執行檔在跑(純查詢,不動別人的行程)。
func processRunning(bin string) bool {
out, _ := exec.Command("pgrep", "-f", bin).Output()
return len(strings.TrimSpace(string(out))) > 0
}
// collectorAlive 回報「同步引擎是不是真的在跑」。
// 🔴 t195:燈號不可以只憑「訊號檔存在」就說在同步——leo 實撞過
// collector 已死、畫面卻一直顯示「正在整理知識卡」13 分鐘。
func collectorAlive() bool {
if sup == nil {
return false
}
return processRunning(collectorBinPath())
}