@@ -95,7 +95,7 @@ class CoderGatewayRecentWorkspaceConnectionsView(private val setContentCallback:
95
95
* API clients and workspaces grouped by deployment and keyed by their
96
96
* config directory.
97
97
*/
98
- private var deployments: Map <String , DeploymentInfo > = emptyMap ()
98
+ private var deployments: MutableMap <String , DeploymentInfo > = mutableMapOf ()
99
99
private var poller: Job ? = null
100
100
101
101
override fun createRecentsView (lifetime : Lifetime ): JComponent {
@@ -167,8 +167,8 @@ class CoderGatewayRecentWorkspaceConnectionsView(private val setContentCallback:
167
167
// Group by the deployment.
168
168
.groupBy { it.deploymentURL }
169
169
// Group the connections in each deployment by workspace.
170
- .mapValues { (deploymentURL, connections ) ->
171
- connections
170
+ .mapValues { (deploymentURL, deploymentConnections ) ->
171
+ deploymentConnections
172
172
.groupBy { it.name.split(" ." , limit = 2 ).first() }
173
173
// Find the matching workspace in the query response.
174
174
.mapValues { (workspaceName, connections) ->
@@ -178,9 +178,10 @@ class CoderGatewayRecentWorkspaceConnectionsView(private val setContentCallback:
178
178
}
179
179
// Remove connections to workspaces that no longer exist.
180
180
.filter {
181
- if (it.value.first == null && deployments[deploymentURL]?.didFetch() == true ) {
182
- logger.info(" Removing recent connections for deleted workspace ${it.key} (found ${it.value.second.size} )" )
183
- it.value.second.forEach { conn ->
181
+ val (workspaceWithAgent, workspaceConnections) = it.value
182
+ if (workspaceWithAgent == null && deployments[deploymentURL]?.didFetch() == true ) {
183
+ logger.info(" Removing recent connections for deleted workspace ${it.key} (found ${workspaceConnections.size} )" )
184
+ workspaceConnections.forEach { conn ->
184
185
recentConnectionsService.removeConnection(conn.toRecentWorkspaceConnection())
185
186
}
186
187
false
@@ -195,10 +196,10 @@ class CoderGatewayRecentWorkspaceConnectionsView(private val setContentCallback:
195
196
val deploymentError = deployment?.error
196
197
connectionsByWorkspace.forEach { (workspaceName, value) ->
197
198
val (workspaceWithAgent, connections) = value
198
- val status = if (workspaceWithAgent != null ) {
199
- Triple (workspaceWithAgent.status.icon, workspaceWithAgent.status.statusColor(), workspaceWithAgent.status.description)
200
- } else if (deploymentError != null ) {
199
+ val status = if (deploymentError != null ) {
201
200
Triple (UIUtil .getBalloonErrorIcon(), UIUtil .getErrorForeground(), deploymentError)
201
+ } else if (workspaceWithAgent != null ) {
202
+ Triple (workspaceWithAgent.status.icon, workspaceWithAgent.status.statusColor(), workspaceWithAgent.status.description)
202
203
} else {
203
204
Triple (AnimatedIcon .Default .INSTANCE , UIUtil .getContextHelpForeground(), " Querying workspace status..." )
204
205
}
@@ -293,20 +294,6 @@ class CoderGatewayRecentWorkspaceConnectionsView(private val setContentCallback:
293
294
* Start polling for workspaces if not already started.
294
295
*/
295
296
private fun triggerWorkspacePolling () {
296
- deployments = recentConnectionsService.getAllRecentConnections()
297
- .mapNotNull { it.deploymentURL }.toSet()
298
- .associateWith { deploymentURL ->
299
- deployments[deploymentURL] ? : try {
300
- val cli = CoderCLIManager (deploymentURL.toURL())
301
- val (url, token) = settings.readConfig(cli.coderConfigPath)
302
- val client = CoderRestClientService (url?.toURL() ? : deploymentURL.toURL(), token)
303
- DeploymentInfo (client)
304
- } catch (e: Exception ) {
305
- logger.error(" Unable to create client for $deploymentURL " , e)
306
- DeploymentInfo (error = " Error connecting to $deploymentURL : ${e.message} " )
307
- }
308
- }
309
-
310
297
if (poller?.isActive == true ) {
311
298
logger.info(" Refusing to start already-started poller" )
312
299
return
@@ -331,16 +318,33 @@ class CoderGatewayRecentWorkspaceConnectionsView(private val setContentCallback:
331
318
*/
332
319
private suspend fun fetchWorkspaces () {
333
320
withContext(Dispatchers .IO ) {
334
- deployments.values
335
- .filter { it.error == null && it.client != null }
336
- .forEach { deployment ->
337
- val url = deployment.client!! .url
338
- try {
339
- deployment.items = deployment.client!!
340
- .workspaces().flatMap { it.toAgentList() }
321
+ recentConnectionsService.getAllRecentConnections()
322
+ .mapNotNull { it.deploymentURL }
323
+ .toSet()
324
+ .map { Pair (it, deployments.getOrPut(it) { DeploymentInfo () }) }
325
+ .forEach { (deploymentURL, deployment) ->
326
+ val client = deployment.client ? : try {
327
+ val cli = CoderCLIManager (deploymentURL.toURL())
328
+ val (_, token) = settings.readConfig(cli.coderConfigPath)
329
+ deployment.client = CoderRestClientService (deploymentURL.toURL(), token)
330
+ deployment.error = null
331
+ deployment.client
341
332
} catch (e: Exception ) {
342
- logger.error(" Failed to fetch workspaces from $url " , e)
343
- deployment.error = e.message ? : " Request failed without further details"
333
+ logger.error(" Unable to create client for $deploymentURL " , e)
334
+ deployment.error = " Error connecting to $deploymentURL : ${e.message} "
335
+ null
336
+ }
337
+ if (client != null ) {
338
+ try {
339
+ deployment.items = client.workspaces()
340
+ .flatMap { it.toAgentList() }
341
+ deployment.error = null
342
+ } catch (e: Exception ) {
343
+ // TODO: If this is an auth error, ask for a new token.
344
+ logger.error(" Failed to fetch workspaces from ${client.url} " , e)
345
+ deployment.items = null
346
+ deployment.error = e.message ? : " Request failed without further details"
347
+ }
344
348
}
345
349
}
346
350
}
0 commit comments