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
31 changes: 31 additions & 0 deletions src/main/kotlin/com/coder/toolbox/CoderRemoteEnvironment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import com.coder.toolbox.sdk.ex.APIResponseException
import com.coder.toolbox.sdk.v2.models.NetworkMetrics
import com.coder.toolbox.sdk.v2.models.Workspace
import com.coder.toolbox.sdk.v2.models.WorkspaceAgent
import com.coder.toolbox.sdk.v2.models.WorkspaceAgentLifecycleState
import com.coder.toolbox.sdk.v2.models.WorkspaceAgentStatus
import com.coder.toolbox.sdk.v2.models.WorkspaceStatus
import com.coder.toolbox.util.waitForFalseWithTimeout
import com.coder.toolbox.util.withPath
import com.coder.toolbox.views.Action
Expand Down Expand Up @@ -256,6 +259,9 @@ class CoderRemoteEnvironment(
context.logger.info("Disconnected from $id")
}

/**
* Update the workspace/agent status to the listeners, if it has changed.
*/
/**
* Update the workspace/agent status to the listeners, if it has changed.
*/
Expand All @@ -265,15 +271,40 @@ class CoderRemoteEnvironment(
}
this.workspace = workspace
this.agent = agent

// workspace&agent status can be different from "environment status"
// which is forced to queued state when a workspace is scheduled to start
updateStatus(WorkspaceAndAgentStatus.from(workspace, agent))
checkConnectionStatus(workspace, agent)

// we have to regenerate the action list in order to force a redraw
// because the actions don't have a state flow on the enabled property
refreshAvailableActions()
}

private fun checkConnectionStatus(
ws: Workspace,
agent: WorkspaceAgent
) {
val isWorkspaceRunning = ws.latestBuild.status == WorkspaceStatus.RUNNING
val isAgentReady = agent.lifecycleState == WorkspaceAgentLifecycleState.READY
val hasConnectionIssue = agent.status in setOf(
WorkspaceAgentStatus.DISCONNECTED,
WorkspaceAgentStatus.TIMEOUT
)

when {
isWorkspaceRunning && isAgentReady && hasConnectionIssue -> {
context.cs.launch {
context.logAndShowWarning(
title = "Connection unstable",
warning = "Unstable connection between Coder server and workspace ${ws.name} detected. Your active sessions may disconnect."
)
}
}
}
}

private fun updateStatus(status: WorkspaceAndAgentStatus) {
environmentStatus = status
state.update {
Expand Down
Loading