8000 [DO NOT MERGE] Toolbox refactor by code-asher · Pull Request #478 · coder/jetbrains-coder · GitHub
[go: up one dir, main page]

Skip to content

[DO NOT MERGE] Toolbox refactor #478

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

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Fix environment status not updating
It looks like you have to use the per-environment listener.  My guess is
that they do not update an environment if the ID is the same.  I made
the environment comparable, which also lets use use a set instead.
  • Loading branch information
code-asher committed Sep 16, 2024
commit 1d83f50e19d481cc951a1dadee4588963a18d478
40 changes: 30 additions & 10 deletions src/main/kotlin/com/coder/gateway/CoderRemoteEnvironment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ class CoderRemoteEnvironment(
) : AbstractRemoteProviderEnvironment(observablePropertiesFactory) {
override fun getId(): String = "${workspace.name}.${agent.name}"
override fun getName(): String = "${workspace.name}.${agent.name}"
private val status = WorkspaceAndAgentStatus.from(workspace, agent)
private var status = WorkspaceAndAgentStatus.from(workspace, agent)


// Map each state to whether a connection can be attempted.
private var state = status.toRemoteEnvironmentState()
/**
* Update the workspace/agent status to the listeners, if it has changed.
*/
fun update(workspace: Workspace, agent: WorkspaceAgent) {
val newStatus = WorkspaceAndAgentStatus.from(workspace, agent)
if (newStatus != status) {
status = newStatus
val state = status.toRemoteEnvironmentState()
listenerSet.forEach { it.consume(state) }
}
}

/**
* The contents are provided by the SSH view provided by Toolbox, all we
Expand All @@ -47,14 +55,26 @@ class CoderRemoteEnvironment(
override fun setVisible(visibilityState: EnvironmentVisibilityState) {}

/**
* Immediately send the state to the listener.
*
* Currently we consume the entire workspace list and are not updating
* individual workspaces, so the state here is static and the listener is
* only used once.
* Immediately send the state to the listener and store for updates.
*/
override fun addStateListener(consumer: EnvironmentStateConsumer): Boolean {
consumer.consume(state)
consumer.consume(status.toRemoteEnvironmentState())
return super.addStateListener(consumer)
}

/**
* An environment is equal if it has the same ID.
*/
override fun equals(other: Any?): Boolean {
if (other == null) return false
if (this === other) return true // Note the triple ===
if (other !is CoderRemoteEnvironment) return false
if (getId() != other.getId()) return false
return true
}

/**
* Companion to equals, for sets.
*/
override fun hashCode(): Int = getId().hashCode()
}
20 changes: 12 additions & 8 deletions src/main/kotlin/com/coder/gateway/CoderRemoteProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class CoderRemoteProvider(

// Current polling job.
private var pollJob: Job? = null
private var lastEnvironments: List<CoderRemoteEnvironment>? = null
private var lastEnvironments: Set<CoderRemoteEnvironment>? = null

// Create our services from the Toolbox ones.
private val settingsService = CoderSettingsService(settingsStore)
Expand Down Expand Up @@ -96,23 +96,27 @@ class CoderRemoteProvider(
// different information?
it.name
}?.map { agent ->
CoderRemoteEnvironment(client, ws, agent, observablePropertiesFactory)
// If we have an environment already, update that.
val env = CoderRemoteEnvironment(client, ws, agent, observablePropertiesFactory)
lastEnvironments?.firstOrNull { it == env }?.let {
it.update(ws, agent)
it
} ?: env
} ?: emptyList()
}
}
}.toSet()

// In case we logged out while running the query.
if (!isActive) {
return@launch
}

// Reconfigure if a new environment is found.
val newEnvironments = environments
.filter { a -> lastEnvironments?.any { b -> a.id == b.id } != true }
.map { it.name }.toSet()
if (newEnvironments.isNotEmpty()) {
// TODO@JB: Should we use the add/remove listeners instead?
val newEnvironments = lastEnvironments?.let { environments.subtract(it) }
if (newEnvironments?.isNotEmpty() == true) {
logger.info("Found new environment(s), reconfiguring CLI: {}", newEnvironments)
cli.configSsh(newEnvironments)
cli.configSsh(newEnvironments.map { it.name }.toSet())
}

consumer.consumeEnvironments(environments)
Expand Down
0