Files
knowledge-governance/internal/releaseadapter/v071/library_source_settlement.go
T
姚章浩 bf049cee1f
build / build (push) Successful in 1m12s
fix(snapshot): 暂停前后强制检查共享源全库任务
2026-10-28 01:05:44 +08:00

39 lines
1.6 KiB
Go

package v071
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
)
// A maintenance window stops a shared writer, not a single KB. Check its entire
// source database, including other tenants, deleted scopes and unknown task
// scopes. Restricting this query to the selected KB can interrupt another job.
// This only establishes recorded processing settlement, never Wiki semantics or
// the absence of unrecorded/remote work; CaptureLibrary still requires the
// deployment's ingress and background-worker readiness callback.
func checkLibrarySourceSettlement(ctx context.Context, db librarySnapshotDB) error {
tx, err := db.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.RepeatableRead, AccessMode: pgx.ReadOnly})
if err != nil {
return errors.New("begin library source settlement")
}
defer tx.Rollback(context.WithoutCancel(ctx))
var unsettled, pending, failed int64
if err := tx.QueryRow(ctx, librarySourceSettlementSQL).Scan(&unsettled, &pending, &failed); err != nil {
return errors.New("read library source settlement")
}
if unsettled != 0 || pending != 0 || failed != 0 {
return errors.New("library source has unfinished or failed processing")
}
if err := tx.Commit(ctx); err != nil {
return errors.New("commit library source settlement")
}
return nil
}
const librarySourceSettlementSQL = `SELECT /* library-source-settlement */
(SELECT count(*) FROM knowledges WHERE deleted_at IS NULL AND (parse_status IS DISTINCT FROM 'completed' OR pending_subtasks_count IS NULL OR pending_subtasks_count<>0)),
(SELECT count(*) FROM task_pending_ops),
(SELECT count(*) FROM task_dead_letters)`