generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
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
9 changed files
with
191 additions
and
181 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
72 changes: 0 additions & 72 deletions
72
src/main/kotlin/me/rafaelldi/aspire/services/AspireServiceManager.kt
This file was deleted.
Oops, something went wrong.
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
18 changes: 0 additions & 18 deletions
18
src/main/kotlin/me/rafaelldi/aspire/sessionHost/AspireSessionHostLifecycleListener.kt
This file was deleted.
Oops, something went wrong.
140 changes: 140 additions & 0 deletions
140
src/main/kotlin/me/rafaelldi/aspire/sessionHost/AspireSessionHostManager.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,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) | ||
} | ||
) | ||
} | ||
} |
Oops, something went wrong.