|
2 | 2 |
|
3 | 3 | package com.coder.gateway
|
4 | 4 |
|
5 |
| -import com.coder.gateway.sdk.humanizeDuration |
6 |
| -import com.coder.gateway.sdk.isCancellation |
7 |
| -import com.coder.gateway.sdk.isWorkerTimeout |
8 |
| -import com.coder.gateway.sdk.suspendingRetryWithExponentialBackOff |
9 |
| -import com.coder.gateway.services.CoderRecentWorkspaceConnectionsService |
10 |
| -import com.intellij.openapi.application.ApplicationManager |
11 |
| -import com.intellij.openapi.components.service |
12 | 5 | import com.intellij.openapi.diagnostic.Logger
|
13 |
| -import com.intellij.openapi.rd.util.launchUnderBackgroundProgress |
14 |
| -import com.intellij.openapi.ui.Messages |
15 | 6 | import com.jetbrains.gateway.api.ConnectionRequestor
|
16 | 7 | import com.jetbrains.gateway.api.GatewayConnectionHandle
|
17 | 8 | import com.jetbrains.gateway.api.GatewayConnectionProvider
|
18 |
| -import com.jetbrains.gateway.api.GatewayUI |
19 |
| -import com.jetbrains.gateway.ssh.SshDeployFlowUtil |
20 |
| -import com.jetbrains.gateway.ssh.SshMultistagePanelContext |
21 |
| -import com.jetbrains.gateway.ssh.deploy.DeployException |
22 |
| -import com.jetbrains.rd.util.lifetime.LifetimeDefinition |
23 |
| -import kotlinx.coroutines.launch |
24 |
| -import net.schmizz.sshj.common.SSHException |
25 |
| -import net.schmizz.sshj.connection.ConnectionException |
26 |
| -import java.time.Duration |
27 |
| -import java.util.concurrent.TimeoutException |
28 | 9 |
|
29 | 10 | class CoderGatewayConnectionProvider : GatewayConnectionProvider {
|
30 |
| - private val recentConnectionsService = service<CoderRecentWorkspaceConnectionsService>() |
31 |
| - |
32 | 11 | override suspend fun connect(parameters: Map<String, String>, requestor: ConnectionRequestor): GatewayConnectionHandle? {
|
33 |
| - val clientLifetime = LifetimeDefinition() |
34 |
| - // TODO: If this fails determine if it is an auth error and if so prompt |
35 |
| - // for a new token, configure the CLI, then try again. |
36 |
| - clientLifetime.launchUnderBackgroundProgress(CoderGatewayBundle.message("gateway.connector.coder.connection.provider.title"), canBeCancelled = true, isIndeterminate = true, project = null) { |
37 |
| - try { |
38 |
| - indicator.text = CoderGatewayBundle.message("gateway.connector.coder.connecting") |
39 |
| - val context = suspendingRetryWithExponentialBackOff( |
40 |
| - action = { attempt -> |
41 |
| - logger.info("Connecting... (attempt $attempt") |
42 |
| - if (attempt > 1) { |
43 |
| - // indicator.text is the text above the progress bar. |
44 |
| - indicator.text = CoderGatewayBundle.message("gateway.connector.coder.connecting.retry", attempt) |
45 |
| - } |
46 |
| - SshMultistagePanelContext(parameters.toHostDeployInputs()) |
47 |
| - }, |
48 |
| - retryIf = { |
49 |
| - it is ConnectionException || it is TimeoutException |
50 |
| - || it is SSHException || it is DeployException |
51 |
| - }, |
52 |
| - onException = { attempt, nextMs, e -> |
53 |
| - logger.error("Failed to connect (attempt $attempt; will retry in $nextMs ms)") |
54 |
| - // indicator.text2 is the text below the progress bar. |
55 |
| - indicator.text2 = |
56 |
| - if (isWorkerTimeout(e)) "Failed to upload worker binary...it may have timed out" |
57 |
| - else e.message ?: CoderGatewayBundle.message("gateway.connector.no-details") |
58 |
| - }, |
59 |
| - onCountdown = { remainingMs -> |
60 |
| - indicator.text = CoderGatewayBundle.message("gateway.connector.coder.connecting.failed.retry", humanizeDuration(remainingMs)) |
61 |
| - }, |
62 |
| - ) |
63 |
| - launch { |
64 |
| - logger.info("Deploying and starting IDE with $context") |
65 |
| - // At this point JetBrains takes over with their own UI. |
66 |
| - @Suppress("UnstableApiUsage") SshDeployFlowUtil.fullDeployCycle( |
67 |
| - clientLifetime, context, Duration.ofMinutes(10) |
68 |
| - ) |
69 |
| - } |
70 |
| - } catch (e: Exception) { |
71 |
| - if (isCancellation(e)) { |
72 |
| - logger.info("Connection canceled due to ${e.javaClass}") |
73 |
| - } else { |
74 |
| - logger.info("Failed to connect (will not retry)", e) |
75 |
| - // The dialog will close once we return so write the error |
76 |
| - // out into a new dialog. |
77 |
| - ApplicationManager.getApplication().invokeAndWait { |
78 |
| - Messages.showMessageDialog( |
79 |
| - e.message ?: CoderGatewayBundle.message("gateway.connector.no-details"), |
80 |
| - CoderGatewayBundle.message("gateway.connector.coder.connection.failed"), |
81 |
| - Messages.getErrorIcon()) |
82 |
| - } |
83 |
| - } |
84 |
| - } |
85 |
| - } |
86 |
| - |
87 |
| - recentConnectionsService.addRecentConnection(parameters.toRecentWorkspaceConnection()) |
88 |
| - GatewayUI.getInstance().reset() |
| 12 | + logger.debug("Launched Coder connection provider", parameters) |
| 13 | + CoderRemoteConnectionHandle().connect(parameters) |
89 | 14 | return null
|
90 | 15 | }
|
91 | 16 |
|
|
0 commit comments