mirror of
https://github.com/git-pkgs/proxy.git
synced 2026-11-09 15:49:16 -05:00
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
21 lines
733 B
Go
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()
|
|
}
|