Watch
1
0
Fork
You've already forked pkg-proxy
1
mirror of https://github.com/git-pkgs/proxy.git synced 2026-11-09 23:59:16 -05:00
pkg-proxy/internal/server/mirror_schedule.go
2026-10-24 19:18:21 +02:00

48 lines
1.4 KiB
Go

package server
import (
"context"
"time"
"github.com/git-pkgs/proxy/internal/handler"
"github.com/git-pkgs/proxy/internal/mirror"
)
// mirrorScheduleShutdownWait bounds how long shutdown waits for a scheduled
// run to stop before storage and database are closed under it.
const mirrorScheduleShutdownWait = 30 * time.Second
func (s *Server) startMirrorSchedules(ctx context.Context, proxy *handler.Proxy) {
if len(s.cfg.Mirror.Schedules) == 0 {
return
}
// One worker, so a schedule never fans out against the registry.
m := s.newMirror(proxy, 1)
schedules := make([]mirror.Schedule, len(s.cfg.Mirror.Schedules))
for i, schedule := range s.cfg.Mirror.Schedules {
schedules[i] = mirror.Schedule{PURLs: schedule.Images, Platforms: schedule.Platforms, Interval: schedule.IntervalDuration()}
}
s.scheduler = mirror.NewScheduler(m, schedules, s.logger)
s.scheduler.Claim = s.db.ClaimJobRun
s.scheduler.Release = s.db.ReleaseJobRun
s.scheduler.Start(ctx)
s.logger.Info("mirror schedules enabled", "schedules", len(schedules))
}
func (s *Server) waitForMirrorSchedules(ctx context.Context) {
if s.scheduler == nil {
return
}
ctx, cancel := context.WithTimeout(ctx, mirrorScheduleShutdownWait)
defer cancel()
done := make(chan struct{})
go func() {
s.scheduler.Wait()
close(done)
}()
select {
case <-done:
case <-ctx.Done():
s.logger.Warn("mirror schedule still running at shutdown")
}
}