From a424ba1bf0ecdb9f13103afaf17c05a27bb5085e Mon Sep 17 00:00:00 2001 From: carlosuc3m <100329787@alumnos.uc3m.es> Date: Thu, 9 Jan 2025 17:53:35 +0100 Subject: [PATCH] improve a lot the waiting time to download engines --- .../bioimageio/download/DownloadTracker.java | 66 +++++++------------ 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/src/main/java/io/bioimage/modelrunner/bioimageio/download/DownloadTracker.java b/src/main/java/io/bioimage/modelrunner/bioimageio/download/DownloadTracker.java index a0aa3cae..2aed4136 100644 --- a/src/main/java/io/bioimage/modelrunner/bioimageio/download/DownloadTracker.java +++ b/src/main/java/io/bioimage/modelrunner/bioimageio/download/DownloadTracker.java @@ -395,11 +395,6 @@ else if (this.sizeFiles == null || this.sizeFiles.keySet().size() == 0) continue; } badDownloads.add(link); - /* TODO - if (consumer.get().get(this.folder + File.separator + name) == null - || consumer.get().get(folder + File.separator + name) != 1.0) - badDownloads.add(link); - */ } catch (MalformedURLException e) { badDownloads.add(link); } @@ -502,47 +497,36 @@ public static TwoParameterConsumer createConsumerProgress() { */ public static void printProgress(Thread downloadThread, DownloadTracker.TwoParameterConsumer consumer) throws InterruptedException { - int n = 30; - String ogProgressStr = ""; - String ogRemainingStr = ""; - for (int i = 0; i < n; i ++) { - ogProgressStr += "#"; - ogRemainingStr += "."; - } Set already = new HashSet(); - boolean keep = true; - while (Thread.currentThread().isAlive() && (downloadThread.isAlive() || keep)) { - boolean end = consumer.get().keySet().contains(TOTAL_PROGRESS_KEY) - && consumer.get().get(TOTAL_PROGRESS_KEY) == 1.0; - int millis = keep == true || end ? 10 : 3000; - keep = false; - //try {Thread.sleep(millis);} catch (InterruptedException ex) {System.out.println("Stopping..."); break;} - Thread.sleep(millis); - String select = null; - for (String key : consumer.get().keySet()) { - if (!already.contains(key) && !key.equals(DownloadTracker.TOTAL_PROGRESS_KEY)) { - select = key; - break; - } + while (Thread.currentThread().isAlive() && downloadThread.isAlive()) { + long waitMillis = 0; + while (consumer.get().get(TOTAL_PROGRESS_KEY) < 1.0 && waitMillis < 3000) { + Thread.sleep(30); + waitMillis += 30; } - if (select == null) { - continue; - } - for (String kk : new String[] {select, DownloadTracker.TOTAL_PROGRESS_KEY}) { - if (kk.endsWith(EngineInstall.NBYTES_SUFFIX)) + for (String key : consumer.get().keySet()) { + if (already.contains(key) || key.equals(DownloadTracker.TOTAL_PROGRESS_KEY) + || key.contains(EngineInstall.NBYTES_SUFFIX)) { continue; - int nProgressBar = (int) (consumer.get().get(kk) * n); - String progressStr = new File(kk).getName() + ": [" - + ogProgressStr.substring(0, nProgressBar) + ogRemainingStr.substring(nProgressBar) - + "] " + Math.round(consumer.get().get(kk) * 100) + "%"; - System.out.println(progressStr); - } - if (consumer.get().get(select) == 1 || consumer.get().get(select) < 0 - || consumer.get().get(TOTAL_PROGRESS_KEY) == 1.0) { - already.add(select); - keep = true; + } + double progress = consumer.get().get(key); + System.out.println(getStringToPrintProgress(key, progress)); + if (progress == 1) already.add(key); + else break; } + double progress = consumer.get().get(DownloadTracker.TOTAL_PROGRESS_KEY); + System.out.println(getStringToPrintProgress(DownloadTracker.TOTAL_PROGRESS_KEY, progress)); } } + + private static String getStringToPrintProgress(String kk, double progress) { + int n = 30; + int nProgressBar = (int) (progress * n); + String progressStr = new File(kk).getName() + ": ["; + for (int i = 0; i < nProgressBar; i ++) progressStr += "#"; + for (int i = nProgressBar; i < n; i ++) progressStr += "."; + progressStr += "] " + Math.round(progress * 100) + "%"; + return progressStr; + } }