Skip to content

Commit 1d83f50

Browse files
committed
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.
1 parent 59e0bd6 commit 1d83f50

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

src/main/kotlin/com/coder/gateway/CoderRemoteEnvironment.kt

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,19 @@ class CoderRemoteEnvironment(
2525
) : AbstractRemoteProviderEnvironment(observablePropertiesFactory) {
2626
override fun getId(): String = "${workspace.name}.${agent.name}"
2727
override fun getName(): String = "${workspace.name}.${agent.name}"
28-
private val status = WorkspaceAndAgentStatus.from(workspace, agent)
28+
private var status = WorkspaceAndAgentStatus.from(workspace, agent)
2929

30-
31-
// Map each state to whether a connection can be attempted.
32-
private var state = status.toRemoteEnvironmentState()
30+
/**
31+
* Update the workspace/agent status to the listeners, if it has changed.
32+
*/
33+
fun update(workspace: Workspace, agent: WorkspaceAgent) {
34+
val newStatus = WorkspaceAndAgentStatus.from(workspace, agent)
35+
if (newStatus != status) {
36+
status = newStatus
37+
val state = status.toRemoteEnvironmentState()
38+
listenerSet.forEach { it.consume(state) }
39+
}
40+
}
3341

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

4957
/**
50-
* Immediately send the state to the listener.
51-
*
52-
* Currently we consume the entire workspace list and are not updating
53-
* individual workspaces, so the state here is static and the listener is
54-
* only used once.
58+
* Immediately send the state to the listener and store for updates.
5559
*/
5660
override fun addStateListener(consumer: EnvironmentStateConsumer): Boolean {
57-
consumer.consume(state)
61+
consumer.consume(status.toRemoteEnvironmentState())
5862
return super.addStateListener(consumer)
5963
}
64+
65+
/**
66+
* An environment is equal if it has the same ID.
67+
*/
68+
override fun equals(other: Any?): Boolean {
69+
if (other == null) return false
70+
if (this === other) return true // Note the triple ===
71+
if (other !is CoderRemoteEnvironment) return false
72+
if (getId() != other.getId()) return false
73+
return true
74+
}
75+
76+
/**
77+
* Companion to equals, for sets.
78+
*/
79+
override fun hashCode(): Int = getId().hashCode()
6080
}

src/main/kotlin/com/coder/gateway/CoderRemoteProvider.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class CoderRemoteProvider(
5151

5252
// Current polling job.
5353
private var pollJob: Job? = null
54-
private var lastEnvironments: List<CoderRemoteEnvironment>? = null
54+
private var lastEnvironments: Set<CoderRemoteEnvironment>? = null
5555

5656
// Create our services from the Toolbox ones.
5757
private val settingsService = CoderSettingsService(settingsStore)
@@ -96,23 +96,27 @@ class CoderRemoteProvider(
9696
// different information?
9797
it.name
9898
}?.map { agent ->
99-
CoderRemoteEnvironment(client, ws, agent, observablePropertiesFactory)
99+
// If we have an environment already, update that.
100+
val env = CoderRemoteEnvironment(client, ws, agent, observablePropertiesFactory)
101+
lastEnvironments?.firstOrNull { it == env }?.let {
102+
it.update(ws, agent)
103+
it
104+
} ?: env
100105
} ?: emptyList()
101106
}
102-
}
107+
}.toSet()
103108

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

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

118122
consumer.consumeEnvironments(environments)

0 commit comments

Comments
 (0)