Skip to content
Open
Show file tree
Hide file tree
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 @@ -11,7 +11,9 @@
*/
package com.redhat.devtools.gateway

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.ui.Messages
import com.jetbrains.gateway.thinClientLink.LinkedClientManager
import com.jetbrains.gateway.thinClientLink.ThinClientHandle
import com.jetbrains.rd.util.lifetime.Lifetime
Expand All @@ -32,6 +34,7 @@ import java.net.ServerSocket
import java.net.URI
import java.util.concurrent.CancellationException
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import kotlin.time.Duration.Companion.seconds

class DevSpacesConnection(private val devSpacesContext: DevSpacesContext) {
Expand Down Expand Up @@ -77,6 +80,34 @@ class DevSpacesConnection(private val devSpacesContext: DevSpacesContext) {
server.getStatus()
}.getOrElse { RemoteIDEServerStatus.empty() }

checkCancelled?.invoke()
if (!remoteIdeServerStatus.isReady) {
val result = AtomicInteger(-1)
ApplicationManager.getApplication().invokeAndWait {
result.set(
Messages.showDialog(
"The Remote IDE Server is not responding properly.\n" +
"Would you like to try restarting the Pod or cancel the connection?",
"Remote IDE Server Issue",
arrayOf("Cancel Connection", "Restart Pod and try again"),
0, // default selected index
Messages.getWarningIcon()
)
)
}

when (result.get()) {
1 -> {
// User chose "Restart Pod"
stopAndWaitDevWorkspace(checkCancelled)
return doConnect(onConnected, onDevWorkspaceStopped, onDisconnected, onProgress, checkCancelled)
}
}

// User chose "Cancel Connection"
throw CancellationException("User cancelled the operation")
}

check(remoteIdeServerStatus.isReady) { "Could not connect, remote IDE is not ready." }

val joinLink = remoteIdeServerStatus.joinLink
Expand Down Expand Up @@ -182,7 +213,12 @@ class DevSpacesConnection(private val devSpacesContext: DevSpacesContext) {

@Throws(IOException::class, ApiException::class, CancellationException::class)
private fun startAndWaitDevWorkspace(checkCancelled: (() -> Unit)? = null) {
if (!devSpacesContext.devWorkspace.started) {
// We really need a refreshed DevWorkspace here
val devWorkspace = DevWorkspaces(devSpacesContext.client).get(
devSpacesContext.devWorkspace.namespace,
devSpacesContext.devWorkspace.name)

if (!devWorkspace.started) {
checkCancelled?.invoke()
DevWorkspaces(devSpacesContext.client)
.start(
Expand All @@ -203,4 +239,33 @@ class DevSpacesConnection(private val devSpacesContext: DevSpacesContext) {
"DevWorkspace '${devSpacesContext.devWorkspace.name}' is not running after ${DevWorkspaces.RUNNING_TIMEOUT} seconds"
)
}

@Throws(IOException::class, ApiException::class, CancellationException::class)
private fun stopAndWaitDevWorkspace(checkCancelled: (() -> Unit)? = null) {
// We really need a refreshed DevWorkspace here
val devWorkspace = DevWorkspaces(devSpacesContext.client).get(
devSpacesContext.devWorkspace.namespace,
devSpacesContext.devWorkspace.name)

if (devWorkspace.started) {
checkCancelled?.invoke()
DevWorkspaces(devSpacesContext.client)
.stop(
devSpacesContext.devWorkspace.namespace,
devSpacesContext.devWorkspace.name
)
}

if (!runBlocking { DevWorkspaces(devSpacesContext.client)
.waitPhase(
devSpacesContext.devWorkspace.namespace,
devSpacesContext.devWorkspace.name,
DevWorkspaces.STOPPED,
DevWorkspaces.RUNNING_TIMEOUT,
checkCancelled
) }
) throw IOException(
"DevWorkspace '${devSpacesContext.devWorkspace.name}' has not stopped after ${DevWorkspaces.RUNNING_TIMEOUT} seconds"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class DevSpacesConnectionProvider : GatewayConnectionProvider {

if (cont.isActive) cont.resume(null)
} catch (e: Exception) {
if (indicator.isCanceled) {
if (e is CancellationException || indicator.isCanceled) {
indicator.text2 = "Error: ${e.message}"
runDelayed(2000) { if (indicator.isRunning) indicator.stop() }
} else {
Expand Down
Loading