-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
112 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 0 additions & 66 deletions
66
...ugin/src/main/kotlin/io/gitpod/ide/jetbrains/backend/services/ControllerStatusProvider.kt
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
...lugin/src/main/kotlin/io/gitpod/ide/jetbrains/backend/services/ControllerStatusService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) 2021 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package io.gitpod.ide.jetbrains.backend.services | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties | ||
import com.intellij.openapi.diagnostic.logger | ||
import io.gitpod.ide.jetbrains.backend.utils.Retrier.retry | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.features.HttpTimeout | ||
import io.ktor.client.features.json.JacksonSerializer | ||
import io.ktor.client.features.json.JsonFeature | ||
import io.ktor.client.request.get | ||
import java.io.IOException | ||
|
||
object ControllerStatusService { | ||
private val logger = logger<ControllerStatusService>() | ||
|
||
private const val PORT = 63342 | ||
private val cwmToken = System.getenv("CWM_HOST_STATUS_OVER_HTTP_TOKEN") | ||
|
||
private val client: HttpClient by lazy { | ||
HttpClient { | ||
install(HttpTimeout) { | ||
@Suppress("MagicNumber") | ||
requestTimeoutMillis = 2000 | ||
} | ||
install(JsonFeature) { | ||
serializer = JacksonSerializer() | ||
} | ||
} | ||
} | ||
|
||
data class ControllerStatus(val connected: Boolean, val secondsSinceLastActivity: Int) | ||
|
||
/** | ||
* @throws IOException | ||
*/ | ||
suspend fun fetch(): ControllerStatus = | ||
@Suppress("MagicNumber") | ||
retry(3, logger) { | ||
@Suppress("TooGenericExceptionCaught") // Unsure what exceptions Ktor might throw | ||
val response: Response = try { | ||
client.get("http://localhost:$PORT/codeWithMe/unattendedHostStatus?token=$cwmToken") | ||
} catch (e: Exception) { | ||
throw IOException("Failed to retrieve controller status.", e) | ||
} | ||
|
||
if (response.projects.isEmpty()) { | ||
throw IOException("Failed to fetch controller status as project list is empty.") | ||
} | ||
|
||
ControllerStatus( | ||
response.projects[0].controllerConnected, | ||
response.projects[0].secondsSinceLastControllerActivity | ||
) | ||
} | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
private data class Response( | ||
val appPid: Int, | ||
val projects: List<Project> | ||
) { | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
data class Project( | ||
val controllerConnected: Boolean, | ||
val secondsSinceLastControllerActivity: Int | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters