Skip to content

Commit

Permalink
feat(jb): observe ports status and send notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Falzetti committed May 24, 2022
1 parent e148325 commit 87e97e5
Showing 1 changed file with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package io.gitpod.jetbrains.remote

import com.intellij.ide.BrowserUtil
import com.intellij.ide.plugins.PluginManagerCore
import com.intellij.notification.NotificationAction
import com.intellij.notification.NotificationGroupManager
Expand All @@ -23,6 +24,8 @@ import io.gitpod.jetbrains.remote.utils.Retrier.retry
import io.gitpod.supervisor.api.*
import io.gitpod.supervisor.api.Info.WorkspaceInfoResponse
import io.gitpod.supervisor.api.Notification.*
import io.gitpod.supervisor.api.Status.PortsStatusRequest
import io.gitpod.supervisor.api.Status.PortsStatusResponse
import io.grpc.ManagedChannel
import io.grpc.ManagedChannelBuilder
import io.grpc.stub.ClientCallStreamObserver
Expand All @@ -46,6 +49,7 @@ import java.util.concurrent.CancellationException
import java.util.concurrent.CompletableFuture
import javax.websocket.DeploymentException


@Service
class GitpodManager : Disposable {

Expand All @@ -57,6 +61,7 @@ class GitpodManager : Disposable {
val devMode = System.getenv("JB_DEV").toBoolean()
private val backendKind = System.getenv("JETBRAINS_GITPOD_BACKEND_KIND") ?: "unknown"
private val backendQualifier = System.getenv("JETBRAINS_BACKEND_QUALIFIER") ?: "unknown"
private val workspaceUrl = System.getenv("GITPOD_WORKSPACE_URL")

private val lifetime = Lifetime.Eternal.createNested()

Expand Down Expand Up @@ -199,6 +204,71 @@ class GitpodManager : Disposable {
}
}

private val portsObserveJob = GlobalScope.launch {
if (application.isHeadlessEnvironment) {
return@launch
}

val status = StatusServiceGrpc.newStub(supervisorChannel)
// while (isActive) {
try {
val f = CompletableFuture<Void>()
status.portsStatus(
PortsStatusRequest.newBuilder().build(),
object : ClientResponseObserver<PortsStatusRequest, PortsStatusResponse> {
override fun beforeStart(requestStream: ClientCallStreamObserver<PortsStatusRequest>) {
println("[Andrea]: beforeStart")
// TODO(ak): actually should be bound to cancellation of notifications job
lifetime.onTerminationOrNow {
requestStream.cancel(null, null)
}
}

override fun onNext(ps: PortsStatusResponse) {
println("[Andrea]: onNext")
for (port in ps.portsList) {
println(port)
if (port.exposed.onExposed.number == 0) {
return
}

println(port)

if (port.served && port.exposed.url.isNullOrEmpty() == false) {
val message = "Your application running on port " + port.localPort + " is available."
val notification = notificationGroup.createNotification(message, NotificationType.INFORMATION)
// TODO(andreafalzetti): add analytics event
val lambda = { BrowserUtil.browse(port.exposed.url) }
val action = NotificationAction.createSimpleExpiring("Open in browser", lambda)
notification.addAction(action)
notification.notify(null)
}
}

}

override fun onError(t: Throwable) {
println("[Andrea]: onError")
f.completeExceptionally(t)
}

override fun onCompleted() {
f.complete(null)
}
})
} catch (t: Throwable) {
if (t is CancellationException) {
throw t
}
thisLogger().error("gitpod: failed to stream ports status: ", t)
}
}
init {
lifetime.onTerminationOrNow {
portsObserveJob.cancel()
}
}

val pendingInfo = CompletableFuture<WorkspaceInfoResponse>()
private val infoJob = GlobalScope.launch {
if (application.isHeadlessEnvironment) {
Expand Down

0 comments on commit 87e97e5

Please sign in to comment.