Skip to content

Commit

Permalink
refactor: move and rename bsp server status
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Oct 19, 2023
1 parent 22efc78 commit 07f75c4
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ object BuildServerConnection {
val tracePrinter = Trace.setupTracePrinter("BSP", bspTraceRoot)
val bspStatusOpt =
if (addLivenessMonitor)
Some(new BspStatus(languageClient, serverName, config.icons))
Some(new ConnectionBspStatus(languageClient, serverName, config.icons))
else None
val requestMonitorOpt =
bspStatusOpt.map(new RequestMonitorImpl(_))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package scala.meta.internal.metals

import java.util.concurrent.atomic.AtomicBoolean

import scala.meta.internal.metals.clients.language.MetalsLanguageClient
import scala.meta.internal.metals.clients.language.MetalsStatusParams
import scala.meta.internal.metals.clients.language.StatusType

class ConnectionBspStatus(
client: MetalsLanguageClient,
serverName: String,
icons: Icons,
) {
private val isServerResponsive = new AtomicBoolean(false)

def connected(): Unit =
if (isServerResponsive.compareAndSet(false, true))
client.metalsStatus(ConnectionBspStatus.connectedParams(serverName, icons))
def noResponse(): Unit =
if (isServerResponsive.compareAndSet(true, false)) {
scribe.debug("server liveness monitor detected no response")
client.metalsStatus(ConnectionBspStatus.noResponseParams(serverName, icons))
}
def disconnected(): Unit = client.metalsStatus(ConnectionBspStatus.disconnectedParams)

def isBuildServerResponsive: Boolean = isServerResponsive.get()
}

object ConnectionBspStatus {
def connectedParams(serverName: String, icons: Icons): MetalsStatusParams =
MetalsStatusParams(
s"$serverName ${icons.link}",
"info",
show = true,
tooltip = s"Metals is connected to the build server ($serverName).",
).withStatusType(StatusType.bsp)

val disconnectedParams: MetalsStatusParams =
MetalsStatusParams("", hide = true).withStatusType(StatusType.bsp)

def noResponseParams(serverName: String, icons: Icons): MetalsStatusParams =
MetalsStatusParams(
s"$serverName ${icons.error}",
"error",
show = true,
tooltip = s"Build sever ($serverName) is not responding.",
command = ServerCommands.ConnectBuildServer.id,
commandTooltip = "Reconnect.",
).withStatusType(StatusType.bsp)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean

import scala.concurrent.duration.Duration

import scala.meta.internal.metals.clients.language.MetalsLanguageClient
import scala.meta.internal.metals.clients.language.MetalsStatusParams
import scala.meta.internal.metals.clients.language.StatusType

import org.eclipse.lsp4j.jsonrpc.MessageConsumer
import org.eclipse.lsp4j.jsonrpc.messages.Message
import org.eclipse.lsp4j.jsonrpc.messages.NotificationMessage
Expand All @@ -23,7 +18,7 @@ trait RequestMonitor {
def lastIncoming: Option[Long]
}

class RequestMonitorImpl(bspStatus: BspStatus) extends RequestMonitor {
class RequestMonitorImpl(bspStatus: ConnectionBspStatus) extends RequestMonitor {
@volatile private var lastOutgoing_ : Option[Long] = None
@volatile private var lastIncoming_ : Option[Long] = None

Expand Down Expand Up @@ -59,7 +54,7 @@ class ServerLivenessMonitor(
ping: () => Unit,
metalsIdleInterval: Duration,
pingInterval: Duration,
bspStatus: BspStatus,
bspStatus: ConnectionBspStatus,
) {
@volatile private var lastPing: Long = 0
val scheduler: ScheduledExecutorService = Executors.newScheduledThreadPool(1)
Expand Down Expand Up @@ -125,46 +120,3 @@ object ServerLivenessMonitor {
object Running extends State

}

class BspStatus(
client: MetalsLanguageClient,
serverName: String,
icons: Icons,
) {
private val isServerResponsive = new AtomicBoolean(false)

def connected(): Unit =
if (isServerResponsive.compareAndSet(false, true))
client.metalsStatus(BspStatus.connectedParams(serverName, icons))
def noResponse(): Unit =
if (isServerResponsive.compareAndSet(true, false)) {
scribe.debug("server liveness monitor detected no response")
client.metalsStatus(BspStatus.noResponseParams(serverName, icons))
}
def disconnected(): Unit = client.metalsStatus(BspStatus.disconnectedParams)

def isBuildServerResponsive: Boolean = isServerResponsive.get()
}

object BspStatus {
def connectedParams(serverName: String, icons: Icons): MetalsStatusParams =
MetalsStatusParams(
s"$serverName ${icons.link}",
"info",
show = true,
tooltip = s"Metals is connected to the build server ($serverName).",
).withStatusType(StatusType.bsp)

val disconnectedParams: MetalsStatusParams =
MetalsStatusParams("", hide = true).withStatusType(StatusType.bsp)

def noResponseParams(serverName: String, icons: Icons): MetalsStatusParams =
MetalsStatusParams(
s"$serverName ${icons.error}",
"error",
show = true,
tooltip = s"Build sever ($serverName) is not responding.",
command = ServerCommands.ConnectBuildServer.id,
commandTooltip = "Reconnect.",
).withStatusType(StatusType.bsp)
}
4 changes: 2 additions & 2 deletions tests/unit/src/main/scala/tests/TestingClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import scala.meta.internal.builds.BspErrorHandler
import scala.meta.internal.builds.BuildTool
import scala.meta.internal.builds.BuildTools
import scala.meta.internal.decorations.PublishDecorationsParams
import scala.meta.internal.metals.BspStatus
import scala.meta.internal.metals.Buffers
import scala.meta.internal.metals.ClientCommands
import scala.meta.internal.metals.ConnectionBspStatus
import scala.meta.internal.metals.FileOutOfScalaCliBspScope
import scala.meta.internal.metals.Icons
import scala.meta.internal.metals.Messages._
Expand Down Expand Up @@ -373,7 +373,7 @@ class TestingClient(workspace: AbsolutePath, val buffers: Buffers)
) {
bspError
} else if (
params.getMessage() == BspStatus
params.getMessage() == ConnectionBspStatus
.noResponseParams("Bill", Icons.default)
.logMessage(Icons.default)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tests

import scala.concurrent.duration.Duration

import scala.meta.internal.metals.BspStatus
import scala.meta.internal.metals.ConnectionBspStatus
import scala.meta.internal.metals.Icons
import scala.meta.internal.metals.Messages
import scala.meta.internal.metals.MetalsEnrichments._
Expand Down Expand Up @@ -60,7 +60,7 @@ class ServerLivenessMonitorLspSuite extends BaseLspSuite("liveness-monitor") {
)
)
_ = Thread.sleep(sleepTime)
noResponseParams = BspStatus.noResponseParams(
noResponseParams = ConnectionBspStatus.noResponseParams(
"Bill",
Icons.default,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContextExecutorService
import scala.concurrent.duration.Duration

import scala.meta.internal.metals.BspStatus
import scala.meta.internal.metals.ConnectionBspStatus
import scala.meta.internal.metals.Icons
import scala.meta.internal.metals.RequestMonitor
import scala.meta.internal.metals.ServerLivenessMonitor
Expand All @@ -23,7 +23,7 @@ class ServerLivenessMonitorSuite extends BaseSuite {
val pingInterval = Duration("3s")
val server = new ResponsiveServer(pingInterval)
val client = new CountMessageRequestsClient
val bspStatus = new BspStatus(client, "responsive-server", Icons.default)
val bspStatus = new ConnectionBspStatus(client, "responsive-server", Icons.default)
val livenessMonitor = new ServerLivenessMonitor(
server,
() => server.sendRequest(true),
Expand Down Expand Up @@ -89,7 +89,7 @@ class CountMessageRequestsClient extends NoopLanguageClient {

override def metalsStatus(params: MetalsStatusParams): Unit =
if (
params == BspStatus.noResponseParams("responsive-server", Icons.default)
params == ConnectionBspStatus.noResponseParams("responsive-server", Icons.default)
) {
showMessageRequests += 1
}
Expand Down

0 comments on commit 07f75c4

Please sign in to comment.