getAddonRegistryWithFallback retrieves addon data from cache or downloads if stale It respects the UpdateInterval setting from global config
()
| 710 | // getAddonRegistryWithFallback retrieves addon data from cache or downloads if stale |
| 711 | // It respects the UpdateInterval setting from global config |
| 712 | func 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 |
no test coverage detected