safely remove status timer on retry

This commit is contained in:
Jonathan Shook 2024-06-26 10:29:32 -05:00
parent e935600a01
commit 8ce9cba559

View File

@ -58,11 +58,13 @@ public class ResolverForNBIOCache implements ContentResolver {
/** /**
* This method is used to resolve the path of a given URI. * This method is used to resolve the path of a given URI.
* It first checks if the URI has a scheme (http or https) and if it does, it tries to resolve the path from the cache. * It first checks if the URI has a scheme (http or https) and if it does, it tries to resolve the path from the
* cache.
* If the file is not in the cache, it tries to download it from the remote URL. * If the file is not in the cache, it tries to download it from the remote URL.
* If the URI does not have a scheme, it returns null. * If the URI does not have a scheme, it returns null.
* *
* @param uri the URI to resolve the path for * @param uri
* the URI to resolve the path for
* @return the resolved Path object, or null if the URI does not have a scheme or the path could not be resolved * @return the resolved Path object, or null if the URI does not have a scheme or the path could not be resolved
*/ */
private Path resolvePath(URI uri) { private Path resolvePath(URI uri) {
@ -72,8 +74,7 @@ public class ResolverForNBIOCache implements ContentResolver {
Path cachedFilePath = Path.of(cacheDir + uri.getPath()); Path cachedFilePath = Path.of(cacheDir + uri.getPath());
if (Files.isReadable(cachedFilePath)) { if (Files.isReadable(cachedFilePath)) {
return pathFromLocalCache(cachedFilePath, uri); return pathFromLocalCache(cachedFilePath, uri);
} } else {
else {
return pathFromRemoteUrl(uri); return pathFromRemoteUrl(uri);
} }
} }
@ -109,6 +110,8 @@ public class ResolverForNBIOCache implements ContentResolver {
while (retries < maxRetries) { while (retries < maxRetries) {
try { try {
if (this.remoteFileExists(uri)) { if (this.remoteFileExists(uri)) {
Timer timer = new Timer();
try {
logger.info(() -> "Downloading remote file " + uri + " to cache at " + cachedFilePath); logger.info(() -> "Downloading remote file " + uri + " to cache at " + cachedFilePath);
ReadableByteChannel channel = Channels.newChannel(uri.toURL().openStream()); ReadableByteChannel channel = Channels.newChannel(uri.toURL().openStream());
FileOutputStream outputStream = new FileOutputStream(cachedFilePath.toFile()); FileOutputStream outputStream = new FileOutputStream(cachedFilePath.toFile());
@ -117,7 +120,6 @@ public class ResolverForNBIOCache implements ContentResolver {
FileChannel fileChannel = outputStream.getChannel(); FileChannel fileChannel = outputStream.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(32768); ByteBuffer buffer = ByteBuffer.allocate(32768);
Timer timer = new Timer();
ProgressPrinter printer = new ProgressPrinter(fileSize, 0); ProgressPrinter printer = new ProgressPrinter(fileSize, 0);
timer.scheduleAtFixedRate(printer, 2000, 2000); timer.scheduleAtFixedRate(printer, 2000, 2000);
while (channel.read(buffer) != -1) { while (channel.read(buffer) != -1) {
@ -133,12 +135,16 @@ public class ResolverForNBIOCache implements ContentResolver {
success = true; success = true;
break; break;
} }
} finally {
timer.cancel();
}
} else { } else {
logger.error(() -> "Error downloading remote file to cache at " + cachedFilePath + ", retrying..."); logger.error(() -> "Error downloading remote file to cache at " + cachedFilePath + ", retrying...");
retries++; retries++;
} }
} catch (IOException e) { } catch (IOException e) {
logger.error(() -> "Error downloading remote file to cache at " + cachedFilePath + ", retrying..."); logger.error(() -> "Error downloading remote file to cache at " + cachedFilePath + ":" + e + ", " +
"retrying...");
retries++; retries++;
} }
} }
@ -174,19 +180,21 @@ public class ResolverForNBIOCache implements ContentResolver {
* Then it tries to download the file and if successful, it generates a SHA256 checksum for the downloaded file. * Then it tries to download the file and if successful, it generates a SHA256 checksum for the downloaded file.
* It then compares the generated checksum with the remote checksum. * It then compares the generated checksum with the remote checksum.
* If the checksums match, it returns the path to the cached file. * If the checksums match, it returns the path to the cached file.
* If the checksums don't match or if there was an error during the download, it cleans up the cache and throws a RuntimeException. * If the checksums don't match or if there was an error during the download, it cleans up the cache and throws a
* RuntimeException.
* *
* @param uri the URI of the remote file to download * @param uri
* the URI of the remote file to download
* @return the Path to the downloaded file in the local cache * @return the Path to the downloaded file in the local cache
* @throws RuntimeException if there was an error during the download or if the checksums don't match * @throws RuntimeException
* if there was an error during the download or if the checksums don't match
*/ */
private Path pathFromRemoteUrl(URI uri) { private Path pathFromRemoteUrl(URI uri) {
Path cachedFilePath = Path.of(cacheDir + uri.getPath()); Path cachedFilePath = Path.of(cacheDir + uri.getPath());
createCacheDir(cachedFilePath); createCacheDir(cachedFilePath);
if (!verifyChecksum) { if (!verifyChecksum) {
return execute(NBIOResolverConditions.UPDATE_NO_VERIFY, cachedFilePath, uri); return execute(NBIOResolverConditions.UPDATE_NO_VERIFY, cachedFilePath, uri);
} } else {
else {
return execute(NBIOResolverConditions.UPDATE_AND_VERIFY, cachedFilePath, uri); return execute(NBIOResolverConditions.UPDATE_AND_VERIFY, cachedFilePath, uri);
} }
} }
@ -244,8 +252,7 @@ public class ResolverForNBIOCache implements ContentResolver {
if (localChecksum.equals(remoteChecksum)) { if (localChecksum.equals(remoteChecksum)) {
logger.info(() -> "Checksums match, returning cached file " + cachedFilePath); logger.info(() -> "Checksums match, returning cached file " + cachedFilePath);
return cachedFilePath; return cachedFilePath;
} } else {
else {
logger.warn(() -> "Checksums do not match, rehydrating cache " + cachedFilePath); logger.warn(() -> "Checksums do not match, rehydrating cache " + cachedFilePath);
return pathFromRemoteUrl(uri); return pathFromRemoteUrl(uri);
} }
@ -269,10 +276,13 @@ public class ResolverForNBIOCache implements ContentResolver {
* If the checksums don't match, it deletes the cached file and downloads it from the remote URL. * If the checksums don't match, it deletes the cached file and downloads it from the remote URL.
* If the remote file or checksum does not exist, it returns the cached file. * If the remote file or checksum does not exist, it returns the cached file.
* *
* @param cachedFilePath the Path to the cached file * @param cachedFilePath
* @param uri the URI of the remote file * the Path to the cached file
* @param uri
* the URI of the remote file
* @return the Path to the cached file * @return the Path to the cached file
* @throws RuntimeException if there was an error during the checksum comparison or if the checksums don't match * @throws RuntimeException
* if there was an error during the checksum comparison or if the checksums don't match
*/ */
private Path pathFromLocalCache(Path cachedFilePath, URI uri) { private Path pathFromLocalCache(Path cachedFilePath, URI uri) {