Skip to content

Commit

Permalink
Wait the guest client to be connected before executing "gp preview" o…
Browse files Browse the repository at this point in the history
…n JetBrains IDEs
  • Loading branch information
felladrin committed May 25, 2022
1 parent b053dae commit 4ca0d14
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
}
Expand Down Expand Up @@ -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"
}
}
}
13 changes: 13 additions & 0 deletions components/ide/jetbrains/cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
package cmd

import (
"net/http"
"os"
"time"

"github.com/spf13/cobra"
)
Expand All @@ -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() {}

0 comments on commit 4ca0d14

Please sign in to comment.