@@ -29,6 +29,8 @@ import com.intellij.ide.BrowserUtil
29
29
import com.intellij.ide.util.PropertiesComponent
30
30
import com.intellij.openapi.actionSystem.AnAction
31
31
import com.intellij.openapi.actionSystem.AnActionEvent
32
+ import com.intellij.openapi.application.ModalityState
33
+ import com.intellij.openapi.application.asContextElement
32
34
import com.intellij.openapi.components.service
33
35
import com.intellij.openapi.diagnostic.Logger
34
36
import com.intellij.openapi.rd.util.launchUnderBackgroundProgress
@@ -302,13 +304,13 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
302
304
withoutNull(client, tableOfWorkspaces.selectedObject?.workspace) { c, workspace ->
303
305
jobs[workspace.id]?.cancel()
304
306
jobs[workspace.id] =
305
- cs.launch {
307
+ cs.launch( ModalityState .current().asContextElement()) {
306
308
withContext(Dispatchers .IO ) {
307
309
try {
308
310
c.startWorkspace(workspace)
309
311
loadWorkspaces()
310
312
} catch (e: Exception ) {
311
- logger.error(" Could not start workspace ${workspace.name} , reason: $e " )
313
+ logger.error(" Could not start workspace ${workspace.name} " , e )
312
314
}
313
315
}
314
316
}
@@ -326,7 +328,7 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
326
328
withoutNull(client, tableOfWorkspaces.selectedObject?.workspace) { c, workspace ->
327
329
jobs[workspace.id]?.cancel()
328
330
jobs[workspace.id] =
329
- cs.launch {
331
+ cs.launch( ModalityState .current().asContextElement()) {
330
332
withContext(Dispatchers .IO ) {
331
333
try {
332
334
// Stop the workspace first if it is running.
@@ -374,7 +376,7 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
374
376
loadWorkspaces()
375
377
}
376
378
} catch (e: Exception ) {
377
- logger.error(" Could not update workspace ${workspace.name} , reason: $e " )
379
+ logger.error(" Could not update workspace ${workspace.name} " , e )
378
380
}
379
381
}
380
382
}
@@ -392,13 +394,13 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
392
394
withoutNull(client, tableOfWorkspaces.selectedObject?.workspace) { c, workspace ->
393
395
jobs[workspace.id]?.cancel()
394
396
jobs[workspace.id] =
395
- cs.launch {
397
+ cs.launch( ModalityState .current().asContextElement()) {
396
398
withContext(Dispatchers .IO ) {
397
399
try {
398
400
c.stopWorkspace(workspace)
399
401
loadWorkspaces()
400
402
} catch (e: Exception ) {
401
- logger.error(" Could not stop workspace ${workspace.name} , reason: $e " )
403
+ logger.error(" Could not stop workspace ${workspace.name} " , e )
402
404
}
403
405
}
404
406
}
@@ -426,13 +428,12 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
426
428
* Authorize the client and start polling for workspaces if we can.
427
429
*/
428
430
fun init () {
429
- // If we already have a client, start polling. Otherwise try to set one
431
+ // After each poll, the workspace list will be updated.
432
+ triggerWorkspacePolling()
433
+ // If we already have a client, we are done. Otherwise try to set one
430
434
// up from storage or config and automatically connect. Place the
431
435
// values in the fields, so they can be seen and edited if necessary.
432
- if (client != null && cliManager != null ) {
433
- // If there is a client then the fields are already filled.
434
- triggerWorkspacePolling(true )
435
- } else {
436
+ if (client == null || cliManager == null ) {
436
437
// Try finding a URL and matching token to use.
437
438
val lastUrl = appPropertiesService.getValue(CODER_URL_KEY )
438
439
val lastToken = appPropertiesService.getValue(SESSION_TOKEN_KEY )
@@ -565,8 +566,6 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
565
566
cliManager = null
566
567
client = null
567
568
568
- stop()
569
-
570
569
// Authenticate and load in a background process with progress.
571
570
return LifetimeDefinition ().launchUnderBackgroundProgress(
572
571
CoderGatewayBundle .message(" gateway.connector.view.coder.workspaces.cli.downloader.dialog.title" ),
@@ -609,7 +608,6 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
609
608
610
609
this .indicator.text = " Retrieving workspaces..."
611
610
loadWorkspaces()
612
- triggerWorkspacePolling(false )
613
611
} catch (e: Exception ) {
614
612
if (isCancellation(e)) {
615
613
tfUrlComment?.text =
@@ -640,30 +638,26 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
640
638
logger.error(msg, e)
641
639
642
640
if (e is APIResponseException && e.isUnauthorized && onAuthFailure != null ) {
643
- cs.launch { onAuthFailure.invoke() }
641
+ onAuthFailure.invoke()
644
642
}
645
643
}
646
644
}
647
645
}
648
646
}
649
647
650
648
/* *
651
- * Start polling for workspace changes. Throw if there is an existing
652
- * poller and it has not been stopped.
649
+ * Start polling for workspace changes if not already started.
653
650
*/
654
- private fun triggerWorkspacePolling (fetchNow : Boolean ) {
651
+ private fun triggerWorkspacePolling () {
655
652
if (poller?.isActive == true ) {
656
653
logger.info(" Refusing to start already-started poller" )
657
654
return
658
655
}
659
656
poller =
660
- cs.launch {
661
- if (fetchNow) {
662
- loadWorkspaces()
663
- }
657
+ cs.launch(ModalityState .current().asContextElement()) {
664
658
while (isActive) {
665
- delay(5000 )
666
659
loadWorkspaces()
660
+ delay(5000 )
667
661
}
668
662
}
669
663
}
@@ -738,7 +732,7 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
738
732
logger.info(" Retrieving the workspaces took: ${timeAfterRequestingWorkspaces - timeBeforeRequestingWorkspaces} millis" )
739
733
return @withContext ams
740
734
} catch (e: Exception ) {
741
- logger.error(" Could not retrieve workspaces for ${clientNow.me.username} on ${clientNow.url} . Reason: $e " )
735
+ logger.error(" Could not retrieve workspaces for ${clientNow.me.username} on ${clientNow.url} " , e )
742
736
emptySet()
743
737
}
744
738
}
0 commit comments