Skip to content

Commit

Permalink
Merge pull request #472 from snyk/fix/HEAD-1269_speed-up-project-close
Browse files Browse the repository at this point in the history
fix: move everything to background thread when closing project [HEAD-1269]
  • Loading branch information
bastiandoetsch authored Feb 7, 2024
2 parents 05efdea + 613bc84 commit ebd8baa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Snyk Changelog

## [2.7.4]
### Fixed
- move all clean-up tasks on project close to a background task and limit execution to 5s

## [2.7.3]
### Fixed
- only send analytics when connected to an MT US environment
Expand Down
36 changes: 18 additions & 18 deletions src/main/kotlin/io/snyk/plugin/SnykProjectManagerListener.kt
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
package io.snyk.plugin

import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManagerListener
import com.intellij.serviceContainer.AlreadyDisposedException
import io.snyk.plugin.services.SnykTaskQueueService
import io.snyk.plugin.services.SnykTaskQueueService.Companion.ls
import io.snyk.plugin.snykcode.core.AnalysisData
import io.snyk.plugin.snykcode.core.RunUtils
import io.snyk.plugin.snykcode.core.SnykCodeIgnoreInfoHolder
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

class SnykProjectManagerListener : ProjectManagerListener {
private const val TIMEOUT = 5L

class SnykProjectManagerListener : ProjectManagerListener {
override fun projectClosing(project: Project) {
RunUtils.instance.runInBackground(
project,
"Project closing: " + project.name
) {
// lets all running ProgressIndicators release MUTEX first
RunUtils.instance.cancelRunningIndicators(project)
AnalysisData.instance.removeProjectFromCaches(project)
SnykCodeIgnoreInfoHolder.instance.removeProject(project)
}

// doesn't need to run in the background, as the called update is already running in a background thread
if (SnykTaskQueueService.ls.isInitialized) {
// limit clean up to 5s
try {
SnykTaskQueueService.ls.updateWorkspaceFolders(
project,
emptySet(),
SnykTaskQueueService.ls.getWorkspaceFolders(project)
)
} catch (ignore: AlreadyDisposedException) {
// ignore
Executors.newSingleThreadExecutor().submit {
// lets all running ProgressIndicators release MUTEX first
RunUtils.instance.cancelRunningIndicators(project)
AnalysisData.instance.removeProjectFromCaches(project)
SnykCodeIgnoreInfoHolder.instance.removeProject(project)
if (ls.isInitialized) {
ls.updateWorkspaceFolders(emptySet(), ls.getWorkspaceFolders(project))
}
}.get(TIMEOUT, TimeUnit.SECONDS)
} catch (ignored: RuntimeException) {
logger<SnykProjectManagerListener>().info("Project closing clean up took too long", ignored)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class SnykTaskQueueService(val project: Project) {
ls.initialize()
}
}
ls.updateWorkspaceFolders(project, ls.getWorkspaceFolders(project), emptySet())
ls.updateWorkspaceFolders(ls.getWorkspaceFolders(project), emptySet())
}

fun scan() {
Expand Down
22 changes: 8 additions & 14 deletions src/main/kotlin/snyk/common/lsp/LanguageServerWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.roots.ProjectRootManager
import io.snyk.plugin.getCliFile
import io.snyk.plugin.pluginSettings
import io.snyk.plugin.snykcode.core.RunUtils
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -117,19 +116,14 @@ class LanguageServerWrapper(private val lsPath: String = getCliFile().absolutePa
languageServer.initialize(params).get(INITIALIZATION_TIMEOUT, TimeUnit.SECONDS)
}

fun updateWorkspaceFolders(project: Project, added: Set<WorkspaceFolder>, removed: Set<WorkspaceFolder>) {
RunUtils.instance.runInBackground(
project,
"Updating workspace folders for project: ${project.name}"
) {
try {
ensureLanguageServerInitialized()
val params = DidChangeWorkspaceFoldersParams()
params.event = WorkspaceFoldersChangeEvent(added.toList(), removed.toList())
languageServer.workspaceService.didChangeWorkspaceFolders(params)
} catch (e: Exception) {
logger.error(e)
}
fun updateWorkspaceFolders(added: Set<WorkspaceFolder>, removed: Set<WorkspaceFolder>) {
try {
ensureLanguageServerInitialized()
val params = DidChangeWorkspaceFoldersParams()
params.event = WorkspaceFoldersChangeEvent(added.toList(), removed.toList())
languageServer.workspaceService.didChangeWorkspaceFolders(params)
} catch (e: Exception) {
logger.error(e)
}
}

Expand Down

0 comments on commit ebd8baa

Please sign in to comment.