Skip to content

Check the agent's status #232

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 10 commits into from
Apr 26, 2023
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 @@ -2,6 +2,7 @@ package com.coder.gateway.models

import com.coder.gateway.sdk.Arch
import com.coder.gateway.sdk.OS
import com.coder.gateway.sdk.v2.models.WorkspaceStatus
import com.coder.gateway.sdk.v2.models.WorkspaceTransition
import java.util.UUID
import javax.swing.Icon
Expand All @@ -15,11 +16,12 @@ data class WorkspaceAgentModel(
val templateIconPath: String,
var templateIcon: Icon?,
val status: WorkspaceVersionStatus,
val agentStatus: WorkspaceAgentStatus,
val workspaceStatus: WorkspaceStatus,
val agentStatus: WorkspaceAndAgentStatus,
val lastBuildTransition: WorkspaceTransition,
val agentOS: OS?,
val agentArch: Arch?,
val homeDirectory: String?
val homeDirectory: String?,
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down
41 changes: 0 additions & 41 deletions src/main/kotlin/com/coder/gateway/models/WorkspaceAgentStatus.kt

This file was deleted.

106 changes: 106 additions & 0 deletions src/main/kotlin/com/coder/gateway/models/WorkspaceAndAgentStatus.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.coder.gateway.models

import com.coder.gateway.sdk.v2.models.Workspace
import com.coder.gateway.sdk.v2.models.WorkspaceAgent
import com.coder.gateway.sdk.v2.models.WorkspaceAgentLifecycleState
import com.coder.gateway.sdk.v2.models.WorkspaceAgentStatus
import com.coder.gateway.sdk.v2.models.WorkspaceStatus
import com.intellij.ui.JBColor

/**
* WorkspaceAndAgentStatus represents the combined status of a single agent and
* its workspace (or just the workspace if there are no agents).
*/
enum class WorkspaceAndAgentStatus(val label: String, val description: String) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something feels a bit off to me about this, like maybe we should just have the workspace status and agent status and check both whenever we need to rather than have this computed state but my thoughts about it are a bit fuzzy and we already had it architectured like this so I left it for now.

// Workspace states.
QUEUED("◍ Queued", "The workspace is queueing to start."),
STARTING("⦿ Starting", "The workspace is starting."),
FAILED("ⓧ Failed", "The workspace has failed to start."),
DELETING("⦸ Deleting", "The workspace is being deleted."),
DELETED("⦸ Deleted", "The workspace has been deleted."),
STOPPING("◍ Stopping", "The workspace is stopping."),
STOPPED("◍ Stopped", "The workspace has stopped."),
CANCELING("◍ Canceling action", "The workspace is being canceled."),
CANCELED("◍ Canceled action", "The workspace has been canceled."),
RUNNING("⦿ Running", "The workspace is running, waiting for agents."),

// Agent states.
CONNECTING("⦿ Connecting", "The agent is connecting."),
DISCONNECTED("⦸ Disconnected", "The agent has disconnected."),
TIMEOUT("ⓧ Timeout", "The agent is taking longer than expected to connect."),
AGENT_STARTING("⦿ Starting", "The startup script is running."),
AGENT_STARTING_READY("⦿ Starting", "The startup script is still running but the agent is ready to accept connections."),
CREATED("⦿ Created", "The agent has been created."),
START_ERROR("◍ Started with error", "The agent is ready but the startup script errored."),
START_TIMEOUT("◍ Starting", "The startup script is taking longer than expected."),
START_TIMEOUT_READY("◍ Starting", "The startup script is taking longer than expected but the agent is ready to accept connections."),
SHUTTING_DOWN("◍ Shutting down", "The agent is shutting down."),
SHUTDOWN_ERROR("⦸ Shutdown with error", "The agent shut down but the shutdown script errored."),
SHUTDOWN_TIMEOUT("⦸ Shutting down", "The shutdown script is taking longer than expected."),
OFF("⦸ Off", "The agent has shut down."),
READY("⦿ Ready", "The agent is ready to accept connections.");

fun statusColor(): JBColor = when (this) {
READY, AGENT_STARTING_READY, START_TIMEOUT_READY -> JBColor.GREEN
START_ERROR, START_TIMEOUT, SHUTDOWN_TIMEOUT -> JBColor.YELLOW
FAILED, DISCONNECTED, TIMEOUT, SHUTDOWN_ERROR -> JBColor.RED
else -> if (JBColor.isBright()) JBColor.LIGHT_GRAY else JBColor.DARK_GRAY
}

/**
* Return true if the agent is in a connectable state.
*/
fun ready(): Boolean {
return listOf(READY, START_ERROR, AGENT_STARTING_READY, START_TIMEOUT_READY)
.contains(this)
}

// We want to check that the workspace is `running`, the agent is
// `connected`, and the agent lifecycle state is `ready` to ensure the best
// possible scenario for attempting a connection.
//
// We can also choose to allow `start_error` for the agent lifecycle state;
// this means the startup script did not successfully complete but the agent
// will still accept SSH connections.
//
// Lastly we can also allow connections when the agent lifecycle state is
// `starting` or `start_timeout` if `login_before_ready` is true on the
// workspace response since this bypasses the need to wait for the script.
//
// Note that latest_build.status is derived from latest_build.job.status and
// latest_build.job.transition so there is no need to check those.
companion object {
fun from(workspace: Workspace, agent: WorkspaceAgent? = null) = when (workspace.latestBuild.status) {
WorkspaceStatus.PENDING -> QUEUED
WorkspaceStatus.STARTING -> STARTING
WorkspaceStatus.RUNNING -> when (agent?.status) {
WorkspaceAgentStatus.CONNECTED -> when (agent.lifecycleState) {
WorkspaceAgentLifecycleState.CREATED -> CREATED
WorkspaceAgentLifecycleState.STARTING -> if (agent.loginBeforeReady == true) AGENT_STARTING_READY else AGENT_STARTING
WorkspaceAgentLifecycleState.START_TIMEOUT -> if (agent.loginBeforeReady == true) START_TIMEOUT_READY else START_TIMEOUT
WorkspaceAgentLifecycleState.START_ERROR -> START_ERROR
WorkspaceAgentLifecycleState.READY -> READY
WorkspaceAgentLifecycleState.SHUTTING_DOWN -> SHUTTING_DOWN
WorkspaceAgentLifecycleState.SHUTDOWN_TIMEOUT -> SHUTDOWN_TIMEOUT
WorkspaceAgentLifecycleState.SHUTDOWN_ERROR -> SHUTDOWN_ERROR
WorkspaceAgentLifecycleState.OFF -> OFF
}

WorkspaceAgentStatus.DISCONNECTED -> DISCONNECTED
WorkspaceAgentStatus.TIMEOUT -> TIMEOUT
WorkspaceAgentStatus.CONNECTING -> CONNECTING
else -> RUNNING
}

WorkspaceStatus.STOPPING -> STOPPING
WorkspaceStatus.STOPPED -> STOPPED
WorkspaceStatus.FAILED -> FAILED
WorkspaceStatus.CANCELING -> CANCELING
WorkspaceStatus.CANCELED -> CANCELED
WorkspaceStatus.DELETING -> DELETING
WorkspaceStatus.DELETED -> DELETED
}

fun from(str: String) = WorkspaceAndAgentStatus.values().first { it.label.contains(str, true) }
}
}
29 changes: 18 additions & 11 deletions src/main/kotlin/com/coder/gateway/sdk/v2/models/WorkspaceAgent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,30 @@ data class WorkspaceAgent(
@SerializedName("latency") val derpLatency: Map<String, DERPRegion>?,
@SerializedName("connection_timeout_seconds") val connectionTimeoutSeconds: Int,
@SerializedName("troubleshooting_url") val troubleshootingURL: String,
@SerializedName("lifecycle_state") val lifecycleState: WorkspaceAgentLifecycleState,
@SerializedName("login_before_ready") val loginBeforeReady: Boolean?,
)

enum class WorkspaceAgentStatus {
@SerializedName("connecting")
CONNECTING,

@SerializedName("connected")
CONNECTED,

@SerializedName("disconnected")
DISCONNECTED,
@SerializedName("connecting") CONNECTING,
@SerializedName("connected") CONNECTED,
@SerializedName("disconnected") DISCONNECTED,
@SerializedName("timeout") TIMEOUT
}

@SerializedName("timeout")
TIMEOUT
enum class WorkspaceAgentLifecycleState {
@SerializedName("created") CREATED,
@SerializedName("starting") STARTING,
@SerializedName("start_timeout") START_TIMEOUT,
@SerializedName("start_error") START_ERROR,
@SerializedName("ready") READY,
@SerializedName("shutting_down") SHUTTING_DOWN,
@SerializedName("shutdown_timeout") SHUTDOWN_TIMEOUT,
@SerializedName("shutdown_error") SHUTDOWN_ERROR,
@SerializedName("off") OFF,
}

data class DERPRegion(
@SerializedName("preferred") val preferred: Boolean,
@SerializedName("latency_ms") val latencyMillis: Double
@SerializedName("latency_ms") val latencyMillis: Double,
)
Loading