From 2a6d43238d8d710d917744cd34880b7bfb145e4f Mon Sep 17 00:00:00 2001 From: carlosuc3m <100329787@alumnos.uc3m.es> Date: Wed, 6 Nov 2024 16:45:17 +0100 Subject: [PATCH] help stopping download when thread is interrupted --- .../engine/installation/EngineInstall.java | 66 ++++++++++++++++--- .../engine/installation/FileDownloader.java | 2 +- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/bioimage/modelrunner/engine/installation/EngineInstall.java b/src/main/java/io/bioimage/modelrunner/engine/installation/EngineInstall.java index ecf34992..60c40cb1 100644 --- a/src/main/java/io/bioimage/modelrunner/engine/installation/EngineInstall.java +++ b/src/main/java/io/bioimage/modelrunner/engine/installation/EngineInstall.java @@ -267,6 +267,8 @@ public static EngineInstall createInstaller(String dir) { * Regard, that for certain engines, downloading all the OS depending engines * is necessary, as the dependencies vary from one system to another. * + * If the thread where the installation is happening stops, the method exits without throwing + * an exception. */ private void checkAndSetBasicEngineInstallation() { isManagementFinished = false; @@ -333,6 +335,8 @@ public void checkBasicEngineInstallation() { * Regard, that for certain engines, downloading all the OS depending engines * is necessary, as the dependencies vary from one system to another. * + * If the thread where the installation is happening stops, the method exits without throwing + * an exception. */ public void basicEngineInstallation() { if (!this.everythingInstalled) @@ -522,6 +526,8 @@ private void checkEnginesInstalled() { * Regard, that for certain engines, downloading all the OS depending engines * is necessary, as the dependencies vary from one system to another. * + * If the thread where the installation is happening stops, the method exits without throwing + * an exception. */ private void manageMissingEngines() { if (missingEngineFolders == null) @@ -543,32 +549,53 @@ private void manageMissingEngines() { } /** - * Install the missing engines from scratch + * Install the missing engines from scratch. + * If the thread where the installation is happening stops, the method exits without throwing + * an exception. */ private void installMissingBasicEngines() { if (missingEngineFolders == null) checkEnginesInstalled(); if (missingEngineFolders.entrySet().size() == 0) return; - getBasicDownloadTotalSize(); + try { + getBasicDownloadTotalSize(); + } catch (InterruptedException e) { + return; + } + /** missingEngineFolders = missingEngineFolders.entrySet().stream() .filter(v -> { try { - return!installEngineByCompleteName(v.getValue(), consumersMap.get(v.getValue())); - } catch (IOException | InterruptedException e) { + return !installEngineByCompleteName(v.getValue(), consumersMap.get(v.getValue())); + } catch (IOException e) { + return true; + } catch (InterruptedException e) { return true; } }) .collect(Collectors.toMap(v -> v.getKey(), v -> v.getValue(), (u, v) -> u, LinkedHashMap::new)); + */ + for (Entry v : missingEngineFolders.entrySet()) { + try { + boolean installed = installEngineByCompleteName(v.getValue(), consumersMap.get(v.getValue())); + if (installed) + missingEngineFolders.remove(v.getKey()); + } catch (IOException e) { + } catch (InterruptedException e) { + return; + } + } } /** * * @return the total numebr of bytes to be downloaded in the basic installation. This also prepares the * download tracker by setting the number of bytes per file + * @throws InterruptedException if the thread where this method is being run is interrupted */ - public long getBasicDownloadTotalSize() { + public long getBasicDownloadTotalSize() throws InterruptedException { if (this.consumersMap == null) getBasicEnginesProgress(); long totalSize = 0; @@ -577,6 +604,8 @@ public long getBasicDownloadTotalSize() { long engineSize = 0; DeepLearningVersion dlVersion = DeepLearningVersion.fromFile(new File(ee.getValue())); for (String link : dlVersion.getJars()) { + if (Thread.currentThread().isInterrupted()) + throw new InterruptedException("Retrieval of download size interrupted."); String key = ee.getValue() + File.separator + DownloadModel.getFileNameFromURLString(link) + NBYTES_SUFFIX; long val = DownloadModel.getFileSize(new URL(link)); this.consumersMap.get(ee.getValue()).accept(key, (double) val); @@ -1150,8 +1179,13 @@ public static boolean installEngineInDir(DeepLearningVersion engine, String engi throw new IOException("Unable to create the folder where the engine " + "will be installed: " + folder); + Thread parentThread = Thread.currentThread(); Thread downloadThread = new Thread(() -> { - downloadEngineFiles(engine, folder); + try { + downloadEngineFiles(engine, folder, parentThread); + } catch (InterruptedException e) { + return; + } }); downloadThread.start(); @@ -1180,8 +1214,24 @@ public static boolean installEngineInDir(DeepLearningVersion engine, String engi * engine to be downloaded * @param engineDir * directory where the files will be downloaded + * @throws InterruptedException if the thread where the download is happening is stopped + */ + public static void downloadEngineFiles(DeepLearningVersion engine, String engineDir) throws InterruptedException { + downloadEngineFiles(engine, engineDir, Thread.currentThread()); + } + + /** + * Method that just downloads all the files that form a given engine into the + * directory provided + * @param engine + * engine to be downloaded + * @param engineDir + * directory where the files will be downloaded + * @param parentThread + * thread that when stopped, stops the download + * @throws InterruptedException if the thread where the download is happening is stopped */ - public static void downloadEngineFiles(DeepLearningVersion engine, String engineDir) { + public static void downloadEngineFiles(DeepLearningVersion engine, String engineDir, Thread parentThread) throws InterruptedException { for (String jar : engine.getJars()) { try { URL website = new URL(jar); @@ -1192,7 +1242,7 @@ public static void downloadEngineFiles(DeepLearningVersion engine, String engine try (ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream()); FileOutputStream fos = new FileOutputStream(new File(engineDir, filePath.toString()))){ FileDownloader downloader = new FileDownloader(rbc, fos); - downloader.call(); + downloader.call(parentThread); } catch (IOException e) { conn.disconnect(); String msg = "The link for the file: " + filePath.getFileName() + " is broken." + System.lineSeparator() diff --git a/src/main/java/io/bioimage/modelrunner/engine/installation/FileDownloader.java b/src/main/java/io/bioimage/modelrunner/engine/installation/FileDownloader.java index a81c9da3..49f79842 100644 --- a/src/main/java/io/bioimage/modelrunner/engine/installation/FileDownloader.java +++ b/src/main/java/io/bioimage/modelrunner/engine/installation/FileDownloader.java @@ -59,7 +59,7 @@ public void call(Thread parentThread) throws IOException, InterruptedException { } position += transferred; - if (!parentThread.isAlive()) { + if (parentThread.isInterrupted()) { // Close resources if needed and exit closeResources(); throw new InterruptedException("File download was interrupted.");