db update requires own download logic for prefetch, previous logic caused check for updates to use old database and all databases were being updated before all repos were checked for cached pkgs
This commit is contained in:
+4
-2
@@ -77,12 +77,14 @@ func main() {
|
|||||||
mux.HandleFunc("POST /api/refresh", srv.handlerRefresh)
|
mux.HandleFunc("POST /api/refresh", srv.handlerRefresh)
|
||||||
mux.HandleFunc("POST /api/loglevel", srv.handlerLogLevel)
|
mux.HandleFunc("POST /api/loglevel", srv.handlerLogLevel)
|
||||||
|
|
||||||
if err := srv.c.FetchDB(); err != nil {
|
for _, repo := range cfg.MirroredRepos {
|
||||||
slog.Error("failed to refesh db files", "err", err)
|
if err := srv.c.FetchDB(repo); err != nil {
|
||||||
|
slog.Error("failed to refesh db file", "repo", repo, "err", err)
|
||||||
//nolint:errcheck //already exiting
|
//nolint:errcheck //already exiting
|
||||||
_ = c.Close() // best effort cleanup on exit
|
_ = c.Close() // best effort cleanup on exit
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
httpServe := &http.Server{
|
httpServe := &http.Server{
|
||||||
Addr: ":" + srv.cfg.Port,
|
Addr: ":" + srv.cfg.Port,
|
||||||
|
|||||||
Vendored
+47
-17
@@ -1,27 +1,57 @@
|
|||||||
package cache
|
package cache
|
||||||
|
|
||||||
import "path/filepath"
|
import (
|
||||||
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
func (c *Cache) FetchDB() error {
|
func (c *Cache) FetchDB(repo string) error {
|
||||||
if !c.refreshMu.TryLock() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
defer c.refreshMu.Unlock()
|
|
||||||
|
|
||||||
for _, repo := range c.cfg.mirroredRepos {
|
|
||||||
if err := c.fetchDB(repo); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Cache) fetchDB(repo string) error {
|
|
||||||
dbFile := repo + ".db.tar.gz"
|
dbFile := repo + ".db.tar.gz"
|
||||||
dbPath := filepath.Join(repo, "os/x86_64", dbFile)
|
dbPath := filepath.Join(repo, "os/x86_64", dbFile)
|
||||||
_, _, err := c.getStream(dbPath)
|
url := c.nextMirror() + dbPath
|
||||||
|
|
||||||
|
// set the user agent
|
||||||
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("failed create request", "err", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.Header.Set("User-Agent", userAgent)
|
||||||
|
|
||||||
|
resp, err := c.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
slog.Warn("fetch failed", "url", url, "err", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
slog.Info("fetch returned", "url", url, "status", resp.StatusCode)
|
||||||
|
return &UpstreamError{StatusCode: resp.StatusCode}
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
tmpPath := dbPath + ".tmp"
|
||||||
|
tmpFile, err := c.cr.Create(tmpPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, err := io.Copy(tmpFile, resp.Body); err != nil {
|
||||||
|
tmpFile.Close()
|
||||||
|
c.cr.Remove(tmpPath)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tmpFile.Close(); err != nil {
|
||||||
|
c.cr.Remove(tmpPath)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.cr.Rename(tmpPath, dbPath); err != nil {
|
||||||
|
c.cr.Remove(tmpPath)
|
||||||
|
slog.Error("failed to move tmpfile to permanent file", "file", tmpFile, "err", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type CacheClient interface {
|
type CacheClient interface {
|
||||||
FetchDB() error
|
FetchDB(repo string) error
|
||||||
Fetch(relpath string) (*cache.CacheFile, error)
|
Fetch(relpath string) (*cache.CacheFile, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ func (r *RepoSync) Sync() error {
|
|||||||
|
|
||||||
// call cache db fetch
|
// call cache db fetch
|
||||||
slog.Info("refreshing databases")
|
slog.Info("refreshing databases")
|
||||||
if err := r.c.FetchDB(); err != nil {
|
if err := r.c.FetchDB(repo); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ type mockCache struct {
|
|||||||
fetchDBCalled bool
|
fetchDBCalled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockCache) FetchDB() error {
|
func (m *mockCache) FetchDB(repo string) error {
|
||||||
m.fetchDBCalled = true
|
m.fetchDBCalled = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user