@@ -29,6 +29,8 @@ import com.intellij.ide.BrowserUtil
2929import com.intellij.ide.util.PropertiesComponent
3030import com.intellij.openapi.actionSystem.AnAction
3131import com.intellij.openapi.actionSystem.AnActionEvent
32+ import com.intellij.openapi.application.ModalityState
33+ import com.intellij.openapi.application.asContextElement
3234import com.intellij.openapi.components.service
3335import com.intellij.openapi.diagnostic.Logger
3436import com.intellij.openapi.rd.util.launchUnderBackgroundProgress
@@ -302,13 +304,13 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
302304 withoutNull(client, tableOfWorkspaces.selectedObject?.workspace) { c, workspace ->
303305 jobs[workspace.id]?.cancel()
304306 jobs[workspace.id] =
305- cs.launch {
307+ cs.launch( ModalityState .current().asContextElement()) {
306308 withContext(Dispatchers .IO ) {
307309 try {
308310 c.startWorkspace(workspace)
309311 loadWorkspaces()
310312 } catch (e: Exception ) {
311- logger.error(" Could not start workspace ${workspace.name} , reason: $e " )
313+ logger.error(" Could not start workspace ${workspace.name} " , e )
312314 }
313315 }
314316 }
@@ -326,7 +328,7 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
326328 withoutNull(client, tableOfWorkspaces.selectedObject?.workspace) { c, workspace ->
327329 jobs[workspace.id]?.cancel()
328330 jobs[workspace.id] =
329- cs.launch {
331+ cs.launch( ModalityState .current().asContextElement()) {
330332 withContext(Dispatchers .IO ) {
331333 try {
332334 // Stop the workspace first if it is running.
@@ -374,7 +376,7 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
374376 loadWorkspaces()
375377 }
376378 } catch (e: Exception ) {
377- logger.error(" Could not update workspace ${workspace.name} , reason: $e " )
379+ logger.error(" Could not update workspace ${workspace.name} " , e )
378380 }
379381 }
380382 }
@@ -392,13 +394,13 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
392394 withoutNull(client, tableOfWorkspaces.selectedObject?.workspace) { c, workspace ->
393395 jobs[workspace.id]?.cancel()
394396 jobs[workspace.id] =
395- cs.launch {
397+ cs.launch( ModalityState .current().asContextElement()) {
396398 withContext(Dispatchers .IO ) {
397399 try {
398400 c.stopWorkspace(workspace)
399401 loadWorkspaces()
400402 } catch (e: Exception ) {
401- logger.error(" Could not stop workspace ${workspace.name} , reason: $e " )
403+ logger.error(" Could not stop workspace ${workspace.name} " , e )
402404 }
403405 }
404406 }
@@ -426,13 +428,12 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
426428 * Authorize the client and start polling for workspaces if we can.
427429 */
428430 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
430434 // up from storage or config and automatically connect. Place the
431435 // 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 ) {
436437 // Try finding a URL and matching token to use.
437438 val lastUrl = appPropertiesService.getValue(CODER_URL_KEY )
438439 val lastToken = appPropertiesService.getValue(SESSION_TOKEN_KEY )
@@ -565,8 +566,6 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
565566 cliManager = null
566567 client = null
567568
568- stop()
569-
570569 // Authenticate and load in a background process with progress.
571570 return LifetimeDefinition ().launchUnderBackgroundProgress(
572571 CoderGatewayBundle .message(" gateway.connector.view.coder.workspaces.cli.downloader.dialog.title" ),
@@ -609,7 +608,6 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
609608
610609 this .indicator.text = " Retrieving workspaces..."
611610 loadWorkspaces()
612- triggerWorkspacePolling(false )
613611 } catch (e: Exception ) {
614612 if (isCancellation(e)) {
615613 tfUrlComment?.text =
@@ -640,30 +638,26 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
640638 logger.error(msg, e)
641639
642640 if (e is APIResponseException && e.isUnauthorized && onAuthFailure != null ) {
643- cs.launch { onAuthFailure.invoke() }
641+ onAuthFailure.invoke()
644642 }
645643 }
646644 }
647645 }
648646 }
649647
650648 /* *
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.
653650 */
654- private fun triggerWorkspacePolling (fetchNow : Boolean ) {
651+ private fun triggerWorkspacePolling () {
655652 if (poller?.isActive == true ) {
656653 logger.info(" Refusing to start already-started poller" )
657654 return
658655 }
659656 poller =
660- cs.launch {
661- if (fetchNow) {
662- loadWorkspaces()
663- }
657+ cs.launch(ModalityState .current().asContextElement()) {
664658 while (isActive) {
665- delay(5000 )
666659 loadWorkspaces()
660+ delay(5000 )
667661 }
668662 }
669663 }
@@ -738,7 +732,7 @@ class CoderWorkspacesStepView : CoderWizardStep<CoderWorkspacesStepSelection>(
738732 logger.info(" Retrieving the workspaces took: ${timeAfterRequestingWorkspaces - timeBeforeRequestingWorkspaces} millis" )
739733 return @withContext ams
740734 } 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 )
742736 emptySet()
743737 }
744738 }
0 commit comments