From 7f46347c0fa002a78d9f6406464d2d464ad9c7d2 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sat, 26 Oct 2019 05:33:22 +0100 Subject: [PATCH] retry failed downloads --- .../muwire/clilanterna/DownloadsModel.groovy | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/cli-lanterna/src/main/groovy/com/muwire/clilanterna/DownloadsModel.groovy b/cli-lanterna/src/main/groovy/com/muwire/clilanterna/DownloadsModel.groovy index 4a691b58..647ea44d 100644 --- a/cli-lanterna/src/main/groovy/com/muwire/clilanterna/DownloadsModel.groovy +++ b/cli-lanterna/src/main/groovy/com/muwire/clilanterna/DownloadsModel.groovy @@ -16,15 +16,23 @@ class DownloadsModel { private final List downloaders = new ArrayList<>() private final TableModel model = new TableModel("Name", "Status", "Progress", "Speed", "ETA") + + private long lastRetryTime + DownloadsModel(TextGUIThread guiThread, Core core) { this.guiThread = guiThread this.core = core core.eventBus.register(DownloadStartedEvent.class, this) Timer timer = new Timer(true) - Runnable refreshModel = {refreshModel()} + Runnable guiRunnable = { + refreshModel() + resumeDownloads() + } timer.schedule({ - guiThread.invokeLater(refreshModel) + if (core.shutdown.get()) + return + guiThread.invokeLater(guiRunnable) } as TimerTask, 1000,1000) } @@ -63,4 +71,20 @@ class DownloadsModel { } } + + private void resumeDownloads() { + int retryInterval = core.muOptions.downloadRetryInterval + if (retryInterval == 0) + return + retryInterval *= 1000 + long now = System.currentTimeMillis() + if (now - lastRetryTime > retryInterval) { + lastRetryTime = now + downloaders.each { + def state = it.getCurrentState() + if (state == Downloader.DownloadState.FAILED || state == Downloader.DownloadState.DOWNLOADING) + it.resume() + } + } + } }