Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.intellij.openapi.application.EDT
import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.application.asContextElement
import io.kubernetes.client.openapi.ApiException
import io.kubernetes.client.util.Watch
import kotlinx.coroutines.*

Expand Down Expand Up @@ -49,30 +50,41 @@

private suspend fun watchLoop(latestResourceVersion: String? = null) {
while (scope.isActive && !stopped) {
val watcher = createWatcher(namespace, latestResourceVersion)
var matches = createFilter(namespace)

try {
for (event in watcher) {
if (!scope.isActive || stopped) break
val watcher = createWatcher(namespace, latestResourceVersion)
try {

Check notice on line 55 in src/main/kotlin/com/redhat/devtools/gateway/devworkspace/DevWorkspaceWatcher.kt

View workflow job for this annotation

GitHub Actions / Inspect code

Convert try / finally to use() call

try-finally can be replaced with 'use()'
var matches = createFilter(namespace)
for (event in watcher) {
if (!scope.isActive || stopped) break

val dw = DevWorkspace.from(event.`object`)
if (event.type == "ADDED") {
matches = createFilter(namespace)
}
withContext(Dispatchers.EDT + ModalityState.any().asContextElement()) {
if (stopped) return@withContext
when (event.type) {
"ADDED" -> if(matches(dw)) listener.onAdded(dw)
"MODIFIED" -> if(matches(dw)) listener.onUpdated(dw) else listener.onDeleted(dw)
"DELETED" -> listener.onDeleted(dw)
val dw = DevWorkspace.from(event.`object`)
if (event.type == "ADDED") {
matches = createFilter(namespace)
}
withContext(Dispatchers.EDT + ModalityState.any().asContextElement()) {
if (stopped) return@withContext
when (event.type) {
"ADDED" -> if(matches(dw)) listener.onAdded(dw)
"MODIFIED" -> if(matches(dw)) listener.onUpdated(dw) else listener.onDeleted(dw)
"DELETED" -> listener.onDeleted(dw)
}
}
}
// connection dropped or closed — reconnect
} finally {
watcher.close()
}
} catch (e: CancellationException) {
throw e
} catch (e: ApiException) {
if (e.code == 403 || e.code == 404) {
// User cannot watch this namespace/resource.
return
}

// Other Kubernetes API errors — retry.
} catch (_: Exception) {
// connection dropped or closed — reconnect
} finally {
watcher.close()
// Connection dropped or closed — reconnect.
}

@Suppress("ConvertLongToDuration")
Expand Down
Loading