Skip to content

Commit 71d3405

Browse files
committed
Cancel jobs when exiting workspace step
Especially important for the update job, since it has a loop.
1 parent 2126fe1 commit 71d3405

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import java.net.SocketTimeoutException
7171
import java.net.URL
7272
import java.net.UnknownHostException
7373
import java.time.Duration
74+
import java.util.*
7475
import javax.net.ssl.SSLHandshakeException
7576
import javax.swing.Icon
7677
import javax.swing.JCheckBox
@@ -87,6 +88,7 @@ private const val SESSION_TOKEN = "session-token"
8788

8889
class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : CoderWorkspacesWizardStep, Disposable {
8990
private val cs = CoroutineScope(Dispatchers.Main)
91+
private val jobs: MutableMap<UUID, Job> = mutableMapOf()
9092
private var localWizardModel = CoderWorkspacesWizardModel()
9193
private val settings: CoderSettingsService = service<CoderSettingsService>()
9294

@@ -249,7 +251,8 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
249251
val c = localWizardModel.client
250252
if (tableOfWorkspaces.selectedObject != null && c != null) {
251253
val workspace = (tableOfWorkspaces.selectedObject as WorkspaceAgentListModel).workspace
252-
cs.launch {
254+
jobs[workspace.id]?.cancel()
255+
jobs[workspace.id] = cs.launch {
253256
withContext(Dispatchers.IO) {
254257
try {
255258
c.startWorkspace(workspace)
@@ -269,7 +272,8 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
269272
val c = localWizardModel.client
270273
if (tableOfWorkspaces.selectedObject != null && c != null) {
271274
val workspace = (tableOfWorkspaces.selectedObject as WorkspaceAgentListModel).workspace
272-
cs.launch {
275+
jobs[workspace.id]?.cancel()
276+
jobs[workspace.id] = cs.launch {
273277
withContext(Dispatchers.IO) {
274278
try {
275279
// Stop the workspace first if it is running.
@@ -278,7 +282,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
278282
c.stopWorkspace(workspace)
279283
loadWorkspaces()
280284
var elapsed = Duration.ofSeconds(0)
281-
val timeout = Duration.ofSeconds(1)
285+
val timeout = Duration.ofSeconds(5)
282286
val maxWait = Duration.ofMinutes(10)
283287
while (isActive) { // Wait for the workspace to fully stop.
284288
delay(timeout.toMillis())
@@ -330,7 +334,8 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
330334
val c = localWizardModel.client
331335
if (tableOfWorkspaces.selectedObject != null && c != null) {
332336
val workspace = (tableOfWorkspaces.selectedObject as WorkspaceAgentListModel).workspace
333-
cs.launch {
337+
jobs[workspace.id]?.cancel()
338+
jobs[workspace.id] = cs.launch {
334339
withContext(Dispatchers.IO) {
335340
try {
336341
c.stopWorkspace(workspace)
@@ -457,6 +462,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
457462
localWizardModel.cliManager = null
458463
localWizardModel.client = null
459464
poller?.cancel()
465+
jobs.forEach { it.value.cancel() }
460466
tfUrlComment?.foreground = UIUtil.getContextHelpForeground()
461467
tfUrlComment?.text = CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.connect.text.connecting", deploymentURL.host)
462468
tableOfWorkspaces.setEmptyState(CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.connect.text.connecting", deploymentURL.host))
@@ -555,6 +561,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
555561

556562
private fun triggerWorkspacePolling(fetchNow: Boolean) {
557563
poller?.cancel()
564+
jobs.forEach { it.value.cancel() }
558565

559566
poller = cs.launch {
560567
if (fetchNow) {
@@ -643,6 +650,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
643650
super.onPrevious()
644651
logger.info("Going back to the main view")
645652
poller?.cancel()
653+
jobs.forEach { it.value.cancel() }
646654
}
647655

648656
override fun onNext(wizardModel: CoderWorkspacesWizardModel): Boolean {
@@ -673,6 +681,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
673681
configDirectory = cliManager?.coderConfigPath?.toString() ?: ""
674682
}
675683
poller?.cancel()
684+
jobs.forEach { it.value.cancel() }
676685

677686
logger.info("Opening IDE selection window for ${selected.name}")
678687
return true

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy