Skip to content

Commit

Permalink
Clean up 1
Browse files Browse the repository at this point in the history
  • Loading branch information
atduarte committed Nov 23, 2021
1 parent a92f4be commit 8ee5515
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

package io.gitpod.ide.jetbrains.backend.listeners

import com.intellij.ide.ApplicationInitializedListener
import com.intellij.openapi.application.ApplicationActivationListener
import com.intellij.openapi.components.ServiceManager
import io.gitpod.ide.jetbrains.backend.services.HeartbeatService
import com.intellij.openapi.wm.IdeFrame
import com.intellij.openapi.components.service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.intellij.openapi.Disposable
import com.intellij.openapi.components.Service
import com.intellij.openapi.diagnostic.logger
import io.gitpod.gitpodprotocol.api.ConnectionHelper
import io.gitpod.gitpodprotocol.api.GitpodServer
import io.gitpod.gitpodprotocol.api.entities.SendHeartBeatOptions
import io.gitpod.ide.jetbrains.backend.services.ControllerStatusProvider.Companion.ControllerStatus
import io.gitpod.ide.jetbrains.backend.utils.Retrier.retry
Expand All @@ -19,22 +18,18 @@ import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicReference
import kotlin.concurrent.thread
import kotlin.random.Random.Default.nextInt
import java.lang.Thread
import java.util.concurrent.CompletableFuture

@Service
class HeartbeatService() : Disposable {
private val logger = logger<HeartbeatService>()
private val fetchToken: suspend () -> String = { AuthTokenService.fetchToken() }
private val fetchInfo: suspend () -> SupervisorInfoService.Info = { SupervisorInfoService.fetch() }
private val controllerStatusProvider = ControllerStatusProvider()

@Suppress("MagicNumber")
private val intervalInSeconds = 30

private val uri = "wss://${System.getenv("GITPOD_HOST").split("//").last()}/api/v1"
private val origin = System.getenv("GITPOD_WORKSPACE_URL")
private val instanceId = System.getenv("GITPOD_INSTANCE_ID")

private val server = AtomicReference<GitpodServer>()
private val client = AtomicReference<HeartbeatClient>()
private val status = AtomicReference(
ControllerStatus(
connected = false,
Expand All @@ -44,7 +39,7 @@ class HeartbeatService() : Disposable {
private val closed = AtomicBoolean(false)

init {
logger.info("Service initiated")
logger.info("Service initiating")

@Suppress("MagicNumber")
thread(name = "gitpod-heartbeat", contextClassLoader = this.javaClass.classLoader) {
Expand Down Expand Up @@ -79,19 +74,18 @@ class HeartbeatService() : Disposable {
@Synchronized
private suspend fun sendHeartbeat(wasClosed: Boolean = false) {
retry(2, logger) {
if (server.get() == null) {
server.set(createServer())
if (client.get() == null) {
client.set(createHeartbeatClient())
}

@Suppress("TooGenericExceptionCaught") // Unsure what exceptions might be thrown
try {
val s = server.get()!!
s.sendHeartBeat(SendHeartBeatOptions(instanceId, wasClosed)).await()
client.get()!!(wasClosed).await()
logger.info("Heartbeat sent with wasClosed=$wasClosed")
} catch (e: Exception) {
// If connection fails for some reason,
// remove the reference to the existing server.
server.set(null)
client.set(null)
throw e
}
}
Expand All @@ -102,12 +96,20 @@ class HeartbeatService() : Disposable {
* @throws IOException
* @throws IllegalStateException
*/
private suspend fun createServer(): GitpodServer {
logger.info("Creating GitpodServer")
val token = fetchToken()
val server = ConnectionHelper().connect(uri, origin, token).server()
return server
private suspend fun createHeartbeatClient(): HeartbeatClient {
logger.info("Creating HeartbeatClient")
val info = fetchInfo()

val server = ConnectionHelper().connect(
"wss://${info.host.split("//").last()}/api/v1",
info.workspaceUrl,
info.authToken
).server()

return { wasClosed: Boolean -> server.sendHeartBeat(SendHeartBeatOptions(info.instanceId, wasClosed)) }
}

override fun dispose() = closed.set(true)
}

typealias HeartbeatClient = (Boolean) -> CompletableFuture<Void>
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,41 @@ package io.gitpod.ide.jetbrains.backend.services

import com.intellij.openapi.diagnostic.logger
import io.gitpod.ide.jetbrains.backend.utils.Retrier.retry
import io.gitpod.supervisor.api.Info
import io.gitpod.supervisor.api.Token.GetTokenRequest
import io.gitpod.supervisor.api.Info.WorkspaceInfoRequest
import io.gitpod.supervisor.api.TokenServiceGrpc
import io.gitpod.supervisor.api.InfoServiceGrpc
import io.grpc.ManagedChannelBuilder
import kotlinx.coroutines.guava.asDeferred

object AuthTokenService {
private val logger = logger<AuthTokenService>()
object SupervisorInfoService {
private val logger = logger<SupervisorInfoService>()
private const val SUPERVISOR_ADDRESS = "localhost:22999"

data class Info(
val host: String,
val workspaceUrl: String,
val instanceId: String,
val authToken: String
)

@Suppress("MagicNumber")
suspend fun fetchToken(): String =
suspend fun fetch(): Info =
retry(3, logger) {
val channel = ManagedChannelBuilder
.forTarget(SUPERVISOR_ADDRESS)
.usePlaintext()
.build()

val infoResponse: io.gitpod.supervisor.api.Info.WorkspaceInfoResponse = InfoServiceGrpc
.newFutureStub(channel)
.workspaceInfo(WorkspaceInfoRequest.newBuilder().build())
.asDeferred()
.await()

val request = GetTokenRequest.newBuilder()
.setHost(System.getenv("GITPOD_HOST").split("//").last())
.setHost(infoResponse.gitpodHost.split("//").last())
.addScope("function:sendHeartBeat")
.setKind("gitpod")
.build()
Expand All @@ -35,6 +51,11 @@ object AuthTokenService {
.asDeferred()
.await()

response.token
Info(
host = infoResponse.gitpodHost,
workspaceUrl = infoResponse.workspaceUrl,
instanceId = infoResponse.instanceId,
authToken = response.token
)
}
}

0 comments on commit 8ee5515

Please sign in to comment.