From 4ca0d14ad0d83f212c29e77582f0d7c94afa9d3b Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Wed, 25 May 2022 08:07:33 +0000 Subject: [PATCH] Wait the guest client to be connected before executing "gp preview" on JetBrains IDEs --- .../jetbrains/remote/GitpodCLIService.kt | 35 ++++++++++++++++--- components/ide/jetbrains/cli/cmd/root.go | 13 +++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodCLIService.kt b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodCLIService.kt index a8fd8be4145cf6..60e021e571cc00 100644 --- a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodCLIService.kt +++ b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodCLIService.kt @@ -7,20 +7,26 @@ package io.gitpod.jetbrains.remote import com.intellij.codeWithMe.ClientId import com.intellij.ide.BrowserUtil import com.intellij.ide.CommandLineProcessor +import com.intellij.openapi.application.runInEdt import com.intellij.openapi.client.ClientSession import com.intellij.openapi.client.ClientSessionsManager import com.intellij.openapi.diagnostic.thisLogger import com.intellij.openapi.project.Project import com.intellij.openapi.util.io.FileUtilRt import com.intellij.util.application +import com.jetbrains.rdserver.unattendedHost.UnattendedHostManager import io.netty.channel.ChannelHandlerContext import io.netty.handler.codec.http.FullHttpRequest import io.netty.handler.codec.http.QueryStringDecoder +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import org.jetbrains.ide.RestService import java.nio.file.InvalidPathException import java.nio.file.Path - +@OptIn(DelicateCoroutinesApi::class) class GitpodCLIService : RestService() { override fun getServiceName() = SERVICE_NAME @@ -46,9 +52,20 @@ class GitpodCLIService : RestService() { if (url.isNullOrBlank()) { return "url is missing" } - return withClient(request, context) { project -> - BrowserUtil.browse(url, project) + GlobalScope.launch { + val controllerClientId = getControllerClientIdAsync() + runInEdt { + ClientId.withClientId(controllerClientId) { + BrowserUtil.browse(url) + } + } } + sendOk(request, context) + return null + } + if (operation == "ping") { + sendOk(request, context) + return null } return "invalid operation" } @@ -85,7 +102,17 @@ class GitpodCLIService : RestService() { } } + private tailrec suspend fun getControllerClientIdAsync(): ClientId? { + val unattendedHostManager = UnattendedHostManager.getInstance() + return if (unattendedHostManager.controllerClientId == null) { + delay(1000L) + getControllerClientIdAsync() + } else { + unattendedHostManager.controllerClientId; + } + } + companion object { const val SERVICE_NAME = "gitpod/cli" } -} \ No newline at end of file +} diff --git a/components/ide/jetbrains/cli/cmd/root.go b/components/ide/jetbrains/cli/cmd/root.go index 9f44d048853be5..af4b6ad8ccfacc 100644 --- a/components/ide/jetbrains/cli/cmd/root.go +++ b/components/ide/jetbrains/cli/cmd/root.go @@ -5,7 +5,9 @@ package cmd import ( + "net/http" "os" + "time" "github.com/spf13/cobra" ) @@ -15,10 +17,21 @@ var rootCmd = &cobra.Command{ } func Execute() { + waitUntilBackendPluginIsAccessible() err := rootCmd.Execute() if err != nil { os.Exit(1) } } +func waitUntilBackendPluginIsAccessible() { + for { + resp, httpError := http.Get("http://localhost:63342/api/gitpod/cli?op=ping") + if httpError == nil && resp.StatusCode == http.StatusOK { + break + } + time.Sleep(1000) + } +} + func init() {}