@@ -24,6 +24,7 @@ import com.coder.gateway.util.OS
24
24
import com.coder.gateway.util.SemVer
25
25
import com.coder.gateway.util.isCancellation
26
26
import com.coder.gateway.util.toURL
27
+ import com.coder.gateway.util.withoutNull
27
28
import com.intellij.icons.AllIcons
28
29
import com.intellij.ide.ActivityTracker
29
30
import com.intellij.ide.BrowserUtil
@@ -262,30 +263,24 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
262
263
private inner class GoToTemplateAction :
263
264
AnActionButton (CoderGatewayBundle .message(" gateway.connector.view.coder.workspaces.template.text" ), CoderGatewayBundle .message(" gateway.connector.view.coder.workspaces.template.text" ), AllIcons .Nodes .Template ) {
264
265
override fun actionPerformed (p0 : AnActionEvent ) {
265
- withoutNull(client) {
266
- if (tableOfWorkspaces.selectedObject != null ) {
267
- val workspace = (tableOfWorkspaces.selectedObject as WorkspaceAgentListModel ).workspace
268
- BrowserUtil .browse(it.url.toURI().resolve(" /templates/${workspace.templateName} " ))
269
- }
266
+ withoutNull(client, tableOfWorkspaces.selectedObject?.workspace) { c, workspace ->
267
+ BrowserUtil .browse(c.url.toURI().resolve(" /templates/${workspace.templateName} " ))
270
268
}
271
269
}
272
270
}
273
271
274
272
private inner class StartWorkspaceAction :
275
273
AnActionButton (CoderGatewayBundle .message(" gateway.connector.view.coder.workspaces.start.text" ), CoderGatewayBundle .message(" gateway.connector.view.coder.workspaces.start.text" ), CoderIcons .RUN ) {
276
274
override fun actionPerformed (p0 : AnActionEvent ) {
277
- withoutNull(client) {
278
- if (tableOfWorkspaces.selectedObject != null ) {
279
- val workspace = (tableOfWorkspaces.selectedObject as WorkspaceAgentListModel ).workspace
280
- jobs[workspace.id]?.cancel()
281
- jobs[workspace.id] = cs.launch {
282
- withContext(Dispatchers .IO ) {
283
- try {
284
- it.startWorkspace(workspace)
285
- loadWorkspaces()
286
- } catch (e: WorkspaceResponseException ) {
287
- logger.error(" Could not start workspace ${workspace.name} , reason: $e " )
288
- }
275
+ withoutNull(client, tableOfWorkspaces.selectedObject?.workspace) { c, workspace ->
276
+ jobs[workspace.id]?.cancel()
277
+ jobs[workspace.id] = cs.launch {
278
+ withContext(Dispatchers .IO ) {
279
+ try {
280
+ c.startWorkspace(workspace)
281
+ loadWorkspaces()
282
+ } catch (e: WorkspaceResponseException ) {
283
+ logger.error(" Could not start workspace ${workspace.name} , reason: $e " )
289
284
}
290
285
}
291
286
}
@@ -296,59 +291,56 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
296
291
private inner class UpdateWorkspaceTemplateAction :
297
292
AnActionButton (CoderGatewayBundle .message(" gateway.connector.view.coder.workspaces.update.text" ), CoderGatewayBundle .message(" gateway.connector.view.coder.workspaces.update.text" ), CoderIcons .UPDATE ) {
298
293
override fun actionPerformed (p0 : AnActionEvent ) {
299
- withoutNull(client) {
300
- if (tableOfWorkspaces.selectedObject != null ) {
301
- val workspace = (tableOfWorkspaces.selectedObject as WorkspaceAgentListModel ).workspace
302
- jobs[workspace.id]?.cancel()
303
- jobs[workspace.id] = cs.launch {
304
- withContext(Dispatchers .IO ) {
305
- try {
306
- // Stop the workspace first if it is running.
307
- if (workspace.latestBuild.status == WorkspaceStatus .RUNNING ) {
308
- logger.info(" Waiting for ${workspace.name} to stop before updating" )
309
- it.stopWorkspace(workspace)
310
- loadWorkspaces()
311
- var elapsed = Duration .ofSeconds(0 )
312
- val timeout = Duration .ofSeconds(5 )
313
- val maxWait = Duration .ofMinutes(10 )
314
- while (isActive) { // Wait for the workspace to fully stop.
315
- delay(timeout.toMillis())
316
- val found = tableOfWorkspaces.items.firstOrNull{ it.workspace.id == workspace.id }
317
- when (val status = found?.workspace?.latestBuild?.status) {
318
- WorkspaceStatus .PENDING , WorkspaceStatus .STOPPING , WorkspaceStatus .RUNNING -> {
319
- logger.info(" Still waiting for ${workspace.name} to stop before updating" )
320
- }
321
- WorkspaceStatus .STARTING , WorkspaceStatus .FAILED ,
322
- WorkspaceStatus .CANCELING , WorkspaceStatus .CANCELED ,
323
- WorkspaceStatus .DELETING , WorkspaceStatus .DELETED -> {
324
- logger.warn(" Canceled ${workspace.name} update due to status change to $status " )
325
- break
326
- }
327
- null -> {
328
- logger.warn(" Canceled ${workspace.name} update because it no longer exists" )
329
- break
330
- }
331
- WorkspaceStatus .STOPPED -> {
332
- logger.info(" ${workspace.name} has stopped; updating now" )
333
- it.updateWorkspace(workspace)
334
- break
335
- }
294
+ withoutNull(client, tableOfWorkspaces.selectedObject?.workspace) { c, workspace ->
295
+ jobs[workspace.id]?.cancel()
296
+ jobs[workspace.id] = cs.launch {
297
+ withContext(Dispatchers .IO ) {
298
+ try {
299
+ // Stop the workspace first if it is running.
300
+ if (workspace.latestBuild.status == WorkspaceStatus .RUNNING ) {
301
+ logger.info(" Waiting for ${workspace.name} to stop before updating" )
302
+ c.stopWorkspace(workspace)
303
+ loadWorkspaces()
304
+ var elapsed = Duration .ofSeconds(0 )
305
+ val timeout = Duration .ofSeconds(5 )
306
+ val maxWait = Duration .ofMinutes(10 )
307
+ while (isActive) { // Wait for the workspace to fully stop.
308
+ delay(timeout.toMillis())
309
+ val found = tableOfWorkspaces.items.firstOrNull{ it.workspace.id == workspace.id }
310
+ when (val status = found?.workspace?.latestBuild?.status) {
311
+ WorkspaceStatus .PENDING , WorkspaceStatus .STOPPING , WorkspaceStatus .RUNNING -> {
312
+ logger.info(" Still waiting for ${workspace.name} to stop before updating" )
313
+ }
314
+ WorkspaceStatus .STARTING , WorkspaceStatus .FAILED ,
315
+ WorkspaceStatus .CANCELING , WorkspaceStatus .CANCELED ,
316
+ WorkspaceStatus .DELETING , WorkspaceStatus .DELETED -> {
317
+ logger.warn(" Canceled ${workspace.name} update due to status change to $status " )
318
+ break
336
319
}
337
- elapsed + = timeout
338
- if (elapsed > maxWait) {
339
- logger.error(" Canceled ${workspace.name} update because it took took longer than ${maxWait.toMinutes()} minutes to stop" )
320
+ null -> {
321
+ logger.warn(" Canceled ${workspace.name} update because it no longer exists" )
340
322
break
341
323
}
324
+ WorkspaceStatus .STOPPED -> {
325
+ logger.info(" ${workspace.name} has stopped; updating now" )
326
+ c.updateWorkspace(workspace)
327
+ break
328
+ }
329
+ }
330
+ elapsed + = timeout
331
+ if (elapsed > maxWait) {
332
+ logger.error(" Canceled ${workspace.name} update because it took took longer than ${maxWait.toMinutes()} minutes to stop" )
333
+ break
342
334
}
343
- } else {
344
- it.updateWorkspace(workspace)
345
- loadWorkspaces()
346
335
}
347
- } catch (e: WorkspaceResponseException ) {
348
- logger.error(" Could not update workspace ${workspace.name} , reason: $e " )
349
- } catch (e: TemplateResponseException ) {
350
- logger.error(" Could not update workspace ${workspace.name} , reason: $e " )
336
+ } else {
337
+ c.updateWorkspace(workspace)
338
+ loadWorkspaces()
351
339
}
340
+ } catch (e: WorkspaceResponseException ) {
341
+ logger.error(" Could not update workspace ${workspace.name} , reason: $e " )
342
+ } catch (e: TemplateResponseException ) {
343
+ logger.error(" Could not update workspace ${workspace.name} , reason: $e " )
352
344
}
353
345
}
354
346
}
@@ -359,18 +351,16 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
359
351
private inner class StopWorkspaceAction :
360
352
AnActionButton (CoderGatewayBundle .message(" gateway.connector.view.coder.workspaces.stop.text" ), CoderGatewayBundle .message(" gateway.connector.view.coder.workspaces.stop.text" ), CoderIcons .STOP ) {
361
353
override fun actionPerformed (p0 : AnActionEvent ) {
362
- withoutNull(client) {
363
- if (tableOfWorkspaces.selectedObject != null ) {
364
- val workspace = (tableOfWorkspaces.selectedObject as WorkspaceAgentListModel ).workspace
365
- jobs[workspace.id]?.cancel()
366
- jobs[workspace.id] = cs.launch {
367
- withContext(Dispatchers .IO ) {
368
- try {
369
- it.stopWorkspace(workspace)
370
- loadWorkspaces()
371
- } catch (e: WorkspaceResponseException ) {
372
- logger.error(" Could not stop workspace ${workspace.name} , reason: $e " )
373
- }
354
+ withoutNull(client, tableOfWorkspaces.selectedObject) { c, workspace ->
355
+ val workspace = (tableOfWorkspaces.selectedObject as WorkspaceAgentListModel ).workspace
356
+ jobs[workspace.id]?.cancel()
357
+ jobs[workspace.id] = cs.launch {
358
+ withContext(Dispatchers .IO ) {
359
+ try {
360
+ c.stopWorkspace(workspace)
361
+ loadWorkspaces()
362
+ } catch (e: WorkspaceResponseException ) {
363
+ logger.error(" Could not stop workspace ${workspace.name} , reason: $e " )
374
364
}
375
365
}
376
366
}
0 commit comments