diff --git a/src/main/java/com/jfrog/ide/idea/scan/ScanManager.java b/src/main/java/com/jfrog/ide/idea/scan/ScanManager.java index 6aa14719..826ba104 100644 --- a/src/main/java/com/jfrog/ide/idea/scan/ScanManager.java +++ b/src/main/java/com/jfrog/ide/idea/scan/ScanManager.java @@ -31,7 +31,7 @@ import static com.jfrog.ide.common.utils.XrayConnectionUtils.createXrayClientBuilder; public class ScanManager { - private final int SCAN_TIMEOUT_MINUTES = 10; + private final int SCAN_TIMEOUT_MINUTES = 60; private final Project project; private final ScannerFactory factory; private final SourceCodeScannerManager sourceCodeScannerManager; @@ -96,8 +96,9 @@ public void startScan() { scanner.asyncScanAndUpdateResults(); } executor.shutdown(); - //noinspection ResultOfMethodCallIgnored - executor.awaitTermination(SCAN_TIMEOUT_MINUTES, TimeUnit.MINUTES); + if (!executor.awaitTermination(SCAN_TIMEOUT_MINUTES, TimeUnit.MINUTES)) { + logError(Logger.getInstance(), "Scan timeout of " + SCAN_TIMEOUT_MINUTES + " minutes elapsed. The scan is being canceled.", true); + } // Cache tree only if no errors occurred during scan. if (scanners.values().stream().anyMatch(ScannerBase::isScanInterrupted)) { componentsTree.deleteCachedTree(); diff --git a/src/main/java/com/jfrog/ide/idea/scan/ScannerBase.java b/src/main/java/com/jfrog/ide/idea/scan/ScannerBase.java index ce3970f0..a87523f6 100644 --- a/src/main/java/com/jfrog/ide/idea/scan/ScannerBase.java +++ b/src/main/java/com/jfrog/ide/idea/scan/ScannerBase.java @@ -53,6 +53,7 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import static com.jfrog.ide.common.log.Utils.logError; @@ -75,6 +76,7 @@ public abstract class ScannerBase { protected SourceCodeScannerManager sourceCodeScannerManager; String basePath; private ExecutorService executor; + private com.intellij.openapi.progress.ProgressIndicator progressIndicator; /** * @param project currently opened IntelliJ project. We'll use this project to retrieve project based services @@ -295,6 +297,7 @@ public void run(@NotNull com.intellij.openapi.progress.ProgressIndicator indicat log.info("Scan already in progress"); return; } + progressIndicator = indicator; scanAndUpdate(new ProgressIndicatorImpl(indicator)); } @@ -311,7 +314,7 @@ public void onThrowable(@NotNull Throwable error) { } }; - executor.submit(createRunnable(scanAndUpdateTask, latch, this.log)); + executor.submit(createRunnable(scanAndUpdateTask, latch, progressIndicator, log)); } /** @@ -334,12 +337,13 @@ private String getTaskTitle() { /** * Create a runnable to be submitted to the executor service, or run directly. * - * @param task - The task to submit - * @param latch - The countdown latch, which makes sure the executor service doesn't get more than 3 tasks. - * If null, the scan was initiated by a change in the project descriptor and the executor - * service is terminated. In this case, there is no requirement to wait. + * @param task The task to submit + * @param latch The countdown latch, which makes sure the executor service doesn't get more than 3 tasks. + * If null, the scan was initiated by a change in the project descriptor and the executor + * service is terminated. In this case, there is no requirement to wait. + * @param progressIndicator The task's {@link com.intellij.openapi.progress.ProgressIndicator} object. */ - public static Runnable createRunnable(Task.Backgroundable task, CountDownLatch latch, Log log) { + public static Runnable createRunnable(Task.Backgroundable task, CountDownLatch latch, com.intellij.openapi.progress.ProgressIndicator progressIndicator, Log log) { return () -> { // The progress manager is only good for foreground threads. if (SwingUtilities.isEventDispatchThread()) { @@ -354,7 +358,9 @@ public static Runnable createRunnable(Task.Backgroundable task, CountDownLatch l latch.await(); } } catch (InterruptedException e) { - logError(log, ExceptionUtils.getRootCauseMessage(e), e, true); + // This exception is thrown when this thread is interrupted (e.g. when the scan timeout has elapsed). + logError(log, ExceptionUtils.getRootCauseMessage(e), e, false); + progressIndicator.cancel(); } }; } diff --git a/src/main/java/com/jfrog/ide/idea/scan/SourceCodeScannerManager.java b/src/main/java/com/jfrog/ide/idea/scan/SourceCodeScannerManager.java index 610e1b27..020e88f9 100644 --- a/src/main/java/com/jfrog/ide/idea/scan/SourceCodeScannerManager.java +++ b/src/main/java/com/jfrog/ide/idea/scan/SourceCodeScannerManager.java @@ -40,6 +40,7 @@ public class SourceCodeScannerManager { protected Project project; protected PackageManagerType packageType; private static final String SKIP_FOLDERS_SUFFIX = "*/**"; + private com.intellij.openapi.progress.ProgressIndicator progressIndicator; public SourceCodeScannerManager(Project project) { this.project = project; @@ -113,6 +114,7 @@ public void run(@NotNull com.intellij.openapi.progress.ProgressIndicator indicat log.info("Advanced source code scan is already in progress"); return; } + progressIndicator = indicator; sourceCodeScanAndUpdate(new ProgressIndicatorImpl(indicator), ProgressManager::checkCanceled, log); } @@ -128,7 +130,7 @@ public void onThrowable(@NotNull Throwable error) { } }; - executor.submit(createRunnable(sourceCodeScanTask, latch, log)); + executor.submit(createRunnable(sourceCodeScanTask, latch, progressIndicator, log)); } private void sourceCodeScanAndUpdate(ProgressIndicator indicator, Runnable checkCanceled, Logger log) {