4528a55e6a
为什么:T6a 需要一张批次/差异表、全量只读扫描、R1-R8 判定和 finding 级处置;现有解析监控只转述台账状态,无法回答库内是否一致。 做什么:迁移 000084(upload_md5、runs/findings/actions、共享 removal_candidates 视图);WeKnora 字段与 1000/页扫描;runtime 只读客户端(0400 Key、503 降级);扫描/矩阵/动作/定时/僵死回收;9 条接口与 RBAC;overview 队列与删两张常量卡。 边界:只对测试库;对账零写请求;R1/R2 单条确认;R6 复用 /removal;R2 只管理员;生产链路未改。 验证:make verify 全绿(17 项);新增矩阵/扫描 R1-R8/零写/失败整批/动作/运行时/密钥权限测试。
155 lines
5.5 KiB
Go
155 lines
5.5 KiB
Go
package testweknora
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/governance"
|
|
"knowledge-governance/internal/jobs"
|
|
postgresstore "knowledge-governance/internal/platform/postgres"
|
|
"knowledge-governance/internal/platform/weknora"
|
|
)
|
|
|
|
var (
|
|
ErrInvalidPollJobPayload = errors.New("test_poll_invalid_payload")
|
|
ErrPollRemoteFailed = errors.New("test_poll_remote_failed")
|
|
ErrPollRemoteIdentity = errors.New("test_poll_remote_identity_invalid")
|
|
ErrPollStateConflict = errors.New("test_poll_state_conflict")
|
|
)
|
|
|
|
type PollStore interface {
|
|
LoadPublicationForPoll(context.Context, string) (postgresstore.PublicationForPoll, error)
|
|
RecordRemoteStatusAndScheduleNextPoll(context.Context, string, int, governance.ProcessingState, *time.Time, time.Time, time.Time) (int, error)
|
|
RecordCompletedAndScheduleValidation(context.Context, string, int, *time.Time, time.Time) error
|
|
RecordProcessingFailure(context.Context, string, int, string, time.Time) error
|
|
MarkStalled(context.Context, string, int, time.Time, time.Time) error
|
|
}
|
|
|
|
type PollClient interface {
|
|
GetKnowledge(context.Context, string) (weknora.Knowledge, error)
|
|
}
|
|
|
|
type PollHandlerConfig struct {
|
|
Store PollStore
|
|
Client PollClient
|
|
PollDelay time.Duration
|
|
StallAfter time.Duration
|
|
// StallAfterSource 可选:返回当前生效的停滞阈值(来自活动 UI 配置)。
|
|
StallAfterSource func() time.Duration
|
|
Now func() time.Time
|
|
}
|
|
|
|
type PollHandler struct {
|
|
store PollStore
|
|
client PollClient
|
|
pollDelay time.Duration
|
|
stallAfter time.Duration
|
|
// stallAfterSource 存在时优先于 stallAfter:页面保存的“解析停滞阈值”
|
|
// 必须真的决定判定,而不是只写进数据库。
|
|
stallAfterSource func() time.Duration
|
|
now func() time.Time
|
|
}
|
|
|
|
type pollPayload struct {
|
|
PublicationID string `json:"publication_id"`
|
|
Sequence int `json:"sequence"`
|
|
}
|
|
|
|
func NewPollHandler(config PollHandlerConfig) (*PollHandler, error) {
|
|
if config.Store == nil || config.Client == nil || config.PollDelay <= 0 || config.StallAfter <= 0 {
|
|
return nil, errors.New("invalid test poll handler configuration")
|
|
}
|
|
if config.Now == nil {
|
|
config.Now = time.Now
|
|
}
|
|
return &PollHandler{
|
|
store: config.Store, client: config.Client, pollDelay: config.PollDelay,
|
|
stallAfter: config.StallAfter, stallAfterSource: config.StallAfterSource, now: config.Now,
|
|
}, nil
|
|
}
|
|
|
|
func (handler *PollHandler) Handle(ctx context.Context, job jobs.Job) error {
|
|
var payload pollPayload
|
|
if err := decodeStrictJSON(job.Payload, &payload); err != nil || payload.PublicationID == "" || payload.Sequence < 1 {
|
|
return jobs.Permanent(jobs.ErrCodeHandlerFallback, ErrInvalidPollJobPayload)
|
|
}
|
|
publication, err := handler.store.LoadPublicationForPoll(ctx, payload.PublicationID)
|
|
if err != nil {
|
|
return ErrPollStateConflict
|
|
}
|
|
if publication.KnowledgeID == "" || publication.PollSequence != payload.Sequence {
|
|
return ErrPollStateConflict
|
|
}
|
|
now := handler.now()
|
|
ctx, identity, hasProvider, identityErr := resolveIdentity(ctx, handler.client, publication.TestKBID)
|
|
if identityErr != nil {
|
|
return ErrPollRemoteFailed
|
|
}
|
|
if hasProvider && !identityMatches(publication.TestInstanceURL, publication.TestInstanceTenant, identity) {
|
|
// 旧对象属于另一个实例/租户:不能在新实例上按同名/同 ID 的对象继续推进。
|
|
if err := handler.store.RecordProcessingFailure(ctx, publication.ID, publication.PollSequence, "remote_instance_changed", now); err != nil {
|
|
return ErrPollStateConflict
|
|
}
|
|
return nil
|
|
}
|
|
knowledge, err := handler.client.GetKnowledge(ctx, publication.KnowledgeID)
|
|
if err != nil {
|
|
return ErrPollRemoteFailed
|
|
}
|
|
if knowledge.ID != publication.KnowledgeID || knowledge.KnowledgeBaseID != publication.TestKBID {
|
|
return ErrPollRemoteIdentity
|
|
}
|
|
state := mapParseStatus(knowledge.ParseStatus)
|
|
if state == governance.ProcessingCompleted {
|
|
if err := handler.store.RecordCompletedAndScheduleValidation(ctx, publication.ID, publication.PollSequence, &knowledge.UpdatedAt, now); err != nil {
|
|
return ErrPollStateConflict
|
|
}
|
|
return nil
|
|
}
|
|
if state == governance.ProcessingFailed {
|
|
if err := handler.store.RecordProcessingFailure(ctx, publication.ID, publication.PollSequence, "remote_parse_failed", now); err != nil {
|
|
return ErrPollStateConflict
|
|
}
|
|
return nil
|
|
}
|
|
if !state.Valid() {
|
|
if err := handler.store.RecordProcessingFailure(ctx, publication.ID, publication.PollSequence, "remote_unknown_status", now); err != nil {
|
|
return ErrPollStateConflict
|
|
}
|
|
return nil
|
|
}
|
|
stall := handler.stallAfter
|
|
if handler.stallAfterSource != nil {
|
|
if fromSettings := handler.stallAfterSource(); fromSettings > 0 {
|
|
stall = fromSettings
|
|
}
|
|
}
|
|
cutoff := now.Add(-stall)
|
|
lastProgress := publication.LastProgressAt
|
|
if lastProgress == nil {
|
|
lastProgress = publication.ProcessingStartedAt
|
|
}
|
|
if lastProgress == nil {
|
|
lastProgress = publication.LastObservedAt
|
|
}
|
|
if lastProgress != nil && !lastProgress.After(cutoff) {
|
|
if err := handler.store.MarkStalled(ctx, publication.ID, publication.PollSequence, cutoff, now); err != nil {
|
|
return ErrPollStateConflict
|
|
}
|
|
return nil
|
|
}
|
|
if _, err := handler.store.RecordRemoteStatusAndScheduleNextPoll(ctx, publication.ID, publication.PollSequence, state, &knowledge.UpdatedAt, now, now.Add(handler.pollDelay)); err != nil {
|
|
return ErrPollStateConflict
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func mapParseStatus(status string) governance.ProcessingState {
|
|
normalized, ok := governance.NormalizeWeKnoraParseStatus(status)
|
|
if !ok {
|
|
return governance.ProcessingState(status)
|
|
}
|
|
return governance.ProcessingState(normalized)
|
|
}
|