fe9d055522
- 为什么:运行时读另用 WEKNORA_RUNTIME_KEY_FILE(0400 文件、启动读一次),当前部署没有该文件,队列与处理详情恒不可用;换 Key 还要重启,且与绑定页不同步。 - 做什么:RuntimeProvider 改为每次请求经 kbbinding.Resolve(EnvTest) 取地址与 Key,与 reconciliationRemote 共用同一 resolver;删除配置字段/校验、ReadRuntimeKeyFile 与测试、compose/env、启动 WARN。 - 三态:绑定不可用→503 runtime_read_unavailable;上游 401/403→502 runtime_read_forbidden;超时/DNS→502 runtime_read_unreachable;overview 增 weknora_queue_error,页面三句不同文案并保留 WeKnora 跳转链接。 - 同步:openapi progress 契约与 502、需求书 §5/Q7、阶段计划、运维手册、CHANGELOG。 - 验证:make verify 17 项 success;新增换 Key 二次请求头、401/403/不可达分类、Service 透传、overview 原因码、页面三文案用例。
156 lines
5.2 KiB
Go
156 lines
5.2 KiB
Go
package reconciliation
|
|
|
|
import (
|
|
"context"
|
|
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/governance"
|
|
"knowledge-governance/internal/platform/weknora"
|
|
)
|
|
|
|
// RuntimeConfig.Resolve runs for every request instead of once at startup: the
|
|
// runtime read uses the same knowledge-base binding (test environment) as the
|
|
// reconciliation remote, so an admin saving a new key must take effect on the
|
|
// next request without restarting the API.
|
|
type RuntimeConfig struct {
|
|
Resolve func(context.Context) (baseURL, apiKey string, err error)
|
|
Timeout time.Duration
|
|
AllowDockerHTTP bool
|
|
AllowPrivateHTTP bool
|
|
Now func() time.Time
|
|
}
|
|
|
|
type RuntimeProvider struct {
|
|
resolve func(context.Context) (baseURL, apiKey string, err error)
|
|
timeout time.Duration
|
|
allowDockerHTTP bool
|
|
allowPrivateHTTP bool
|
|
now func() time.Time
|
|
}
|
|
|
|
func NewRuntimeProvider(config RuntimeConfig) *RuntimeProvider {
|
|
if config.Now == nil {
|
|
config.Now = time.Now
|
|
}
|
|
return &RuntimeProvider{
|
|
resolve: config.Resolve, timeout: config.Timeout,
|
|
allowDockerHTTP: config.AllowDockerHTTP, allowPrivateHTTP: config.AllowPrivateHTTP, now: config.Now,
|
|
}
|
|
}
|
|
|
|
func (p *RuntimeProvider) client(ctx context.Context) (*weknora.RuntimeClient, error) {
|
|
if p == nil || p.resolve == nil {
|
|
return nil, ErrRuntimeUnavailable
|
|
}
|
|
baseURL, apiKey, err := p.resolve(ctx)
|
|
if err != nil {
|
|
// Not configured, storage locked, or the saved key cannot be opened:
|
|
// all of them mean "no usable binding", never "upstream rejected us".
|
|
return nil, ErrRuntimeUnavailable
|
|
}
|
|
baseURL, apiKey = strings.TrimSpace(baseURL), strings.TrimSpace(apiKey)
|
|
if baseURL == "" || apiKey == "" {
|
|
return nil, ErrRuntimeUnavailable
|
|
}
|
|
client, err := weknora.NewRuntimeClient(weknora.RuntimeClientConfig{
|
|
BaseURL: baseURL,
|
|
APIKey: apiKey,
|
|
Timeout: p.timeout,
|
|
AllowDockerHTTP: p.allowDockerHTTP,
|
|
AllowPrivateHTTP: p.allowPrivateHTTP,
|
|
})
|
|
if err != nil {
|
|
return nil, ErrRuntimeUnavailable
|
|
}
|
|
return client, nil
|
|
}
|
|
|
|
// classifyRuntimeReadFailure separates the two reasons an operator can act on:
|
|
// 401/403 means the bound key lacks system_runtime_read; everything else
|
|
// (timeout, DNS, connection, upstream error) means the read itself failed.
|
|
func classifyRuntimeReadFailure(err error) error {
|
|
switch weknora.StatusCode(err) {
|
|
case http.StatusUnauthorized, http.StatusForbidden:
|
|
return ErrRuntimeForbidden
|
|
default:
|
|
return ErrRuntimeUnreachable
|
|
}
|
|
}
|
|
|
|
func (p *RuntimeProvider) Queues(ctx context.Context) (*governance.WeKnoraQueueSummary, error) {
|
|
client, err := p.client(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// The overview must not hang for the full business timeout just because the
|
|
// queue endpoint is slow; a short probe is enough to decide null vs data.
|
|
queueCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
queues, err := client.Queues(queueCtx)
|
|
if err != nil {
|
|
return nil, classifyRuntimeReadFailure(err)
|
|
}
|
|
summary := &governance.WeKnoraQueueSummary{
|
|
Available: queues.Available, QueuesError: queues.QueuesError, WorkersError: queues.WorkersError,
|
|
Queues: make([]governance.WeKnoraQueue, 0, len(queues.Queues)), Timestamp: queues.Timestamp,
|
|
}
|
|
for _, queue := range queues.Queues {
|
|
summary.Queues = append(summary.Queues, governance.WeKnoraQueue{
|
|
Name: queue.Name, Pool: queue.Pool, Pending: queue.Pending, Active: queue.Active,
|
|
Scheduled: queue.Scheduled, Retry: queue.Retry, Archived: queue.Archived,
|
|
Processed: queue.Processed, Failed: queue.Failed,
|
|
})
|
|
}
|
|
return summary, nil
|
|
}
|
|
|
|
func (p *RuntimeProvider) DocumentProgress(ctx context.Context, documentID string) (governance.ReconciliationProgress, error) {
|
|
client, err := p.client(ctx)
|
|
if err != nil {
|
|
return governance.ReconciliationProgress{}, err
|
|
}
|
|
detail, err := client.DocumentProgress(ctx, documentID)
|
|
if err != nil {
|
|
if weknora.IsNotFound(err) {
|
|
return governance.ReconciliationProgress{}, err
|
|
}
|
|
return governance.ReconciliationProgress{}, classifyRuntimeReadFailure(err)
|
|
}
|
|
progress := governance.ReconciliationProgress{
|
|
DocumentID: detail.Document.ID, ParseStatus: detail.Document.ParseStatus,
|
|
IngestionStatus: detail.Document.IngestionStatus, WikiStatus: detail.Document.WikiStatus,
|
|
HasMoreFailures: detail.HasMoreFailures, ObservedAt: p.now().UTC(),
|
|
Stages: make([]governance.ReconciliationStage, 0, len(detail.Document.Stages)),
|
|
Failures: make([]governance.ReconciliationFailure, 0, len(detail.Failures)),
|
|
}
|
|
if progress.DocumentID == "" {
|
|
progress.DocumentID = documentID
|
|
}
|
|
for _, stage := range detail.Document.Stages {
|
|
progress.Stages = append(progress.Stages, governance.ReconciliationStage{
|
|
Name: stage.Name, Status: stage.Status, ErrorCode: stage.ErrorCode, FinishedAt: stage.FinishedAt,
|
|
})
|
|
}
|
|
for _, failure := range detail.Failures {
|
|
progress.Failures = append(progress.Failures, governance.ReconciliationFailure{
|
|
TaskType: failure.TaskType, FailCount: failure.FailCount, FailedAt: failure.FailedAt,
|
|
LastError: truncateRunes(failure.LastError, 500),
|
|
})
|
|
}
|
|
return progress, nil
|
|
}
|
|
|
|
func truncateRunes(value string, limit int) string {
|
|
if limit <= 0 {
|
|
return ""
|
|
}
|
|
runes := []rune(value)
|
|
if len(runes) <= limit {
|
|
return value
|
|
}
|
|
return string(runes[:limit])
|
|
}
|