Skip to content

Commit

Permalink
Merge pull request #227 from JetBrains/fix-226
Browse files Browse the repository at this point in the history
Use OTEL_RESOURCE_ATTRIBUTES env var to get the resource id
  • Loading branch information
rafaelldi authored Aug 8, 2024
2 parents 6e197ee + 469ff27 commit be1c44d
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

## [Unreleased]

### Fixed

- [#226](https://github.com/JetBrains/aspire-plugin/issues/226): Cannot restart .NET project services

## [1.4.0] - 2024-07-24

### Added
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = me.rafaelldi.aspire
pluginName = aspire-plugin
pluginRepositoryUrl = https://github.com/JetBrains/aspire-plugin
# SemVer format -> https://semver.org
pluginVersion = 1.4.0
pluginVersion = 1.4.1

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 242
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ import me.rafaelldi.aspire.AspireBundle
import me.rafaelldi.aspire.generated.ResourceState
import me.rafaelldi.aspire.generated.ResourceType
import me.rafaelldi.aspire.sessionHost.SessionManager
import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_SERVICE_INSTANCE_ID
import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_STATE
import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_TYPE
import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_UID

class RestartDebugResourceAction : AnAction() {
override fun actionPerformed(event: AnActionEvent) {
val project = event.project ?: return
val resourceUid = event.getData(ASPIRE_RESOURCE_UID)
val serviceInstanceId = event.getData(ASPIRE_RESOURCE_SERVICE_INSTANCE_ID)

if (resourceUid != null) {
if (serviceInstanceId != null) {
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE)
val resourceState = event.getData(ASPIRE_RESOURCE_STATE)

if (resourceType == ResourceType.Project && resourceState == ResourceState.Running) {
currentThreadCoroutineScope().launch {
withBackgroundProgress(project, AspireBundle.message("progress.stop.resource")) {
SessionManager.getInstance(project).restartResource(resourceUid, true)
SessionManager.getInstance(project).restartResource(serviceInstanceId, true)
}
}
}
Expand All @@ -58,9 +58,9 @@ class RestartDebugResourceAction : AnAction() {
return
}

val resourceUid = event.getData(ASPIRE_RESOURCE_UID)
val serviceInstanceId = event.getData(ASPIRE_RESOURCE_SERVICE_INSTANCE_ID)

if (resourceUid != null) {
if (serviceInstanceId != null) {
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE)
val resourceState = event.getData(ASPIRE_RESOURCE_STATE)

Expand All @@ -69,7 +69,7 @@ class RestartDebugResourceAction : AnAction() {
return
}

if (!SessionManager.getInstance(project).isResourceRunning(resourceUid)) {
if (!SessionManager.getInstance(project).isResourceRunning(serviceInstanceId)) {
event.presentation.isEnabledAndVisible = false
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ import me.rafaelldi.aspire.AspireBundle
import me.rafaelldi.aspire.generated.ResourceState
import me.rafaelldi.aspire.generated.ResourceType
import me.rafaelldi.aspire.sessionHost.SessionManager
import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_SERVICE_INSTANCE_ID
import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_STATE
import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_TYPE
import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_UID

class RestartResourceAction : AnAction() {
override fun actionPerformed(event: AnActionEvent) {
val project = event.project ?: return
val resourceUid = event.getData(ASPIRE_RESOURCE_UID)
val serviceInstanceId = event.getData(ASPIRE_RESOURCE_SERVICE_INSTANCE_ID)

if (resourceUid != null) {
if (serviceInstanceId != null) {
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE)
val resourceState = event.getData(ASPIRE_RESOURCE_STATE)

if (resourceType == ResourceType.Project && resourceState == ResourceState.Running) {
currentThreadCoroutineScope().launch {
withBackgroundProgress(project, AspireBundle.message("progress.stop.resource")) {
SessionManager.getInstance(project).restartResource(resourceUid, false)
SessionManager.getInstance(project).restartResource(serviceInstanceId, false)
}
}
}
Expand All @@ -58,9 +58,9 @@ class RestartResourceAction : AnAction() {
return
}

val resourceUid = event.getData(ASPIRE_RESOURCE_UID)
val serviceInstanceId = event.getData(ASPIRE_RESOURCE_SERVICE_INSTANCE_ID)

if (resourceUid != null) {
if (serviceInstanceId != null) {
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE)
val resourceState = event.getData(ASPIRE_RESOURCE_STATE)

Expand All @@ -69,7 +69,7 @@ class RestartResourceAction : AnAction() {
return
}

if (!SessionManager.getInstance(project).isResourceRunning(resourceUid)) {
if (!SessionManager.getInstance(project).isResourceRunning(serviceInstanceId)) {
event.presentation.isEnabledAndVisible = false
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ import me.rafaelldi.aspire.AspireBundle
import me.rafaelldi.aspire.generated.ResourceState
import me.rafaelldi.aspire.generated.ResourceType
import me.rafaelldi.aspire.sessionHost.SessionManager
import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_SERVICE_INSTANCE_ID
import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_STATE
import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_TYPE
import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_UID

class StopResourceAction : AnAction() {
override fun actionPerformed(event: AnActionEvent) {
val project = event.project ?: return
val resourceUid = event.getData(ASPIRE_RESOURCE_UID)
val serviceInstanceId = event.getData(ASPIRE_RESOURCE_SERVICE_INSTANCE_ID)

if (resourceUid != null) {
if (serviceInstanceId != null) {
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE)
val resourceState = event.getData(ASPIRE_RESOURCE_STATE)

if (resourceType == ResourceType.Project && resourceState == ResourceState.Running) {
currentThreadCoroutineScope().launch {
withBackgroundProgress(project, AspireBundle.message("progress.stop.resource")) {
SessionManager.getInstance(project).stopResource(resourceUid)
SessionManager.getInstance(project).stopResource(serviceInstanceId)
}
}
}
Expand All @@ -58,9 +58,9 @@ class StopResourceAction : AnAction() {
return
}

val resourceUid = event.getData(ASPIRE_RESOURCE_UID)
val serviceInstanceId = event.getData(ASPIRE_RESOURCE_SERVICE_INSTANCE_ID)

if (resourceUid != null) {
if (serviceInstanceId != null) {
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE)
val resourceState = event.getData(ASPIRE_RESOURCE_STATE)

Expand All @@ -69,7 +69,7 @@ class StopResourceAction : AnAction() {
return
}

if (!SessionManager.getInstance(project).isResourceRunning(resourceUid)) {
if (!SessionManager.getInstance(project).isResourceRunning(serviceInstanceId)) {
event.presentation.isEnabledAndVisible = false
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import me.rafaelldi.aspire.AspireService
import me.rafaelldi.aspire.generated.*
import me.rafaelldi.aspire.util.getServiceInstanceId
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.math.roundToInt
Expand Down Expand Up @@ -40,6 +41,9 @@ class AspireResourceService(
var environment: Array<ResourceEnvironmentVariable>
private set

var serviceInstanceId: String? = null
private set

var startTime: LocalDateTime? = null
private set
var exitCode: Int? = null
Expand Down Expand Up @@ -82,6 +86,8 @@ class AspireResourceService(
urls = model?.urls ?: emptyArray()
environment = model?.environment ?: emptyArray()

serviceInstanceId = model?.getServiceInstanceId()

fillFromProperties(model?.properties ?: emptyArray())

wrapper.model.advise(lifetime, ::update)
Expand Down Expand Up @@ -158,6 +164,8 @@ class AspireResourceService(
urls = resourceModel.urls
environment = resourceModel.environment

serviceInstanceId = resourceModel.getServiceInstanceId()

fillFromProperties(resourceModel.properties)

project.messageBus.syncPublisher(ResourceListener.TOPIC).resourceUpdated(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.intellij.ui.components.JBTabbedPane
import me.rafaelldi.aspire.AspireBundle
import me.rafaelldi.aspire.services.components.ResourceConsolePanel
import me.rafaelldi.aspire.services.components.ResourceDashboardPanel
import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_SERVICE_INSTANCE_ID
import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_STATE
import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_TYPE
import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_UID
Expand Down Expand Up @@ -51,6 +52,7 @@ class AspireResourceServiceViewDescriptor(

override fun getData(dataId: String) =
if (ASPIRE_RESOURCE_UID.`is`(dataId)) resourceService.uid
else if (ASPIRE_RESOURCE_SERVICE_INSTANCE_ID.`is`(dataId)) resourceService.serviceInstanceId
else if (ASPIRE_RESOURCE_TYPE.`is`(dataId)) resourceService.type
else if (ASPIRE_RESOURCE_STATE.`is`(dataId)) resourceService.state
else null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package me.rafaelldi.aspire.sessionHost

import com.intellij.openapi.application.EDT
import com.jetbrains.rd.util.lifetime.Lifetime
import com.jetbrains.rider.run.configurations.HotReloadEnvironmentBuilder
import com.jetbrains.rider.run.configurations.RiderHotReloadRunConfigurationExtensionBase
import com.jetbrains.rider.run.configurations.RuntimeHotReloadRunConfigurationInfo
import com.jetbrains.rider.run.configurations.launchSettings.LaunchSettingsJson
import com.jetbrains.rider.runtime.DotNetExecutable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class SessionHotReloadConfigurationExtension : RiderHotReloadRunConfigurationExtensionBase() {
suspend fun canExecute(
Expand All @@ -15,7 +18,7 @@ class SessionHotReloadConfigurationExtension : RiderHotReloadRunConfigurationExt
): Boolean {
if (profile?.hotReloadEnabled == false) return false

return isRuntimeHotReloadAvailable(lifetime, hotReloadRunInfo)
return withContext(Dispatchers.EDT) { isRuntimeHotReloadAvailable(lifetime, hotReloadRunInfo) }
}

fun execute(executable: DotNetExecutable): DotNetExecutable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.rafaelldi.aspire.generated.SessionModel
import me.rafaelldi.aspire.run.AspireHostConfig
import me.rafaelldi.aspire.util.getServiceInstanceId
import java.nio.file.Path
import java.util.concurrent.ConcurrentHashMap
import kotlin.io.path.Path
Expand Down Expand Up @@ -83,11 +84,7 @@ class SessionManager(private val project: Project, scope: CoroutineScope) {
}

private fun saveConnectionToResource(command: CreateSessionCommand) {
val resourceAttributes =
command.sessionModel.envs?.firstOrNull { it.key.equals("OTEL_RESOURCE_ATTRIBUTES", true) }?.value ?: return
val serviceInstanceId =
resourceAttributes.split(",").firstOrNull { it.startsWith("service.instance.id") } ?: return
val idValue = serviceInstanceId.removePrefix("service.instance.id=")
val idValue = command.sessionModel.getServiceInstanceId() ?: return
if (idValue.isEmpty()) return

LOG.trace("Connection between resource $idValue and session ${command.sessionId}")
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/me/rafaelldi/aspire/util/DataKeys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import me.rafaelldi.aspire.generated.ResourceType

val ASPIRE_HOST_PATH: DataKey<String> = DataKey.create("Aspire.Host.Path")
val ASPIRE_RESOURCE_UID: DataKey<String> = DataKey.create("Aspire.Resource.Uid")
val ASPIRE_RESOURCE_SERVICE_INSTANCE_ID: DataKey<String> = DataKey.create("Aspire.Resource.Service.Instance.Id")
val ASPIRE_RESOURCE_TYPE: DataKey<ResourceType> = DataKey.create("Aspire.Resource.Type")
val ASPIRE_RESOURCE_STATE: DataKey<ResourceState?> = DataKey.create("Aspire.Resource.State")
25 changes: 25 additions & 0 deletions src/main/kotlin/me/rafaelldi/aspire/util/OTelUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.rafaelldi.aspire.util

import me.rafaelldi.aspire.generated.ResourceModel
import me.rafaelldi.aspire.generated.SessionModel

private const val OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES"
private const val SERVICE_INSTANCE_ID = "service.instance.id"

fun SessionModel.getServiceInstanceId(): String? {
val resourceAttributes = envs?.firstOrNull { it.key.equals(OTEL_RESOURCE_ATTRIBUTES, true) }?.value
?: return null
val serviceInstanceId = resourceAttributes.split(",").firstOrNull { it.startsWith(SERVICE_INSTANCE_ID) }
?: return null

return serviceInstanceId.removePrefix("service.instance.id=")
}

fun ResourceModel.getServiceInstanceId(): String? {
val resourceAttributes = environment.firstOrNull { it.key.equals(OTEL_RESOURCE_ATTRIBUTES, true) }?.value
?: return null
val serviceInstanceId = resourceAttributes.split(",").firstOrNull { it.startsWith(SERVICE_INSTANCE_ID) }
?: return null

return serviceInstanceId.removePrefix("service.instance.id=")
}

0 comments on commit be1c44d

Please sign in to comment.