Watch
1
0
Fork
You've already forked pkg-proxy
1
mirror of https://github.com/git-pkgs/proxy.git synced 2026-11-09 15:49:16 -05:00
pkg-proxy/internal/server/exclusive.go
Simon Chrzanowski d02ad9b2ba Coordinate instances sharing a Postgres database
Instances started together on an empty database created the schema at the
same time and all but one failed on duplicate types. Schema creation and
migrations now run under a Postgres advisory lock.

Eviction, reclaim, retention and the Gradle build cache sweep take a
per-job advisory lock for each pass, and an instance that finds the job
running elsewhere skips the pass. Before, every instance evicted the whole
overshoot over max_size at once and emptied the cache.

Mirror schedules record their last start in a new job_runs table, so each
schedule runs once per interval instead of once per instance.

Refs #543
2026-10-23 16:15:24 +02:00

21 lines
733 B
Go

package server
import "context"
// runExclusive runs one pass of a background job unless another instance
// sharing the database is running it. That instance does the work, so this
// pass is skipped rather than waited for. Jobs that act on shared state, such
// as eviction against max_size, would otherwise each do all of it.
func (s *Server) runExclusive(ctx context.Context, job string, run func()) {
release, ok, err := s.db.TryLock(ctx, "job:"+job)
if err != nil {
s.logger.Warn("background job: failed to take lock, skipping this pass", "job", job, "error", err)
return
}
if !ok {
s.logger.Debug("background job: running on another instance, skipping this pass", "job", job)
return
}
defer release()
run()
}