downloadFile(): Use expired file if the download fails

This commit is contained in:
Eelco Dolstra 2020-03-19 11:42:50 +01:00
parent f6ddf48882
commit 1c127e6a82
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
2 changed files with 19 additions and 6 deletions

View file

@ -25,17 +25,31 @@ DownloadFileResult downloadFile(
auto cached = getCache()->lookupExpired(store, inAttrs); auto cached = getCache()->lookupExpired(store, inAttrs);
if (cached && !cached->expired) auto useCached = [&]() -> DownloadFileResult
{
return { return {
.storePath = std::move(cached->storePath), .storePath = std::move(cached->storePath),
.etag = getStrAttr(cached->infoAttrs, "etag"), .etag = getStrAttr(cached->infoAttrs, "etag"),
.effectiveUrl = getStrAttr(cached->infoAttrs, "url") .effectiveUrl = getStrAttr(cached->infoAttrs, "url")
}; };
};
if (cached && !cached->expired)
return useCached();
DownloadRequest request(url); DownloadRequest request(url);
if (cached) if (cached)
request.expectedETag = getStrAttr(cached->infoAttrs, "etag"); request.expectedETag = getStrAttr(cached->infoAttrs, "etag");
auto res = getDownloader()->download(request); DownloadResult res;
try {
res = getDownloader()->download(request);
} catch (DownloadError & e) {
if (cached) {
warn("%s; using cached version", e.msg());
return useCached();
} else
throw;
}
// FIXME: write to temporary file. // FIXME: write to temporary file.

View file

@ -268,10 +268,9 @@ nix build -o $TEST_ROOT/result $flake3Dir#sth 2>&1 | grep 'unsupported edition'
# Test whether registry caching works. # Test whether registry caching works.
nix flake list --flake-registry file://$registry | grep -q flake3 nix flake list --flake-registry file://$registry | grep -q flake3
# FIXME mv $registry $registry.tmp
#mv $registry $registry.tmp nix flake list --flake-registry file://$registry --refresh | grep -q flake3
#nix flake list --flake-registry file://$registry --refresh | grep -q flake3 mv $registry.tmp $registry
#mv $registry.tmp $registry
# Test whether flakes are registered as GC roots for offline use. # Test whether flakes are registered as GC roots for offline use.
# FIXME: use tarballs rather than git. # FIXME: use tarballs rather than git.