Files
knowledge-governance/cmd/worker/library_owned_attempt.go
T
姚章浩 8d838d9cb4
build / build (push) Successful in 1m45s
feat(library): 整库发布身份、写集合与收尾边界
执行时真实时钟与持久授权回读(撤销/过期即停并保留预备份)、写集合写前落盘、
租约与 attempt 身份、恢复队列与原发送身份分离、成功收尾必须释放维护后再登记终态。
2026-11-01 11:35:10 +08:00

41 lines
1.7 KiB
Go

package main
import (
"context"
"knowledge-governance/internal/appconfig"
"knowledge-governance/internal/governance"
"knowledge-governance/internal/jobs"
"knowledge-governance/internal/objectgateway"
postgresstore "knowledge-governance/internal/platform/postgres"
"time"
)
// All target-changing handlers share one execution lock, not one lock per job or
// scope. Lease loss cancels work but cannot release ownership before cleanup joins.
func runOwnedLibraryAttempt(ctx context.Context, base *postgresstore.Store, config appconfig.Config, queue jobs.Job, jobID string, run func(context.Context, *libraryAttemptWriter) error) error {
return base.WithLibraryExecution(ctx, config.LibraryTargetInstance, func(ctx context.Context, ping func(context.Context) error) (err error) {
writer, err := beginLibraryWriter(ctx, base, jobID, libraryQueueOwner(config.WorkerID, queue, jobID))
if err != nil {
return err
}
defer func() {
finishCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
defer cancel()
if err != nil {
writer.finish(finishCtx, governance.LibraryAttemptFailed, err.Error())
} else {
writer.finish(finishCtx, governance.LibraryAttemptCompleted, "")
}
}()
return runWithLibraryLease(ctx, libraryAttemptLease/10, func(probe context.Context) error {
if err := ping(probe); err != nil {
return err
}
return base.RenewLibraryAttempt(probe, writer.attempt, libraryAttemptLease, time.Now().UTC())
}, func(runCtx context.Context) error {
runCtx = objectgateway.WithClaim(runCtx, objectgateway.Claim{JobID: writer.attempt.JobID, OperationID: writer.attempt.OperationID, AttemptEpoch: int64(writer.attempt.Epoch), Owner: writer.attempt.Owner})
return run(runCtx, writer)
})
})
}