|
| 1 | +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 2 | +// Licensed under the GNU Affero General Public License (AGPL). |
| 3 | +// See License-AGPL.txt in the project root for license information. |
| 4 | + |
| 5 | +package io.gitpod.jetbrains.remote.latest |
| 6 | + |
| 7 | +import com.intellij.openapi.Disposable |
| 8 | +import com.intellij.openapi.client.ClientProjectSession |
| 9 | +import com.intellij.openapi.diagnostic.thisLogger |
| 10 | +import com.intellij.remoteDev.util.onTerminationOrNow |
| 11 | +import com.intellij.util.application |
| 12 | +import com.jetbrains.rd.util.lifetime.Lifetime |
| 13 | +import com.jetbrains.rdserver.terminal.BackendTerminalManager |
| 14 | +import io.gitpod.jetbrains.remote.GitpodManager |
| 15 | +import io.gitpod.supervisor.api.Status |
| 16 | +import io.gitpod.supervisor.api.StatusServiceGrpc |
| 17 | +import io.gitpod.supervisor.api.TerminalOuterClass |
| 18 | +import io.gitpod.supervisor.api.TerminalServiceGrpc |
| 19 | +import io.grpc.stub.ClientCallStreamObserver |
| 20 | +import io.grpc.stub.ClientResponseObserver |
| 21 | +import org.jetbrains.plugins.terminal.ShellTerminalWidget |
| 22 | +import org.jetbrains.plugins.terminal.TerminalView |
| 23 | +import java.util.* |
| 24 | +import java.util.concurrent.CompletableFuture |
| 25 | +import java.util.concurrent.TimeUnit |
| 26 | + |
| 27 | +@Suppress("UnstableApiUsage") |
| 28 | +class GitpodTerminalService(session: ClientProjectSession) : Disposable { |
| 29 | + private companion object { |
| 30 | + /** Indicates if this service is already running, because we shouldn't run it more than once. */ |
| 31 | + var isRunning = false |
| 32 | + } |
| 33 | + |
| 34 | + private val lifetime = Lifetime.Eternal.createNested() |
| 35 | + private val terminalView = TerminalView.getInstance(session.project) |
| 36 | + private val backendTerminalManager = BackendTerminalManager.getInstance(session.project) |
| 37 | + private val terminalServiceFutureStub = TerminalServiceGrpc.newFutureStub(GitpodManager.supervisorChannel) |
| 38 | + private val statusServiceStub = StatusServiceGrpc.newStub(GitpodManager.supervisorChannel) |
| 39 | + |
| 40 | + override fun dispose() { |
| 41 | + lifetime.terminate() |
| 42 | + } |
| 43 | + |
| 44 | + init { |
| 45 | + run() |
| 46 | + } |
| 47 | + |
| 48 | + private fun run() { |
| 49 | + if (application.isHeadlessEnvironment || isRunning) return |
| 50 | + |
| 51 | + isRunning = true |
| 52 | + |
| 53 | + val task = application.executeOnPooledThread { |
| 54 | + val terminals = getSupervisorTerminalsList() |
| 55 | + val tasks = getSupervisorTasksList() |
| 56 | + |
| 57 | + application.invokeLater { |
| 58 | + createTerminalsAttachedToTasks(terminals, tasks) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + lifetime.onTerminationOrNow { |
| 63 | + task.cancel(true) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private fun createSharedTerminalAndExecuteCommand(title: String, command: String) { |
| 68 | + val registeredTerminals = terminalView.widgets.toMutableList() |
| 69 | + |
| 70 | + backendTerminalManager.createNewSharedTerminal(UUID.randomUUID().toString(), title) |
| 71 | + |
| 72 | + for (widget in terminalView.widgets) { |
| 73 | + if (!registeredTerminals.contains(widget)) { |
| 74 | + widget.terminalTitle.change { |
| 75 | + applicationTitle = title |
| 76 | + } |
| 77 | + (widget as ShellTerminalWidget).executeCommand(command) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private fun createTerminalsAttachedToTasks( |
| 83 | + terminals: List<TerminalOuterClass.Terminal>, |
| 84 | + tasks: List<Status.TaskStatus> |
| 85 | + ) { |
| 86 | + if (tasks.isEmpty()) return |
| 87 | + |
| 88 | + val aliasToTerminalMap: MutableMap<String, TerminalOuterClass.Terminal> = mutableMapOf() |
| 89 | + |
| 90 | + for (terminal in terminals) { |
| 91 | + val terminalAlias = terminal.alias |
| 92 | + aliasToTerminalMap[terminalAlias] = terminal |
| 93 | + } |
| 94 | + |
| 95 | + for (task in tasks) { |
| 96 | + val terminalAlias = task.terminal |
| 97 | + val terminal = aliasToTerminalMap[terminalAlias] |
| 98 | + |
| 99 | + if (terminal != null) { |
| 100 | + createAttachedSharedTerminal(terminal) |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private tailrec fun getSupervisorTasksList(): List<Status.TaskStatus> { |
| 106 | + var tasksList: List<Status.TaskStatus>? = null |
| 107 | + |
| 108 | + try { |
| 109 | + val completableFuture = CompletableFuture<List<Status.TaskStatus>>() |
| 110 | + |
| 111 | + val taskStatusRequest = Status.TasksStatusRequest.newBuilder().setObserve(true).build() |
| 112 | + |
| 113 | + val taskStatusResponseObserver = object : |
| 114 | + ClientResponseObserver<Status.TasksStatusRequest, Status.TasksStatusResponse> { |
| 115 | + override fun beforeStart(request: ClientCallStreamObserver<Status.TasksStatusRequest>) { |
| 116 | + lifetime.onTerminationOrNow { |
| 117 | + request.cancel(null, null) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + override fun onNext(response: Status.TasksStatusResponse) { |
| 122 | + for (task in response.tasksList) { |
| 123 | + if (task.state === Status.TaskState.opening) { |
| 124 | + return |
| 125 | + } |
| 126 | + } |
| 127 | + completableFuture.complete(response.tasksList) |
| 128 | + } |
| 129 | + |
| 130 | + override fun onCompleted() {} |
| 131 | + |
| 132 | + override fun onError(throwable: Throwable) { |
| 133 | + completableFuture.completeExceptionally(throwable) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + statusServiceStub.tasksStatus(taskStatusRequest, taskStatusResponseObserver) |
| 138 | + |
| 139 | + tasksList = completableFuture.get() |
| 140 | + } catch (throwable: Throwable) { |
| 141 | + if (throwable is InterruptedException) { |
| 142 | + throw throwable |
| 143 | + } |
| 144 | + |
| 145 | + thisLogger().error( |
| 146 | + "Got an error while trying to get tasks list from Supervisor. Trying again in on second.", |
| 147 | + throwable |
| 148 | + ) |
| 149 | + } |
| 150 | + |
| 151 | + return if (tasksList != null) { |
| 152 | + tasksList |
| 153 | + } else { |
| 154 | + TimeUnit.SECONDS.sleep(1) |
| 155 | + getSupervisorTasksList() |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private tailrec fun getSupervisorTerminalsList(): List<TerminalOuterClass.Terminal> { |
| 160 | + var terminalsList: List<TerminalOuterClass.Terminal>? = null |
| 161 | + |
| 162 | + try { |
| 163 | + val listTerminalsRequest = TerminalOuterClass.ListTerminalsRequest.newBuilder().build() |
| 164 | + |
| 165 | + val listTerminalsResponseFuture = terminalServiceFutureStub.list(listTerminalsRequest) |
| 166 | + |
| 167 | + lifetime.onTerminationOrNow { |
| 168 | + listTerminalsResponseFuture.cancel(true) |
| 169 | + } |
| 170 | + |
| 171 | + val listTerminalsResponse = listTerminalsResponseFuture.get() |
| 172 | + |
| 173 | + terminalsList = listTerminalsResponse.terminalsList |
| 174 | + } catch (throwable: Throwable) { |
| 175 | + if (throwable is InterruptedException) { |
| 176 | + throw throwable |
| 177 | + } |
| 178 | + |
| 179 | + thisLogger().error( |
| 180 | + "Got an error while trying to get terminals list from Supervisor. Trying again in on second.", |
| 181 | + throwable |
| 182 | + ) |
| 183 | + } |
| 184 | + |
| 185 | + return if (terminalsList != null) { |
| 186 | + terminalsList |
| 187 | + } else { |
| 188 | + TimeUnit.SECONDS.sleep(1) |
| 189 | + getSupervisorTerminalsList() |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private fun createAttachedSharedTerminal(supervisorTerminal: TerminalOuterClass.Terminal) { |
| 194 | + createSharedTerminalAndExecuteCommand( |
| 195 | + supervisorTerminal.title, |
| 196 | + "gp tasks attach ${supervisorTerminal.alias}" |
| 197 | + ) |
| 198 | + } |
| 199 | +} |
0 commit comments