Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9258aa8273 | |||
| fe7edea1a3 | |||
| f4b3f4c4c8 |
+15
-11
@@ -619,6 +619,21 @@ func workerHandlers(store *postgresstore.Store, config appconfig.Config, kbBindi
|
|||||||
}
|
}
|
||||||
handlers["source.scan.pp"] = handler.Handle
|
handlers["source.scan.pp"] = handler.Handle
|
||||||
}
|
}
|
||||||
|
// 导入对账的作业处理器只依赖对账服务本身(租户 Key 走知识库绑定),
|
||||||
|
// 与旧测试库取件/上传链路(TEST_WEKNORA_ENABLED)无关,必须在下面那个
|
||||||
|
// 早退之前注册;否则 RECONCILIATION_ENABLED=true 时候选只接线了 API,
|
||||||
|
// worker 从不认领 reconciliation.run,批次永远停在 running。
|
||||||
|
if reconciliationService != nil {
|
||||||
|
handlers["reconciliation.run"] = func(handlerCtx context.Context, job jobs.Job) error {
|
||||||
|
var payload struct {
|
||||||
|
RunID string `json:"run_id"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(job.Payload, &payload); err != nil || payload.RunID == "" {
|
||||||
|
return jobs.Permanent(jobs.ErrCodeHandlerFallback, errors.New("invalid reconciliation run payload"))
|
||||||
|
}
|
||||||
|
return reconciliationService.ExecuteRun(handlerCtx, payload.RunID)
|
||||||
|
}
|
||||||
|
}
|
||||||
if !config.TestWeKnoraEnabled || !config.LibraryWorkerRole.ExecutesSourceWork() {
|
if !config.TestWeKnoraEnabled || !config.LibraryWorkerRole.ExecutesSourceWork() {
|
||||||
return handlers, nil
|
return handlers, nil
|
||||||
}
|
}
|
||||||
@@ -695,17 +710,6 @@ func workerHandlers(store *postgresstore.Store, config appconfig.Config, kbBindi
|
|||||||
handlers["test.poll"] = poll.Handle
|
handlers["test.poll"] = poll.Handle
|
||||||
handlers["test.validate"] = validate.Handle
|
handlers["test.validate"] = validate.Handle
|
||||||
handlers["test.remove"] = remove.Handle
|
handlers["test.remove"] = remove.Handle
|
||||||
if reconciliationService != nil {
|
|
||||||
handlers["reconciliation.run"] = func(handlerCtx context.Context, job jobs.Job) error {
|
|
||||||
var payload struct {
|
|
||||||
RunID string `json:"run_id"`
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(job.Payload, &payload); err != nil || payload.RunID == "" {
|
|
||||||
return jobs.Permanent(jobs.ErrCodeHandlerFallback, errors.New("invalid reconciliation run payload"))
|
|
||||||
}
|
|
||||||
return reconciliationService.ExecuteRun(handlerCtx, payload.RunID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return handlers, nil
|
return handlers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -72,16 +72,19 @@ func (s *Store) ClaimScheduledReconciliationRun(ctx context.Context, run governa
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return governance.ReconciliationRun{}, false, err
|
return governance.ReconciliationRun{}, false, err
|
||||||
}
|
}
|
||||||
|
// The KB id is passed twice on purpose: PostgreSQL deduces varchar(255) for
|
||||||
|
// the INSERT target and varchar for the comparison, and one parameter used in
|
||||||
|
// both positions fails with 42P08 "inconsistent types deduced".
|
||||||
row := s.db.QueryRow(ctx, `INSERT INTO reconciliation_runs
|
row := s.db.QueryRow(ctx, `INSERT INTO reconciliation_runs
|
||||||
(id,test_kb_id,state,trigger,started_at,counts,created_at)
|
(id,test_kb_id,state,trigger,started_at,counts,created_at)
|
||||||
SELECT $1,$2,'running','schedule',$3,$4::jsonb,$5
|
SELECT $1,$2,'running','schedule',$3,$4::jsonb,$5
|
||||||
WHERE NOT EXISTS(
|
WHERE NOT EXISTS(
|
||||||
SELECT 1 FROM reconciliation_runs
|
SELECT 1 FROM reconciliation_runs
|
||||||
WHERE test_kb_id=$2 AND trigger='schedule' AND state IN ('running','succeeded','failed') AND started_at >= $6
|
WHERE test_kb_id=$7 AND trigger='schedule' AND state IN ('running','succeeded','failed') AND started_at >= $6
|
||||||
)
|
)
|
||||||
ON CONFLICT DO NOTHING
|
ON CONFLICT DO NOTHING
|
||||||
RETURNING `+reconciliationRunColumns,
|
RETURNING `+reconciliationRunColumns,
|
||||||
run.ID, run.KBID, run.StartedAt, counts, now, since)
|
run.ID, run.KBID, run.StartedAt, counts, now, since, run.KBID)
|
||||||
claimed, err := scanReconciliationRun(row)
|
claimed, err := scanReconciliationRun(row)
|
||||||
if errors.Is(err, pgx.ErrNoRows) {
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
return governance.ReconciliationRun{}, false, nil
|
return governance.ReconciliationRun{}, false, nil
|
||||||
|
|||||||
@@ -214,8 +214,15 @@ func (s *Service) enqueueRun(ctx context.Context, runID string, now time.Time) e
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// jobs.id is VARCHAR(36) and the run id alone already fills it ("rec-" + 32 hex),
|
||||||
|
// so the job carries its own id; idempotency is guaranteed by the unique
|
||||||
|
// idempotency_key below, not by deriving the id from the run.
|
||||||
|
jobID, err := newID("job-")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
_, err = s.queue.Enqueue(ctx, jobs.EnqueueRequest{
|
_, err = s.queue.Enqueue(ctx, jobs.EnqueueRequest{
|
||||||
ID: "job-" + runID,
|
ID: jobID,
|
||||||
Type: "reconciliation.run",
|
Type: "reconciliation.run",
|
||||||
Payload: payload,
|
Payload: payload,
|
||||||
Priority: 5,
|
Priority: 5,
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"knowledge-governance/internal/governance"
|
"knowledge-governance/internal/governance"
|
||||||
|
"knowledge-governance/internal/jobs"
|
||||||
"knowledge-governance/internal/platform/weknora"
|
"knowledge-governance/internal/platform/weknora"
|
||||||
"knowledge-governance/internal/readiness"
|
"knowledge-governance/internal/readiness"
|
||||||
"knowledge-governance/internal/weknoraaccess"
|
"knowledge-governance/internal/weknoraaccess"
|
||||||
@@ -60,6 +61,7 @@ type fakeReconciliationStore struct {
|
|||||||
removeHit bool
|
removeHit bool
|
||||||
requeueDoc string
|
requeueDoc string
|
||||||
orphan bool
|
orphan bool
|
||||||
|
enqueued []jobs.EnqueueRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeReconciliationStore) ReapStaleReconciliationRuns(context.Context, time.Time, time.Duration) (int64, error) {
|
func (f *fakeReconciliationStore) ReapStaleReconciliationRuns(context.Context, time.Time, time.Duration) (int64, error) {
|
||||||
@@ -133,6 +135,11 @@ func (f *fakeReconciliationStore) MarkReconciliationOrphanRemoved(context.Contex
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *fakeReconciliationStore) Enqueue(_ context.Context, request jobs.EnqueueRequest, _ time.Time) (*jobs.Job, error) {
|
||||||
|
f.enqueued = append(f.enqueued, request)
|
||||||
|
return &jobs.Job{ID: request.ID}, nil
|
||||||
|
}
|
||||||
|
|
||||||
type fakeReconciliationRemote struct {
|
type fakeReconciliationRemote struct {
|
||||||
items []weknora.Knowledge
|
items []weknora.Knowledge
|
||||||
missing map[string]bool
|
missing map[string]bool
|
||||||
@@ -538,3 +545,38 @@ func TestExecuteRunR6UsesRemovalCandidatesViewOnly(t *testing.T) {
|
|||||||
t.Fatalf("run counts R6 = %#v", store.completedRun)
|
t.Fatalf("run counts R6 = %#v", store.completedRun)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestCreateRunEnqueuesJobWithinJobIDLimit freezes the queue contract: jobs.id is
|
||||||
|
// VARCHAR(36), and the run id ("rec-" + 32 hex) already fills it. Deriving the job
|
||||||
|
// id from the run id overflowed the column, so every batch was created `failed`
|
||||||
|
// with queue_unavailable before a worker ever saw it.
|
||||||
|
func TestCreateRunEnqueuesJobWithinJobIDLimit(t *testing.T) {
|
||||||
|
now := time.Date(2026, 12, 1, 12, 0, 0, 0, time.UTC)
|
||||||
|
store := &fakeReconciliationStore{
|
||||||
|
run: governance.ReconciliationRun{ID: "rec-unused", KBID: "kb-1", State: "running", Trigger: "manual", StartedAt: now},
|
||||||
|
}
|
||||||
|
service, err := New(Config{Store: store, Queue: store, Remote: &fakeReconciliationRemote{},
|
||||||
|
Now: func() time.Time { return now }, Grace: 30 * time.Minute, StallAfter: 2 * time.Hour,
|
||||||
|
StaleAfter: 6 * time.Hour, ConfirmConcurrency: 1, KeepRuns: 30, MaxAge: 90 * 24 * time.Hour,
|
||||||
|
WeKnoraWebBase: func(context.Context) string { return "https://test.example/" }})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
run, err := service.CreateRun(context.Background(), "kb-1", "manual")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateRun error = %v", err)
|
||||||
|
}
|
||||||
|
if len(store.enqueued) != 1 {
|
||||||
|
t.Fatalf("enqueued %d jobs", len(store.enqueued))
|
||||||
|
}
|
||||||
|
request := store.enqueued[0]
|
||||||
|
if len(request.ID) != 36 {
|
||||||
|
t.Fatalf("job id %q length = %d, want exactly 36 (VARCHAR(36))", request.ID, len(request.ID))
|
||||||
|
}
|
||||||
|
if request.Type != "reconciliation.run" || request.IdempotencyKey != "reconciliation-run:"+run.ID {
|
||||||
|
t.Fatalf("enqueue request = %#v", request)
|
||||||
|
}
|
||||||
|
if string(request.Payload) != `{"run_id":"`+run.ID+`"}` {
|
||||||
|
t.Fatalf("payload = %s", request.Payload)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user