Skip to content

Commit daa6993

Browse files
committed
Fix cache extractor redownloading up-to-date caches for Go Cloud URLs
For pre-signed URLs, the runner helper checks that the `Last-Modified` header of the cache blob is later than the modification time of the downloaded cache file. This avoids having to download an unchanged cache file. This was previously not happening for Go Cloud URLs. This commit adds support for this checking. Note that the Last-Modified time appears to be updated automatically by Go Cloud, so we don't to set this header as we do with presigned URLs in the cache archiver. Changelog: fixed
1 parent 6a28229 commit daa6993

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

commands/helpers/cache_extractor.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ func (c *CacheExtractorCommand) getClient() *CacheClient {
4949
}
5050

5151
func checkIfUpToDate(path string, resp *http.Response) (bool, time.Time) {
52-
fi, _ := os.Lstat(path)
5352
date, _ := time.Parse(http.TimeFormat, resp.Header.Get("Last-Modified"))
54-
return fi != nil && !date.After(fi.ModTime()), date
53+
return isLocalCacheFileUpToDate(path, date), date
54+
}
55+
56+
func isLocalCacheFileUpToDate(path string, date time.Time) bool {
57+
fi, _ := os.Lstat(path)
58+
return fi != nil && !date.After(fi.ModTime())
5559
}
5660

5761
func getRemoteCacheSize(resp *http.Response) int64 {
@@ -145,6 +149,11 @@ func (c *CacheExtractorCommand) handleGoCloudURL() error {
145149
return err
146150
}
147151

152+
if isLocalCacheFileUpToDate(c.File, attrs.ModTime) {
153+
logrus.Infoln(filepath.Base(c.File), "is up to date")
154+
return nil
155+
}
156+
148157
reader, err := b.NewReader(ctx, objectName, nil)
149158
if err != nil {
150159
return err

commands/helpers/cache_extractor_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,25 @@ func TestCacheExtractorRemoteServerFailOnInvalidServer(t *testing.T) {
288288
_, err := os.Stat(cacheExtractorTestArchivedFile)
289289
assert.Error(t, err)
290290
}
291+
292+
func TestIsLocalCacheFileUpToDate(t *testing.T) {
293+
tmpDir := t.TempDir()
294+
cacheFile := path.Join(tmpDir, "cache-file")
295+
296+
// Create cache file
297+
err := os.WriteFile(cacheFile, []byte("test content"), 0644)
298+
require.NoError(t, err)
299+
300+
// Set a specific modification time
301+
modTime := time.Now()
302+
err = os.Chtimes(cacheFile, modTime, modTime)
303+
require.NoError(t, err)
304+
305+
// Test when remote file is older (cache is up to date)
306+
result := isLocalCacheFileUpToDate(cacheFile, modTime.Add(-1*time.Hour))
307+
require.True(t, result, "Cache should be up to date when remote file is older")
308+
309+
// Test when remote file is newer (cache is outdated)
310+
result = isLocalCacheFileUpToDate(cacheFile, modTime.Add(1*time.Hour))
311+
require.False(t, result, "Cache should be outdated when remote file is newer")
312+
}

0 commit comments

Comments
 (0)