Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelldi committed Dec 23, 2023
1 parent ddcaa13 commit f9bfe28
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.jetbrains.rider.debugger.DotNetProgramRunner
import com.jetbrains.rider.run.DotNetProcessRunProfileState
import com.jetbrains.rider.util.NetUtils
import me.rafaelldi.aspire.sessionHost.AspireSessionHostConfig
import me.rafaelldi.aspire.sessionHost.AspireSessionHostRunner
import me.rafaelldi.aspire.sessionHost.AspireSessionHostManager
import org.jetbrains.concurrency.Promise
import org.jetbrains.concurrency.asPromise

Expand Down Expand Up @@ -60,7 +60,7 @@ class AspireHostProgramRunner : DotNetProgramRunner() {

val sessionHostLifetime = environment.project.lifetime.createNested()

val sessionHostRunner = AspireSessionHostRunner.getInstance()
val sessionHostManager = AspireSessionHostManager.getInstance(environment.project)
val openTelemetryPort = NetUtils.findFreePort(77800)
val config = AspireSessionHostConfig(
debugSessionToken,
Expand All @@ -74,7 +74,7 @@ class AspireHostProgramRunner : DotNetProgramRunner() {
LOG.trace("Aspire session host config: $config")

val sessionHostPromise = sessionHostLifetime.startOnUiAsync {
sessionHostRunner.runSessionHost(environment.project, config, sessionHostLifetime)
sessionHostManager.runSessionHost(config, sessionHostLifetime)
}.asPromise()

return sessionHostPromise.then {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.intellij.execution.services.ServiceViewContributor
import com.intellij.execution.services.SimpleServiceViewDescriptor
import com.intellij.openapi.project.Project
import me.rafaelldi.aspire.AspireIcons
import me.rafaelldi.aspire.sessionHost.AspireSessionHostManager
import me.rafaelldi.aspire.settings.AspireSettings

class AspireServiceContributor : ServiceViewContributor<AspireHostServiceContributor> {
Expand All @@ -13,7 +14,7 @@ class AspireServiceContributor : ServiceViewContributor<AspireHostServiceContrib
override fun getServices(project: Project): MutableList<AspireHostServiceContributor> {
val showServices = AspireSettings.getInstance().showServices
return if (showServices)
AspireServiceManager.getInstance(project)
AspireSessionHostManager.getInstance(project)
.getSessionHosts()
.map { AspireHostServiceContributor(it) }
.toMutableList()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import com.intellij.execution.services.ServiceViewDescriptor
import com.intellij.ide.projectView.PresentationData
import com.intellij.ui.SimpleTextAttributes
import com.intellij.ui.components.JBTabbedPane
import com.intellij.ui.dsl.builder.panel
import icons.RiderIcons
import me.rafaelldi.aspire.AspireBundle
import me.rafaelldi.aspire.services.components.EnvironmentVariablePanel
import me.rafaelldi.aspire.services.components.SessionDashboardPanel
import me.rafaelldi.aspire.settings.AspireSettings
import java.awt.BorderLayout
import javax.swing.JPanel
import kotlin.io.path.Path
Expand All @@ -26,6 +28,9 @@ class SessionServiceViewDescriptor(private val sessionData: SessionServiceData)
val tabs = JBTabbedPane()
tabs.addTab(AspireBundle.getMessage("service.tab.Information"), SessionDashboardPanel(sessionData))
tabs.addTab(AspireBundle.getMessage("service.tab.EnvironmentVariables"), EnvironmentVariablePanel(sessionData))
if (AspireSettings.getInstance().collectTelemetry) {
tabs.addTab(AspireBundle.getMessage("service.tab.Metrics"), panel { })
}
return JPanel(BorderLayout()).apply {
add(tabs, BorderLayout.CENTER)
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package me.rafaelldi.aspire.sessionHost

import com.intellij.execution.services.ServiceEventListener
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.rd.util.launchOnUi
import com.intellij.openapi.rd.util.withUiContext
import com.jetbrains.rd.util.addUnique
import com.jetbrains.rd.util.lifetime.Lifetime
import com.jetbrains.rd.util.lifetime.LifetimeDefinition
import com.jetbrains.rd.util.lifetime.isNotAlive
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.consumeAsFlow
import me.rafaelldi.aspire.generated.*
import me.rafaelldi.aspire.services.AspireServiceContributor
import me.rafaelldi.aspire.services.SessionHostServiceData
import java.util.concurrent.ConcurrentHashMap

@Service(Service.Level.PROJECT)
class AspireSessionHostManager(private val project: Project) {
companion object {
fun getInstance(project: Project) = project.service<AspireSessionHostManager>()

private val LOG = logger<AspireSessionHostManager>()
}

private val sessionHosts = ConcurrentHashMap<String, SessionHostServiceData>()
private val notifier = project.messageBus.syncPublisher(ServiceEventListener.TOPIC)

fun getSessionHosts(): List<SessionHostServiceData> = sessionHosts.values.toList()

suspend fun runSessionHost(
sessionHostConfig: AspireSessionHostConfig,
sessionHostLifetime: LifetimeDefinition
) {
LOG.info("Starting Aspire session host: $sessionHostConfig")

if (sessionHostLifetime.isNotAlive) {
LOG.warn("Unable to start Aspire host because lifetime is not alive")
return
}

LOG.trace("Starting new session hosts with runner")
val sessionHostRunner = AspireSessionHostRunner.getInstance(project)
val sessionHostModel = sessionHostRunner.runSessionHost(
sessionHostConfig,
sessionHostLifetime,
::subscribe
)

val sessionHostData = SessionHostServiceData(
sessionHostConfig.id,
sessionHostConfig.hostName,
sessionHostConfig.dashboardUrl,
sessionHostModel
)
LOG.trace("Adding new session host data $sessionHostData")
sessionHosts.addUnique(sessionHostLifetime, sessionHostConfig.id, sessionHostData)

sessionHostLifetime.bracketIfAlive(
{
val event = ServiceEventListener.ServiceEvent.createResetEvent(AspireServiceContributor::class.java)
notifier.handle(event)
},
{
val event = ServiceEventListener.ServiceEvent.createResetEvent(AspireServiceContributor::class.java)
notifier.handle(event)
}
)
}

private suspend fun subscribe(
sessionHostConfig: AspireSessionHostConfig,
sessionHostModel: AspireSessionHostModel,
sessionHostLifetime: Lifetime
) {
val sessionEvents = Channel<AspireSessionEvent>(Channel.UNLIMITED)
sessionHostLifetime.launchOnUi {
sessionEvents.consumeAsFlow().collect {
when (it) {
is AspireSessionStarted -> {
LOG.trace("Aspire session started (${it.id}, ${it.pid})")
sessionHostModel.processStarted.fire(ProcessStarted(it.id, it.pid))
}

is AspireSessionTerminated -> {
LOG.trace("Aspire session terminated (${it.id}, ${it.exitCode})")
sessionHostModel.processTerminated.fire(ProcessTerminated(it.id, it.exitCode))
}

is AspireSessionLogReceived -> {
LOG.trace("Aspire session log received (${it.id}, ${it.isStdErr}, ${it.message})")
sessionHostModel.logReceived.fire(LogReceived(it.id, it.isStdErr, it.message))
}
}
}
}

withUiContext {
sessionHostModel.sessions.view(sessionHostLifetime) { sessionLifetime, sessionId, sessionModel ->
LOG.info("New session added $sessionId, $sessionModel")
sessionAdded(sessionId, sessionModel, sessionLifetime, sessionEvents, sessionHostConfig)
}
}
}

private fun sessionAdded(
sessionId: String,
sessionModel: SessionModel,
sessionLifetime: Lifetime,
sessionEvents: Channel<AspireSessionEvent>,
sessionHostConfig: AspireSessionHostConfig
) {
val runner = AspireSessionRunner.getInstance(project)
runner.runSession(
AspireSessionRunner.RunSessionCommand(
sessionId,
sessionModel,
sessionLifetime,
sessionEvents,
sessionHostConfig.hostName,
sessionHostConfig.isDebug,
sessionHostConfig.openTelemetryPort
)
)

sessionLifetime.bracketIfAlive(
{
val event = ServiceEventListener.ServiceEvent.createResetEvent(AspireServiceContributor::class.java)
notifier.handle(event)
},
{
val event = ServiceEventListener.ServiceEvent.createResetEvent(AspireServiceContributor::class.java)
notifier.handle(event)
}
)
}
}
Loading

0 comments on commit f9bfe28

Please sign in to comment.