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)`