diff --git a/plugins/maven-server-api/src/main/java/org/jetbrains/idea/maven/server/MavenIndexUpdateState.java b/plugins/maven-server-api/src/main/java/org/jetbrains/idea/maven/server/MavenIndexUpdateState.java index 537f75df7a47..20837a0e35e2 100644 --- a/plugins/maven-server-api/src/main/java/org/jetbrains/idea/maven/server/MavenIndexUpdateState.java +++ b/plugins/maven-server-api/src/main/java/org/jetbrains/idea/maven/server/MavenIndexUpdateState.java @@ -30,6 +30,7 @@ public class MavenIndexUpdateState implements Serializable { public enum State { INDEXING, SUCCEED, + CANCELLED, FAILED } } diff --git a/plugins/maven/maven-server-indexer/src/org/jetbrains/idea/maven/server/indexer/WagonTransferListenerAdapter.java b/plugins/maven/maven-server-indexer/src/org/jetbrains/idea/maven/server/indexer/WagonTransferListenerAdapter.java index 882a4a773574..4053a9adb1a0 100644 --- a/plugins/maven/maven-server-indexer/src/org/jetbrains/idea/maven/server/indexer/WagonTransferListenerAdapter.java +++ b/plugins/maven/maven-server-indexer/src/org/jetbrains/idea/maven/server/indexer/WagonTransferListenerAdapter.java @@ -83,7 +83,7 @@ public class WagonTransferListenerAdapter implements TransferListener { @Override public void debug(String s) { - checkCanceled(); + } private void updateProgress(String resourceName, DownloadData data) { @@ -100,7 +100,10 @@ public class WagonTransferListenerAdapter implements TransferListener { sizeInfo = StringUtilRt.formatFileSize(data.downloaded); } else { - sizeInfo = ((int)100f * data.downloaded / data.total) + "% of " + StringUtilRt.formatFileSize(data.total); + float fraction = (float)data.downloaded / (float)data.total; + String percentHumanReadable = String.format("%.2f", fraction * 100.0); + sizeInfo = + StringUtilRt.formatFileSize(data.downloaded) + " - " + percentHumanReadable + "% of " + StringUtilRt.formatFileSize(data.total); } try { diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenLuceneClassIndexServer.kt b/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenLuceneClassIndexServer.kt index d1798200d7b8..ed22153c19c8 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenLuceneClassIndexServer.kt +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenLuceneClassIndexServer.kt @@ -35,8 +35,8 @@ class MavenLuceneClassIndexServer(private val myRepo: MavenRepositoryInfo, return myNexusIndexer.search(myIndexId, pattern, maxResult) } - override suspend fun update(indicator: MavenProgressIndicator, explicit: Boolean) { - myNexusIndexer.updateIndex(myIndexId, indicator, explicit) + override fun updateOrRepair(fullUpdate: Boolean, progress: MavenProgressIndicator, explicit: Boolean) { + myNexusIndexer.updateIndex(myIndexId, progress, explicit) myUpdateTimestamp = System.currentTimeMillis() } diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenRepositoriesConfigurable.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenRepositoriesConfigurable.java index ca5e7374da56..d6f95b70844e 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenRepositoriesConfigurable.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenRepositoriesConfigurable.java @@ -87,7 +87,9 @@ public class MavenRepositoriesConfigurable implements SearchableConfigurable, Co private void doUpdateIndex() { MavenRepositoryInfo repositoryInfo = getSelectedIndices().stream().findFirst().orElse(null); - MavenSystemIndicesManager.getInstance().updateIndexContentFromEDT(repositoryInfo); + if (repositoryInfo != null) { + MavenSystemIndicesManager.getInstance().updateIndexContent(repositoryInfo, myProject); + } } private List getSelectedIndices() { diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenSystemIndicesManager.kt b/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenSystemIndicesManager.kt index 556e77e0a7f3..72d15801f3ec 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenSystemIndicesManager.kt +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenSystemIndicesManager.kt @@ -1,15 +1,19 @@ // Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.idea.maven.indices -import com.intellij.ide.AppLifecycleListener import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.ReadAction import com.intellij.openapi.components.* -import com.intellij.openapi.progress.* +import com.intellij.openapi.progress.ProcessCanceledException +import com.intellij.openapi.progress.ProgressManager +import com.intellij.openapi.progress.blockingContext +import com.intellij.openapi.progress.blockingContextToIndicator import com.intellij.openapi.project.Project import com.intellij.openapi.project.ProjectCloseListener import com.intellij.openapi.project.getOpenedProjects import com.intellij.openapi.util.registry.Registry +import com.intellij.platform.ide.progress.TaskCancellation +import com.intellij.platform.ide.progress.withBackgroundProgress import com.intellij.util.PathUtilRt import com.intellij.util.messages.Topic import com.intellij.util.xmlb.annotations.OptionTag @@ -59,28 +63,6 @@ class MavenSystemIndicesManager(val cs: CoroutineScope) : PersistentStateCompone init { - cs.launch { - while (isActive) { - delay(2000) - val statusToSend = ArrayList() - if (!needPoll) continue - var anyInProgress = false - status().forEach { s -> - anyInProgress = anyInProgress || s.myState == MavenIndexUpdateState.State.INDEXING - val oldStatus = luceneUpdateStatusMap[s.myUrl] - if (oldStatus == null || oldStatus.timestamp < s.timestamp) { - statusToSend.add(s) - luceneUpdateStatusMap[s.myUrl] = s - } - } - - statusToSend.forEach { - ApplicationManager.getApplication().messageBus.syncPublisher(TOPIC).indexStatusChanged(it) - } - needPoll = anyInProgress - } - - } ApplicationManager.getApplication().messageBus.connect().subscribe(ProjectCloseListener.TOPIC, object : ProjectCloseListener { override fun projectClosed(project: Project) { @@ -127,61 +109,6 @@ class MavenSystemIndicesManager(val cs: CoroutineScope) : PersistentStateCompone return ourTestIndicesDir ?: MavenUtil.getPluginSystemDir("Indices") } - fun getIndexForRepoSync(repo: MavenRepositoryInfo): MavenSearchIndex { - return runBlockingMaybeCancellable { - getIndexForRepo(repo) - } - } - - fun updateIndexContentSync(repo: MavenRepositoryInfo, - fullUpdate: Boolean, - explicit: Boolean, - indicator: MavenProgressIndicator) { - return runBlockingMaybeCancellable { - updateLuceneIndexContent(repo, fullUpdate, explicit, indicator) - } - } - - private suspend fun updateLuceneIndexContent(repo: MavenRepositoryInfo, - fullUpdate: Boolean, - explicit: Boolean, - indicator: MavenProgressIndicator) { - - coroutineScope { - - val updateScope = this - val connection = ApplicationManager.getApplication().messageBus.connect(updateScope) - connection.subscribe(AppLifecycleListener.TOPIC, object : AppLifecycleListener { - override fun appClosing() { - updateScope.cancel() - indicator.cancel() - MavenLog.LOG.info("Application is closing, gracefully shutdown all indexing operations") - } - }) - - startUpdateLuceneIndex(repo) - } - - - } - - fun startUpdateLuceneIndex(repo: MavenRepositoryInfo) { - val indexFile = getDirForMavenIndex(repo).toFile() - val status = getIndexWrapper().startIndexing(repo, indexFile) - needPoll = true - if (status != null) { - luceneUpdateStatusMap[repo.url] = status - ApplicationManager.getApplication().messageBus.syncPublisher(TOPIC).indexStatusChanged(status) - } - } - - fun stopIndexing(repo: MavenRepositoryInfo) { - getIndexWrapper().stopIndexing(repo) - } - - fun status(): List = getIndexWrapper().status() - - private suspend fun getIndexForRepo(repo: MavenRepositoryInfo): MavenSearchIndex { return cs.async(Dispatchers.IO) { val dir = getDirForMavenIndex(repo) @@ -380,19 +307,50 @@ class MavenSystemIndicesManager(val cs: CoroutineScope) : PersistentStateCompone return inMemoryIndices.values.toImmutableList() } - fun updateIndexContentFromEDT(repositoryInfo: MavenRepositoryInfo) { - val task = object : Task.Backgroundable(null, IndicesBundle.message("maven.indices.updating"), true) { - override fun run(indicator: ProgressIndicator) { - val mavenIndicator = MavenProgressIndicator(null, indicator, null) - runBlockingCancellable { - val mavenIndex = getClassIndexForRepository(repositoryInfo) - (mavenIndex as? MavenUpdatableIndex)?.update(mavenIndicator, true) + fun updateIndexContent(repositoryInfo: MavenRepositoryInfo, project: Project) { + cs.launch(Dispatchers.IO) { + withBackgroundProgress(project, IndicesBundle.message("maven.indices.updating.for.repo", repositoryInfo.url), TaskCancellation.cancellable()) { + val mavenIndex = getClassIndexForRepository(repositoryInfo) + blockingContext { + ApplicationManager.getApplication().messageBus.syncPublisher(TOPIC).indexStatusChanged( + MavenIndexUpdateState(repositoryInfo.url, + null, + IndicesBundle.message("maven.indices.updating.for.repo", repositoryInfo.url), + MavenIndexUpdateState.State.INDEXING)) + + blockingContextToIndicator { + val indicator = MavenProgressIndicator(null, ProgressManager.getInstance().progressIndicator, null) + try { + (mavenIndex as? MavenUpdatableIndex)?.updateOrRepair(true, indicator, true) + ApplicationManager.getApplication().messageBus.syncPublisher(TOPIC).indexStatusChanged( + MavenIndexUpdateState(repositoryInfo.url, + null, + IndicesBundle.message("maven.indices.updated.for.repo", repositoryInfo.url), + MavenIndexUpdateState.State.SUCCEED)) + } + catch (e: MavenProcessCanceledException) { + ApplicationManager.getApplication().messageBus.syncPublisher(TOPIC).indexStatusChanged( + MavenIndexUpdateState(repositoryInfo.url, + e.message, + IndicesBundle.message("maven.indices.updated.for.repo", repositoryInfo.url), + MavenIndexUpdateState.State.CANCELLED)) + } + catch (e: Exception) { + if (e !is ProcessCanceledException) { + MavenLog.LOG.warn(e) + } + ApplicationManager.getApplication().messageBus.syncPublisher(TOPIC).indexStatusChanged( + MavenIndexUpdateState(repositoryInfo.url, + e.message, + IndicesBundle.message("maven.index.updated.error"), + MavenIndexUpdateState.State.FAILED)) + } + + } } } } - ProgressManager.getInstance().run(task) - } companion object { diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenUpdatableIndex.kt b/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenUpdatableIndex.kt index ad7d75cd3e6c..ce597a934577 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenUpdatableIndex.kt +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenUpdatableIndex.kt @@ -1,13 +1,17 @@ // Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.idea.maven.indices +import org.apache.http.annotation.Obsolete import org.jetbrains.idea.maven.server.AddArtifactResponse import org.jetbrains.idea.maven.utils.MavenProcessCanceledException import org.jetbrains.idea.maven.utils.MavenProgressIndicator import java.io.File interface MavenUpdatableIndex : MavenRepositoryIndex { - @Deprecated("do not use it") + /*** + * still required in maven lucene indexer, until dropping MavenProgressIndicator + */ + @Obsolete @Throws(MavenProcessCanceledException::class) fun updateOrRepair(fullUpdate: Boolean, progress: MavenProgressIndicator, explicit: Boolean) { } diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/navigator/actions/IndexUpdateAction.kt b/plugins/maven/src/main/java/org/jetbrains/idea/maven/navigator/actions/IndexUpdateAction.kt index 2139070562ef..cffab47ced69 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/navigator/actions/IndexUpdateAction.kt +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/navigator/actions/IndexUpdateAction.kt @@ -3,14 +3,16 @@ package org.jetbrains.idea.maven.navigator.actions import com.intellij.openapi.actionSystem.ActionUpdateThread import com.intellij.openapi.actionSystem.AnActionEvent -import com.intellij.openapi.actionSystem.CommonDataKeys +import com.intellij.openapi.progress.ProgressManager +import com.intellij.openapi.progress.blockingContext +import com.intellij.openapi.progress.blockingContextToIndicator import com.intellij.openapi.project.DumbAwareAction +import com.intellij.platform.ide.progress.TaskCancellation import com.intellij.platform.ide.progress.withBackgroundProgress -import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import org.jetbrains.idea.maven.indices.MavenSystemIndicesManager -import org.jetbrains.idea.maven.indices.MavenUpdatableIndex +import org.jetbrains.idea.maven.utils.MavenCoroutineScopeProvider import org.jetbrains.idea.maven.utils.MavenDataKeys import org.jetbrains.idea.maven.utils.MavenProgressIndicator @@ -19,9 +21,10 @@ class IndexUpdateAction : DumbAwareAction() { override fun actionPerformed(e: AnActionEvent) { val mavenRepo = e.getData(MavenDataKeys.MAVEN_REPOSITORY) ?: return + val project = e.project ?: return val manager = MavenSystemIndicesManager.getInstance() - manager.updateIndexContentFromEDT(mavenRepo) + manager.updateIndexContent(mavenRepo, project) } override fun getActionUpdateThread(): ActionUpdateThread { diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/server/MavenIndexerWrapper.kt b/plugins/maven/src/main/java/org/jetbrains/idea/maven/server/MavenIndexerWrapper.kt index 2649bfdeb9f5..b78c395a2c27 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/server/MavenIndexerWrapper.kt +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/server/MavenIndexerWrapper.kt @@ -107,6 +107,7 @@ abstract class MavenIndexerWrapper : MavenRemoteObjectWrapper( + indicator, RetriableCancelable { val indicatorWrapper = wrapAndExport(indicator) try { diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/server/RemoteObjectWrapper.kt b/plugins/maven/src/main/java/org/jetbrains/idea/maven/server/RemoteObjectWrapper.kt index 79faa9ce604a..a324c16e9661 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/server/RemoteObjectWrapper.kt +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/server/RemoteObjectWrapper.kt @@ -18,6 +18,7 @@ package org.jetbrains.idea.maven.server import org.jetbrains.idea.maven.execution.SyncBundle import org.jetbrains.idea.maven.utils.MavenLog import org.jetbrains.idea.maven.utils.MavenProcessCanceledException +import org.jetbrains.idea.maven.utils.MavenProgressIndicator import java.rmi.RemoteException abstract class RemoteObjectWrapper protected constructor() { @@ -67,11 +68,13 @@ abstract class RemoteObjectWrapper protected constructor() { } @Throws(MavenProcessCanceledException::class) - protected fun performCancelable(r: RetriableCancelable): R { + protected fun performCancelable(indicator: MavenProgressIndicator, r: RetriableCancelable): R { var last: RemoteException? = null for (i in 0..1) { try { - return r.execute() + if (!indicator.isCanceled) { + return r.execute() + } } catch (e: RemoteException) { handleRemoteError(e.also { last = it })