F01:新增 /api/v1/system/worker-capabilities,按 job_types 判定 test.upload 执行器是否注册, 服务卡片分「地址可达 / 凭据与库读取 / 执行器能力」三行。 F04:routing-bindings 返回 LIBRARY_SOURCE_KB_* 与一致性,设置页同屏显示入库目标与封存源。 结构:测试绑定与生产目标合并为「知识库映射」。
This commit is contained in:
@@ -41,6 +41,24 @@ type routingBindingView struct {
|
||||
// that had not been uploaded yet. A plain read reports zeros.
|
||||
Replanned int `json:"replanned"`
|
||||
RetiredDuplicates int `json:"retired_duplicates"`
|
||||
// 整库封存源(LIBRARY_SOURCE_KB_*)与“测试入库绑定”是两套配置:
|
||||
// 页面必须同时给出两者,并标明是否一致,避免改绑后名字变了、封存还是旧库。
|
||||
LibrarySourceInternalKB string `json:"library_source_internal_kb"`
|
||||
LibrarySourceExternalKB string `json:"library_source_external_kb"`
|
||||
InternalSourceState string `json:"internal_source_state"`
|
||||
ExternalSourceState string `json:"external_source_state"`
|
||||
}
|
||||
|
||||
// sourceState 只回答“入库目标与封存源是不是同一个库”。
|
||||
func sourceState(binding, source string) string {
|
||||
switch {
|
||||
case source == "":
|
||||
return "unconfigured"
|
||||
case binding == source:
|
||||
return "consistent"
|
||||
default:
|
||||
return "mismatch"
|
||||
}
|
||||
}
|
||||
|
||||
type routingKnowledgeBaseOption struct {
|
||||
@@ -101,7 +119,11 @@ func (config Config) routingBindingPayload(ctx context.Context, replan postgress
|
||||
InternalEnvFallback: storedInternal == "" && config.InternalRoutingKnowledgeBaseID != "",
|
||||
ExternalEnvFallback: storedExternal == "" && config.ExternalRoutingKnowledgeBaseID != "",
|
||||
InternalKBName: names[storedInternal], ExternalKBName: names[storedExternal],
|
||||
UpdatedBy: updatedBy, WeKnoraConnected: connected,
|
||||
LibrarySourceInternalKB: config.LibrarySourceInternalKB,
|
||||
LibrarySourceExternalKB: config.LibrarySourceExternalKB,
|
||||
InternalSourceState: sourceState(internal, config.LibrarySourceInternalKB),
|
||||
ExternalSourceState: sourceState(external, config.LibrarySourceExternalKB),
|
||||
UpdatedBy: updatedBy, WeKnoraConnected: connected,
|
||||
Replanned: replan.Replanned, RetiredDuplicates: replan.RetiredDuplicates,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -377,6 +377,9 @@ func NewHandler(config Config) (http.Handler, error) {
|
||||
internal, external := config.effectiveRoutingTargets(r.Context())
|
||||
writeJSON(w, http.StatusOK, map[string]any{"data": map[string]bool{"internal_enabled": internal != "", "external_enabled": external != ""}})
|
||||
})
|
||||
mux.HandleFunc("GET /api/v1/system/worker-capabilities", func(w http.ResponseWriter, r *http.Request) {
|
||||
config.handleWorkerCapabilities(w, r)
|
||||
})
|
||||
mux.HandleFunc("GET /api/v1/routing-bindings", func(w http.ResponseWriter, r *http.Request) {
|
||||
if config.RoutingBindings == nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "routing bindings storage unavailable"})
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
)
|
||||
|
||||
// workerHeartbeatStore 是队列存活记录的只读视图。页面要回答“执行器在不在”,
|
||||
// 只能看 worker 认领任务时自己写下的 job_types;readiness 只证明接口可达。
|
||||
type workerHeartbeatStore interface {
|
||||
ListWorkerHeartbeats(context.Context) ([]postgresstore.WorkerHeartbeat, error)
|
||||
}
|
||||
|
||||
type workerCapabilityView struct {
|
||||
WorkerID string `json:"worker_id"`
|
||||
JobTypes []string `json:"job_types"`
|
||||
Alive bool `json:"alive"`
|
||||
LastSeenAt string `json:"last_seen_at"`
|
||||
}
|
||||
|
||||
type testUploadCapability struct {
|
||||
State string `json:"state"`
|
||||
Consumers int `json:"consumers"`
|
||||
Detail string `json:"detail"`
|
||||
}
|
||||
|
||||
// 与 qaopsmirror 的工作就绪判定同一个窗口:2 分钟没有认领记录就当作不在线。
|
||||
const workerHeartbeatTTL = 2 * time.Minute
|
||||
|
||||
const testUploadJobType = "test.upload"
|
||||
|
||||
func testUploadCapabilityOf(workers []postgresstore.WorkerHeartbeat, now time.Time) testUploadCapability {
|
||||
alive, consumers := 0, 0
|
||||
for _, worker := range workers {
|
||||
if now.Sub(worker.LastSeenAt) > workerHeartbeatTTL {
|
||||
continue
|
||||
}
|
||||
alive++
|
||||
if slices.Contains(worker.JobTypes, testUploadJobType) {
|
||||
consumers++
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case consumers > 0:
|
||||
return testUploadCapability{State: "enabled", Consumers: consumers,
|
||||
Detail: "上传/解析执行器已注册(认领过 test.upload)"}
|
||||
case alive > 0:
|
||||
return testUploadCapability{State: "disabled", Consumers: 0,
|
||||
Detail: "worker 在线但没有注册 test.upload:上传/解析任务不会被消费"}
|
||||
default:
|
||||
return testUploadCapability{State: "stale", Consumers: 0,
|
||||
Detail: "没有有效的 worker 认领记录:执行器未运行,或从未接单"}
|
||||
}
|
||||
}
|
||||
|
||||
func (config Config) handleWorkerCapabilities(w http.ResponseWriter, r *http.Request) {
|
||||
store, ok := config.Store.(workerHeartbeatStore)
|
||||
if !ok || store == nil {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"data": map[string]any{
|
||||
"test_upload": testUploadCapability{State: "unknown", Detail: "worker 存活记录不可用"},
|
||||
"workers": []workerCapabilityView{},
|
||||
}})
|
||||
return
|
||||
}
|
||||
workers, err := store.ListWorkerHeartbeats(r.Context())
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"data": map[string]any{
|
||||
"test_upload": testUploadCapability{State: "unknown", Detail: "读取 worker 存活记录失败"},
|
||||
"workers": []workerCapabilityView{},
|
||||
}})
|
||||
return
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
views := make([]workerCapabilityView, 0, len(workers))
|
||||
for _, worker := range workers {
|
||||
views = append(views, workerCapabilityView{
|
||||
WorkerID: worker.WorkerID,
|
||||
JobTypes: worker.JobTypes,
|
||||
Alive: now.Sub(worker.LastSeenAt) <= workerHeartbeatTTL,
|
||||
LastSeenAt: worker.LastSeenAt.UTC().Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"data": map[string]any{
|
||||
"test_upload": testUploadCapabilityOf(workers, now),
|
||||
"workers": views,
|
||||
}})
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
)
|
||||
|
||||
func TestTestUploadCapabilityReflectsWorkerHeartbeats(t *testing.T) {
|
||||
now := time.Date(2026, time.November, 17, 7, 0, 0, 0, time.UTC)
|
||||
fresh := now.Add(-10 * time.Second)
|
||||
stale := now.Add(-5 * time.Minute)
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
workers []postgresstore.WorkerHeartbeat
|
||||
state string
|
||||
consume int
|
||||
}{
|
||||
{"no heartbeats", nil, "stale", 0},
|
||||
{"heartbeat too old", []postgresstore.WorkerHeartbeat{{LastSeenAt: stale, JobTypes: []string{"test.upload"}}}, "stale", 0},
|
||||
{"worker online without upload chain", []postgresstore.WorkerHeartbeat{{LastSeenAt: fresh, JobTypes: []string{"source.scan.dingtalk"}}}, "disabled", 0},
|
||||
{"enabled consumer", []postgresstore.WorkerHeartbeat{{LastSeenAt: fresh, JobTypes: []string{"test.upload", "test.poll"}}}, "enabled", 1},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
got := testUploadCapabilityOf(tc.workers, now)
|
||||
if got.State != tc.state || got.Consumers != tc.consume {
|
||||
t.Fatalf("%s: got state=%s consumers=%d, want %s/%d", tc.name, got.State, got.Consumers, tc.state, tc.consume)
|
||||
}
|
||||
if got.Detail == "" {
|
||||
t.Fatalf("%s: detail must explain the state", tc.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user