diff --git a/src/main/java/lion/Service/DownloadCheckService.java b/src/main/java/lion/Service/DownloadCheckService.java index 36cf25a..26c579b 100644 --- a/src/main/java/lion/Service/DownloadCheckService.java +++ b/src/main/java/lion/Service/DownloadCheckService.java @@ -124,29 +124,48 @@ public class DownloadCheckService { } } - public boolean addToQueue(GalleryTask galleryTask){ + public GalleryTask addToQueue(GalleryTask galleryTask){ // A reconnect can resend a task whose name is stale (for example, the // downloader appended a resolution suffix). Resolve completed archives // by gid first, because the gid is stable while the directory name is not. File storedDirectory = findStoredDirectoryByGid(galleryTask.getGid()); if(storedDirectory != null){ + queue.remove(galleryTask.getGid()); galleryTask.setName(storedDirectory.getName()); galleryTask.setStatus(GalleryTask.COMPRESS_COMPLETE); CustomUtil.notifyMe(String.format("任务:%s在添加时已下载完成,更新任务状态", galleryTask.getName())); - return true; + return galleryTask; } // If the task is still downloading, retain the actual directory name so // subsequent progress and compression use the same identity. File downloadingDirectory = findDirectoryByGid(new File(downloadPath), galleryTask.getGid()); if(downloadingDirectory != null){ - galleryTask.setName(downloadingDirectory.getName()); - queue.putIfAbsent(galleryTask.getGid(), galleryTask); - return false; + GalleryTask queuedTask = queue.get(galleryTask.getGid()); + if(queuedTask == null){ + refreshDownloadingTask(galleryTask, downloadingDirectory); + queue.put(galleryTask.getGid(), galleryTask); + return galleryTask; + } + if(!queuedTask.is_compressing() && !queuedTask.is_compress_complete()) + refreshDownloadingTask(queuedTask, downloadingDirectory); + return queuedTask; } - queue.putIfAbsent(galleryTask.getGid(), galleryTask); - return false; + GalleryTask queuedTask = queue.putIfAbsent(galleryTask.getGid(), galleryTask); + return queuedTask == null ? galleryTask : queuedTask; + } + + private void refreshDownloadingTask(GalleryTask galleryTask, File downloadingDirectory){ + galleryTask.setName(downloadingDirectory.getName()); + File[] pages = downloadingDirectory.listFiles((dir, name) -> !name.equals("galleryinfo.txt")); + galleryTask.setProceeding(pages == null ? 0 : pages.length); + if(new File(downloadingDirectory, "galleryinfo.txt").isFile()){ + galleryTask.setStatus(GalleryTask.DOWNLOAD_COMPLETE); + galleryTask.setPath(downloadingDirectory.getPath()); + }else{ + galleryTask.setStatus(GalleryTask.DOWNLOADING); + } } private File findStoredDirectoryByGid(int gid){ diff --git a/src/main/java/lion/storageNode.java b/src/main/java/lion/storageNode.java index 37f7aaf..215cd91 100644 --- a/src/main/java/lion/storageNode.java +++ b/src/main/java/lion/storageNode.java @@ -157,12 +157,11 @@ public class storageNode { DownloadPostMessage dpm = (DownloadPostMessage) abstractMessage; lock.lock(); try { - //添加到队列方法返回真说明该任务已下载完成,直接发送下载进度 - if(downloadCheckService.addToQueue(dpm.getGalleryTask())){ - DownloadStatusMessage downloadStatusMessage = new DownloadStatusMessage(); - downloadStatusMessage.setGalleryTasks(new GalleryTask[]{dpm.getGalleryTask()}); - server.writeAndFlush(downloadStatusMessage); - } + //每次收到任务都重新检查并立即回传当前状态,供主站的单任务重试接口使用 + GalleryTask currentTask = downloadCheckService.addToQueue(dpm.getGalleryTask()); + DownloadStatusMessage downloadStatusMessage = new DownloadStatusMessage(); + downloadStatusMessage.setGalleryTasks(new GalleryTask[]{currentTask}); + server.writeAndFlush(downloadStatusMessage); log.info(String.valueOf(queue)); } finally { lock.unlock(); @@ -207,4 +206,4 @@ public class storageNode { } } } -} \ No newline at end of file +}