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