Skip to content

Code refactoring #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2025
Merged
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 @@ -13,7 +13,7 @@ package com.redhat.devtools.gateway

import com.redhat.devtools.gateway.openshift.DevWorkspaces
import com.redhat.devtools.gateway.openshift.Pods
import com.redhat.devtools.gateway.server.RemoteServer
import com.redhat.devtools.gateway.server.RemoteIDEServer
import com.jetbrains.gateway.thinClientLink.LinkedClientManager
import com.jetbrains.gateway.thinClientLink.ThinClientHandle
import com.jetbrains.rd.util.lifetime.Lifetime
Expand Down Expand Up @@ -50,25 +50,25 @@ class DevSpacesConnection(private val devSpacesContext: DevSpacesContext) {
): ThinClientHandle {
startAndWaitDevWorkspace()

val remoteServer = RemoteServer(devSpacesContext).also { it.waitProjectsReady() }
val projectStatus = remoteServer.getProjectStatus()
val remoteIdeServer = RemoteIDEServer(devSpacesContext).also { it.waitServerReady() }
val remoteIdeServerStatus = remoteIdeServer.getStatus()

val client = LinkedClientManager
.getInstance()
.startNewClient(
Lifetime.Eternal,
URI(projectStatus.joinLink),
URI(remoteIdeServerStatus.joinLink),
"",
onConnected,
false
)

val forwarder = Pods(devSpacesContext.client).forward(remoteServer.pod, 5990, 5990)
val forwarder = Pods(devSpacesContext.client).forward(remoteIdeServer.pod, 5990, 5990)

client.run {
lifetime.onTermination { forwarder.close() }
lifetime.onTermination {
if (remoteServer.waitProjectsTerminated())
if (remoteIdeServer.waitServerTerminated())
DevWorkspaces(devSpacesContext.client)
.stop(
devSpacesContext.devWorkspace.metadata.namespace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean


class RemoteServer(private val devSpacesContext: DevSpacesContext) {
/**
* Represent an IDE server running in a CDE.
*/
class RemoteIDEServer(private val devSpacesContext: DevSpacesContext) {
var pod: V1Pod
private var container: V1Container
private var readyTimeout: Long = 60
Expand All @@ -37,7 +39,10 @@ class RemoteServer(private val devSpacesContext: DevSpacesContext) {
container = findContainer()
}

fun getProjectStatus(): ProjectStatus {
/**
* Asks the CDE for the remote IDE server status.
*/
fun getStatus(): RemoteIDEServerStatus {
Pods(devSpacesContext.client)
.exec(
pod,
Expand All @@ -56,44 +61,44 @@ class RemoteServer(private val devSpacesContext: DevSpacesContext) {
)
.trim()
.also {
return if (Strings.isNullOrEmpty(it)) ProjectStatus.empty()
else Gson().fromJson(it, ProjectStatus::class.java)
return if (Strings.isNullOrEmpty(it)) RemoteIDEServerStatus.empty()
else Gson().fromJson(it, RemoteIDEServerStatus::class.java)
}
}

@Throws(IOException::class)
fun waitProjectsReady() {
doWaitProjectsState(true, readyTimeout)
fun waitServerReady() {
doWaitServerState(true, readyTimeout)
.also {
if (!it) throw IOException(
String.format(
"Projects are not ready after %d seconds.",
"Remote IDE server is not ready after %d seconds.",
readyTimeout
)
)
}
}

@Throws(IOException::class)
fun waitProjectsTerminated(): Boolean {
return doWaitProjectsState(false, terminationTimeout)
fun waitServerTerminated(): Boolean {
return doWaitServerState(false, terminationTimeout)
}

@Throws(IOException::class)
fun doWaitProjectsState(isReadyState: Boolean, timeout: Long): Boolean {
fun doWaitServerState(isReadyState: Boolean, timeout: Long): Boolean {
val projectsInDesiredState = AtomicBoolean()
val executor = Executors.newSingleThreadScheduledExecutor()
executor.scheduleAtFixedRate(
{
try {
getProjectStatus().also {
getStatus().also {
if (isReadyState == !Arrays.isNullOrEmpty(it.projects)) {
projectsInDesiredState.set(true)
executor.shutdown()
}
}
} catch (e: Exception) {
thisLogger().debug("Failed to check project status", e)
thisLogger().debug("Failed to check remote IDE server state.", e)
}
}, 0, 5, TimeUnit.SECONDS
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package com.redhat.devtools.gateway.server

data class ProjectStatus(
data class RemoteIDEServerStatus(
val joinLink: String,
val httpLink: String,
val gatewayLink: String,
Expand All @@ -21,15 +21,15 @@ data class ProjectStatus(
val projects: Array<ProjectInfo>
) {
companion object {
fun empty(): ProjectStatus {
return ProjectStatus("", "", "", "", "", emptyArray())
fun empty(): RemoteIDEServerStatus {
return RemoteIDEServerStatus("", "", "", "", "", emptyArray())
}
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as ProjectStatus
other as RemoteIDEServerStatus

if (joinLink != other.joinLink) return false
if (httpLink != other.httpLink) return false
Expand Down
Loading