MCPcopy Create free account
hub / github.com/ddev/ddev / getAddonRegistryWithFallback

Function getAddonRegistryWithFallback

pkg/ddevapp/addons.go:712–755  ·  view source on GitHub ↗

getAddonRegistryWithFallback retrieves addon data from cache or downloads if stale It respects the UpdateInterval setting from global config

()

Source from the content-addressed store, hash-verified

710// getAddonRegistryWithFallback retrieves addon data from cache or downloads if stale
711// It respects the UpdateInterval setting from global config
712func getAddonRegistryWithFallback() (*types.AddonData, error) {
713 globalconfig.EnsureGlobalConfig()
714 globalDir := globalconfig.GetGlobalDdevDir()
715 cacheFile := filepath.Join(globalDir, ".addon-data")
716 addonStorage := storage.NewAddonFileStorage(cacheFile)
717
718 // Try to read from cache first
719 cachedData, err := addonStorage.Read()
720
721 // Check if cache is stale
722 cacheIsStale := true
723 if err == nil && len(cachedData.Addons) > 0 {
724 // Get file modification time to check staleness
725 fileInfo, statErr := os.Stat(cacheFile)
726 if statErr == nil {
727 updateInterval := globalconfig.DdevGlobalConfig.RemoteConfig.UpdateInterval
728 if updateInterval == 0 {
729 updateInterval = 24 // Default to 24 hours if not set
730 }
731 staleTime := time.Duration(updateInterval) * time.Hour
732 cacheIsStale = fileInfo.ModTime().Add(staleTime).Before(time.Now())
733 }
734 }
735
736 // If cache is fresh, return it
737 if !cacheIsStale {
738 return cachedData, nil
739 }
740
741 // Cache is stale or missing, try to download fresh data
742 freshData, downloadErr := downloadAddonRegistry()
743 if downloadErr == nil {
744 return freshData, nil
745 }
746
747 // Download failed, return cached data if we have it
748 if err == nil && len(cachedData.Addons) > 0 {
749 // Return stale cache as fallback
750 return cachedData, nil
751 }
752
753 // Both download and cache failed
754 return nil, fmt.Errorf("failed to download add-on registry and no cache available: %w", downloadErr)
755}
756
757// downloadAddonRegistry downloads the add-on registry from the configured URL
758// and caches it in the global config directory

Callers 2

GetAddonTarballURLFunction · 0.85

Calls 6

EnsureGlobalConfigFunction · 0.92
GetGlobalDdevDirFunction · 0.92
NewAddonFileStorageFunction · 0.92
downloadAddonRegistryFunction · 0.85
ErrorfMethod · 0.80
ReadMethod · 0.65

Tested by

no test coverage detected