|
| 1 | +// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package software.aws.toolkits.jetbrains.services.caws |
| 5 | + |
| 6 | +import com.intellij.openapi.project.Project |
| 7 | +import com.intellij.openapi.startup.StartupActivity |
| 8 | +import com.intellij.openapi.ui.MessageDialogBuilder |
| 9 | +import com.jetbrains.rdserver.unattendedHost.UnattendedStatusUtil |
| 10 | +import kotlinx.coroutines.delay |
| 11 | +import kotlinx.coroutines.launch |
| 12 | +import kotlinx.coroutines.runBlocking |
| 13 | +import kotlinx.coroutines.withContext |
| 14 | +import software.amazon.awssdk.services.codecatalyst.CodeCatalystClient |
| 15 | +import software.aws.toolkits.core.utils.error |
| 16 | +import software.aws.toolkits.core.utils.getLogger |
| 17 | +import software.aws.toolkits.core.utils.info |
| 18 | +import software.aws.toolkits.jetbrains.core.awsClient |
| 19 | +import software.aws.toolkits.jetbrains.core.coroutines.getCoroutineBgContext |
| 20 | +import software.aws.toolkits.jetbrains.core.coroutines.getCoroutineUiContext |
| 21 | +import software.aws.toolkits.jetbrains.core.coroutines.projectCoroutineScope |
| 22 | +import software.aws.toolkits.jetbrains.core.credentials.sono.SonoCredentialManager |
| 23 | +import software.aws.toolkits.jetbrains.services.caws.envclient.CawsEnvironmentClient |
| 24 | +import software.aws.toolkits.jetbrains.services.caws.envclient.models.UpdateActivityRequest |
| 25 | +import software.aws.toolkits.jetbrains.utils.notifyError |
| 26 | +import software.aws.toolkits.resources.message |
| 27 | +import java.time.Instant |
| 28 | +import java.time.temporal.ChronoUnit |
| 29 | + |
| 30 | +class DevEnvStatusWatcher : StartupActivity { |
| 31 | + |
| 32 | + companion object { |
| 33 | + private val LOG = getLogger<DevEnvStatusWatcher>() |
| 34 | + } |
| 35 | + |
| 36 | + override fun runActivity(project: Project) { |
| 37 | + if (System.getenv(CawsConstants.CAWS_ENV_ID_VAR) == null) { |
| 38 | + return |
| 39 | + } |
| 40 | + val connection = SonoCredentialManager.getInstance(project).getConnectionSettings() |
| 41 | + ?: error("Failed to fetch connection settings from Dev Environment") |
| 42 | + val envId = System.getenv(CawsConstants.CAWS_ENV_ID_VAR) ?: error("envId env var null") |
| 43 | + val org = System.getenv(CawsConstants.CAWS_ENV_ORG_NAME_VAR) ?: error("space env var null") |
| 44 | + val projectName = System.getenv(CawsConstants.CAWS_ENV_PROJECT_NAME_VAR) ?: error("project env var null") |
| 45 | + val client = connection.awsClient<CodeCatalystClient>() |
| 46 | + val coroutineScope = projectCoroutineScope(project) |
| 47 | + coroutineScope.launch(getCoroutineBgContext()) { |
| 48 | + val initialEnv = client.getDevEnvironment { |
| 49 | + it.id(envId) |
| 50 | + it.spaceName(org) |
| 51 | + it.projectName(projectName) |
| 52 | + } |
| 53 | + val inactivityTimeout = initialEnv.inactivityTimeoutMinutes() |
| 54 | + if (inactivityTimeout == 0) { |
| 55 | + LOG.info { "Dev environment inactivity timeout is 0, not monitoring" } |
| 56 | + return@launch |
| 57 | + } |
| 58 | + val inactivityTimeoutInSeconds = inactivityTimeout * 60 |
| 59 | + |
| 60 | + // ensure the JetBrains inactivity tracker and the activity api are in sync |
| 61 | + val jbActivityStatusJson = UnattendedStatusUtil.getStatus() |
| 62 | + val jbActivityStatus = jbActivityStatusJson.projects?.first()?.secondsSinceLastControllerActivity ?: 0 |
| 63 | + notifyBackendOfActivity((getActivityTime(jbActivityStatus).toString())) |
| 64 | + var secondsSinceLastControllerActivity = jbActivityStatus |
| 65 | + |
| 66 | + while (true) { |
| 67 | + val response = checkHeartbeat(secondsSinceLastControllerActivity, inactivityTimeoutInSeconds, project) |
| 68 | + if (response.first) return@launch |
| 69 | + delay(30000) |
| 70 | + secondsSinceLastControllerActivity = response.second |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // This function returns a Pair The first value is a boolean indicating if the API returned the last recorded activity. |
| 76 | + // If inactivity tracking is disabled or if the value returned by the API is unparseable, the heartbeat is not sent |
| 77 | + // The second value indicates the seconds since last activity as recorded by JB in the most recent run |
| 78 | + fun checkHeartbeat( |
| 79 | + secondsSinceLastControllerActivity: Long, |
| 80 | + inactivityTimeoutInSeconds: Int, |
| 81 | + project: Project |
| 82 | + ): Pair<Boolean, Long> { |
| 83 | + val lastActivityTime = getJbRecordedActivity() |
| 84 | + |
| 85 | + if (lastActivityTime < secondsSinceLastControllerActivity) { |
| 86 | + // update the API in case of any activity |
| 87 | + notifyBackendOfActivity((getActivityTime(lastActivityTime).toString())) |
| 88 | + } |
| 89 | + |
| 90 | + val lastRecordedActivityTime = getLastRecordedApiActivity() |
| 91 | + if (lastRecordedActivityTime == null) { |
| 92 | + LOG.error { "Couldn't retrieve last recorded activity from API" } |
| 93 | + return Pair(true, lastActivityTime) |
| 94 | + } |
| 95 | + val durationRecordedSinceLastActivity = Instant.now().toEpochMilli().minus(lastRecordedActivityTime.toLong()) |
| 96 | + val secondsRecordedSinceLastActivity = durationRecordedSinceLastActivity / 1000 |
| 97 | + |
| 98 | + if (secondsRecordedSinceLastActivity >= (inactivityTimeoutInSeconds - 300)) { |
| 99 | + try { |
| 100 | + val inactivityDurationInMinutes = secondsRecordedSinceLastActivity / 60 |
| 101 | + val ans = runBlocking { |
| 102 | + val continueWorking = withContext(getCoroutineUiContext()) { |
| 103 | + return@withContext MessageDialogBuilder.okCancel( |
| 104 | + message("caws.devenv.continue.working.after.timeout.title"), |
| 105 | + message("caws.devenv.continue.working.after.timeout", inactivityDurationInMinutes) |
| 106 | + ).ask(project) |
| 107 | + } |
| 108 | + return@runBlocking continueWorking |
| 109 | + } |
| 110 | + |
| 111 | + if (ans) { |
| 112 | + notifyBackendOfActivity(getActivityTime().toString()) |
| 113 | + } |
| 114 | + } catch (e: Exception) { |
| 115 | + val preMessage = "Error while checking if Dev Environment should continue working" |
| 116 | + LOG.error(e) { preMessage } |
| 117 | + notifyError(preMessage, e.message.toString()) |
| 118 | + } |
| 119 | + } |
| 120 | + return Pair(false, lastActivityTime) |
| 121 | + } |
| 122 | + |
| 123 | + fun getLastRecordedApiActivity(): String? = CawsEnvironmentClient.getInstance().getActivity()?.timestamp |
| 124 | + |
| 125 | + fun getJbRecordedActivity(): Long { |
| 126 | + val statusJson = UnattendedStatusUtil.getStatus() |
| 127 | + val lastActivityTime = statusJson.projects?.first()?.secondsSinceLastControllerActivity ?: 0 |
| 128 | + return lastActivityTime |
| 129 | + } |
| 130 | + |
| 131 | + fun notifyBackendOfActivity(timestamp: String = Instant.now().toEpochMilli().toString()) { |
| 132 | + val request = UpdateActivityRequest( |
| 133 | + timestamp = timestamp |
| 134 | + ) |
| 135 | + CawsEnvironmentClient.getInstance().putActivityTimestamp(request) |
| 136 | + } |
| 137 | + |
| 138 | + private fun getActivityTime(secondsSinceLastActivity: Long = 0): Long = Instant.now().minus(secondsSinceLastActivity, ChronoUnit.SECONDS).toEpochMilli() |
| 139 | +} |
0 commit comments