Skip to content

Commit 62eea90

Browse files
committed
refactor: available actions refresh logic
And improved debug messages
1 parent 7c64008 commit 62eea90

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class CoderRemoteEnvironment(
6565
override val description: MutableStateFlow<EnvironmentDescription> =
6666
MutableStateFlow(EnvironmentDescription.General(context.i18n.pnotr(workspace.templateDisplayName)))
6767
override val additionalEnvironmentInformation: MutableMap<LocalizableString, String> = mutableMapOf()
68-
override val actionsList: MutableStateFlow<List<ActionDescription>> = MutableStateFlow(getAvailableActions())
68+
override val actionsList: MutableStateFlow<List<ActionDescription>> = MutableStateFlow(emptyList())
6969

7070
private val networkMetricsMarshaller = Moshi.Builder().build().adapter(NetworkMetrics::class.java)
7171
private val proxyCommandHandle = SshCommandProcessHandle(context)
@@ -76,14 +76,17 @@ class CoderRemoteEnvironment(
7676
context.logger.info("resuming SSH connection to $id — last session was still active.")
7777
startSshConnection()
7878
}
79+
refreshAvailableActions()
7980
}
8081

8182
fun asPairOfWorkspaceAndAgent(): Pair<Workspace, WorkspaceAgent> = Pair(workspace, agent)
8283

83-
private fun getAvailableActions(): List<ActionDescription> {
84+
private fun refreshAvailableActions() {
8485
val actions = mutableListOf<ActionDescription>()
86+
context.logger.debug("Refreshing available actions for workspace $id with status: $environmentStatus")
8587
if (environmentStatus.canStop()) {
8688
actions.add(Action(context, "Open web terminal") {
89+
context.logger.debug("Launching web terminal for $id...")
8790
context.desktop.browse(client.url.withPath("/${workspace.ownerName}/$name/terminal").toString()) {
8891
context.ui.showErrorInfoPopup(it)
8992
}
@@ -95,8 +98,9 @@ class CoderRemoteEnvironment(
9598
val urlTemplate = context.settingsStore.workspaceViewUrl
9699
?: client.url.withPath("/@${workspace.ownerName}/${workspace.name}").toString()
97100
val url = urlTemplate
98-
.replace("\$workspaceOwner", "${workspace.ownerName}")
101+
.replace("\$workspaceOwner", workspace.ownerName)
99102
.replace("\$workspaceName", workspace.name)
103+
context.logger.debug("Opening the dashboard for $id...")
100104
context.desktop.browse(
101105
url
102106
) {
@@ -106,21 +110,22 @@ class CoderRemoteEnvironment(
106110
)
107111

108112
actions.add(Action(context, "View template") {
113+
context.logger.debug("Opening the template for $id...")
109114
context.desktop.browse(client.url.withPath("/templates/${workspace.templateName}").toString()) {
110115
context.ui.showErrorInfoPopup(it)
111116
}
112-
}
113-
)
117+
})
114118

115119
if (environmentStatus.canStart()) {
116120
if (workspace.outdated) {
117121
actions.add(Action(context, "Update and start") {
122+
context.logger.debug("Updating and starting $id...")
118123
val build = client.updateWorkspace(workspace)
119124
update(workspace.copy(latestBuild = build), agent)
120-
}
121-
)
125+
})
122126
} else {
123127
actions.add(Action(context, "Start") {
128+
context.logger.debug("Starting $id... ")
124129
context.cs
125130
.launch(CoroutineName("Start Workspace Action CLI Runner") + Dispatchers.IO) {
126131
cli.startWorkspace(workspace.ownerName, workspace.name)
@@ -135,14 +140,15 @@ class CoderRemoteEnvironment(
135140
if (environmentStatus.canStop()) {
136141
if (workspace.outdated) {
137142
actions.add(Action(context, "Update and restart") {
143+
context.logger.debug("Updating and re-starting $id...")
138144
val build = client.updateWorkspace(workspace)
139145
update(workspace.copy(latestBuild = build), agent)
140146
}
141147
)
142148
}
143149
actions.add(Action(context, "Stop") {
144150
tryStopSshConnection()
145-
151+
context.logger.debug("Stoping $id...")
146152
val build = client.stopWorkspace(workspace)
147153
update(workspace.copy(latestBuild = build), agent)
148154
}
@@ -169,10 +175,14 @@ class CoderRemoteEnvironment(
169175
if (confirmation != workspace.name) {
170176
return@launch
171177
}
178+
context.logger.debug("Deleting $id...")
172179
deleteWorkspace()
173180
}
174181
})
175-
return actions
182+
183+
actionsList.update {
184+
actions
185+
}
176186
}
177187

178188
private suspend fun tryStopSshConnection() {
@@ -253,19 +263,17 @@ class CoderRemoteEnvironment(
253263
agent
254264
)
255265
) {
256-
context.logger.debug("Skipping update for $id - previous and current status ")
257266
return
258267
}
259268
this.workspace = workspace
260269
this.agent = agent
261270
// workspace&agent status can be different from "environment status"
262271
// which is forced to queued state when a workspace is scheduled to start
263272
updateStatus(WorkspaceAndAgentStatus.from(workspace, agent))
273+
264274
// we have to regenerate the action list in order to force a redraw
265275
// because the actions don't have a state flow on the enabled property
266-
actionsList.update {
267-
getAvailableActions()
268-
}
276+
refreshAvailableActions()
269277
}
270278

271279
private fun updateStatus(status: WorkspaceAndAgentStatus) {

0 commit comments

Comments
 (0)