automatic resume of failed downloads

This commit is contained in:
Zlatin Balevsky
2019-12-14 14:16:29 +00:00
parent fba0b001c0
commit dd230c4dfc
2 changed files with 42 additions and 0 deletions

View File

@ -2,6 +2,8 @@ package com.muwire.webui;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Stream;
@ -19,8 +21,16 @@ public class DownloadManager {
private final Map<InfoHash,Downloader> downloaders = new ConcurrentHashMap<>();
private final Timer retryTimer = new Timer("download-resumer", true);
private long lastRetryTime;
public DownloadManager(Core core) {
this.core = core;
retryTimer.schedule(new ResumeTask(), 1000, 1000);
}
void shutdown() {
retryTimer.cancel();
}
public void onDownloadStartedEvent(DownloadStartedEvent e) {
@ -72,4 +82,28 @@ public class DownloadManager {
core.getEventBus().publish(event);
delay();
}
private class ResumeTask extends TimerTask {
@Override
public void run() {
if (core.getShutdown().get())
return;
int retryInterval = core.getMuOptions().getDownloadRetryInterval();
if (retryInterval > 0) {
retryInterval *= 1000;
long now = System.currentTimeMillis();
if (now - lastRetryTime > retryInterval) {
lastRetryTime = now;
for (Downloader d : downloaders.values()) {
Downloader.DownloadState state = d.getCurrentState();
if (state == Downloader.DownloadState.FAILED ||
state == Downloader.DownloadState.DOWNLOADING)
d.resume();
}
}
}
}
}
}

View File

@ -40,6 +40,14 @@ public class DownloadServlet extends HttpServlet {
@Override
public void destroy() {
if (downloadManager != null)
downloadManager.shutdown();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (downloadManager == null) {