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