Skip to content

Commit 900f83b

Browse files
committed
feat: use cache if download fails
1 parent e4c7426 commit 900f83b

File tree

1 file changed

+46
-33
lines changed

1 file changed

+46
-33
lines changed

taskfile/reader.go

+46-33
Original file line numberDiff line numberDiff line change
@@ -371,50 +371,63 @@ func (r *Reader) readRemoteNodeContent(ctx context.Context, node RemoteNode) ([]
371371
timestamp := cache.ReadTimestamp()
372372
expiry := timestamp.Add(r.cacheExpiryDuration)
373373
cacheValid := now.Before(expiry)
374-
375-
// If we have not been been forced to download, check the cache
376-
if !r.download {
377-
r.debugf("checking cache for %q in %q\n", node.Location(), cache.Location())
378-
b, err := cache.Read()
379-
switch {
380-
// If the cache doesn't exist, we need to download the file
381-
case errors.Is(err, os.ErrNotExist):
382-
r.debugf("no cache found\n")
383-
// If we couldn't find a cached copy, and we are offline, we can't do anything
384-
if r.offline {
385-
return nil, &errors.TaskfileCacheNotFoundError{
386-
URI: node.Location(),
387-
}
374+
var cacheFound bool
375+
376+
r.debugf("checking cache for %q in %q\n", node.Location(), cache.Location())
377+
cachedBytes, err := cache.Read()
378+
switch {
379+
// If the cache doesn't exist, we need to download the file
380+
case errors.Is(err, os.ErrNotExist):
381+
r.debugf("no cache found\n")
382+
// If we couldn't find a cached copy, and we are offline, we can't do anything
383+
if r.offline {
384+
return nil, &errors.TaskfileCacheNotFoundError{
385+
URI: node.Location(),
388386
}
387+
}
389388

390-
// If the cache is expired
391-
case !cacheValid:
392-
r.debugf("cache expired at %s\n", expiry.Format(time.RFC3339))
393-
// If we can't fetch a fresh copy, we should use the cache anyway
394-
if r.offline {
395-
r.debugf("in offline mode, using expired cache\n")
396-
return b, nil
397-
}
389+
// If the cache is expired
390+
case !cacheValid:
391+
r.debugf("cache expired at %s\n", expiry.Format(time.RFC3339))
392+
cacheFound = true
393+
// If we can't fetch a fresh copy, we should use the cache anyway
394+
if r.offline {
395+
r.debugf("in offline mode, using expired cache\n")
396+
return cachedBytes, nil
397+
}
398398

399-
// Some other error
400-
case err != nil:
401-
return nil, err
399+
// Some other error
400+
case err != nil:
401+
return nil, err
402402

403-
// Found valid cache, return it
404-
default:
405-
r.debugf("cache found\n")
406-
return b, nil
403+
// Found valid cache
404+
default:
405+
r.debugf("cache found\n")
406+
// Not being forced to redownload, return cache
407+
if !r.download {
408+
return cachedBytes, nil
407409
}
410+
cacheFound = true
408411
}
409412

410413
// Try to read the remote file
411-
b, err := node.ReadContext(ctx)
414+
r.debugf("downloading remote file: %s\n", node.Location())
415+
downloadedBytes, err := node.ReadContext(ctx)
412416
if err != nil {
417+
// If the context timed out or was cancelled, but we found a cached version, use that
418+
if ctx.Err() != nil && cacheFound {
419+
if cacheValid {
420+
r.debugf("failed to fetch remote file: %s: using cache\n", ctx.Err().Error())
421+
} else {
422+
r.debugf("failed to fetch remote file: %s: using expired cache\n", ctx.Err().Error())
423+
}
424+
return cachedBytes, nil
425+
}
413426
return nil, err
414427
}
415428

416429
r.debugf("found remote file at %q\n", node.Location())
417-
checksum := checksum(b)
430+
checksum := checksum(downloadedBytes)
418431
prompt := cache.ChecksumPrompt(checksum)
419432

420433
// Prompt the user if required
@@ -440,9 +453,9 @@ func (r *Reader) readRemoteNodeContent(ctx context.Context, node RemoteNode) ([]
440453

441454
// Cache the file
442455
r.debugf("caching %q to %q\n", node.Location(), cache.Location())
443-
if err = cache.Write(b); err != nil {
456+
if err = cache.Write(downloadedBytes); err != nil {
444457
return nil, err
445458
}
446459

447-
return b, nil
460+
return downloadedBytes, nil
448461
}

0 commit comments

Comments
 (0)