Skip to content

Commit

Permalink
Refactored handling of toolserver
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Oct 1, 2022
1 parent 4cc5d3c commit 00fb198
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 39 deletions.
23 changes: 8 additions & 15 deletions IHP/IDE/ToolServer.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module IHP.IDE.ToolServer where
module IHP.IDE.ToolServer (withToolServer) where

import IHP.Prelude
import qualified Network.Wai as Wai
Expand Down Expand Up @@ -46,18 +46,14 @@ import qualified IHP.Version as Version
import qualified IHP.IDE.Types
import qualified IHP.PGListener as PGListener

startToolServer :: (?context :: Context) => IO ()
startToolServer = do
let port = ?context
|> get #portConfig
|> get #toolServerPort
|> fromIntegral
withToolServer :: (?context :: Context) => IO () -> IO ()
withToolServer inner = withAsyncBound async (\_ -> inner)
where
async = do
let port = ?context.portConfig.toolServerPort |> fromIntegral
let isDebugMode = ?context.isDebugMode

let isDebugMode = ?context |> get #isDebugMode

thread <- async (startToolServer' port isDebugMode)

dispatch (UpdateToolServerState (ToolServerStarted { thread }))
startToolServer' port isDebugMode

startToolServer' :: (?context :: Context) => Int -> Bool -> IO ()
startToolServer' port isDebugMode = do
Expand Down Expand Up @@ -108,9 +104,6 @@ startToolServer' port isDebugMode = do
(LiveReloadNotificationServer.app liveReloadNotificationServerState)
application

stopToolServer ToolServerStarted { thread } = uninterruptibleCancel thread
stopToolServer ToolServerNotStarted = pure ()

openUrl :: Text -> IO ()
openUrl url = do
selectedBrowser <- Env.lookupEnv "IHP_BROWSER"
Expand Down
11 changes: 0 additions & 11 deletions IHP/IDE/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ data Action =
| SchemaChanged
| UpdateStatusServerState !StatusServerState
| UpdateLiveReloadNotificationServerState !LiveReloadNotificationServerState
| UpdateToolServerState !ToolServerState
| PauseApp
deriving (Show)

Expand Down Expand Up @@ -101,14 +100,6 @@ instance Show StatusServerState where
show StatusServerStarted { } = "Started"
show StatusServerPaused { } = "Paused"

data ToolServerState
= ToolServerNotStarted
| ToolServerStarted { thread :: !(Async ()) }

instance Show ToolServerState where
show ToolServerNotStarted = "NotStarted"
show ToolServerStarted {} = "Started"


instance Show (IORef x) where show _ = "(..)"
instance Show ProcessHandle where show _ = "(..)"
Expand All @@ -119,7 +110,6 @@ data AppState = AppState
, appGHCIState :: !AppGHCIState
, statusServerState :: !StatusServerState
, liveReloadNotificationServerState :: !LiveReloadNotificationServerState
, toolServerState :: !ToolServerState
, databaseNeedsMigration :: !(IORef Bool)
, lastSchemaCompilerError :: !(IORef (Maybe SomeException))
} deriving (Show)
Expand All @@ -134,7 +124,6 @@ emptyAppState = do
, appGHCIState = AppGHCINotStarted
, statusServerState = StatusServerNotStarted
, liveReloadNotificationServerState = LiveReloadNotificationServerState { clients }
, toolServerState = ToolServerNotStarted
, databaseNeedsMigration
, lastSchemaCompilerError
}
Expand Down
24 changes: 11 additions & 13 deletions exe/IHP/IDE/DevServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,17 @@ main = withUtf8 do

start

withAsync consumeGhciOutput \_ -> do
withFileWatcher do
async Telemetry.reportTelemetry
forever do
appState <- readIORef appStateRef
when isDebugMode (Log.debug $ " ===> " <> (tshow appState))
action <- takeMVar actionVar
when isDebugMode (Log.debug $ tshow action)
nextAppState <- handleAction appState action
writeIORef appStateRef nextAppState
withToolServer do
withAsync consumeGhciOutput \_ -> do
withFileWatcher do
async Telemetry.reportTelemetry
forever do
appState <- readIORef appStateRef
when isDebugMode (Log.debug $ " ===> " <> (tshow appState))
action <- takeMVar actionVar
when isDebugMode (Log.debug $ tshow action)
nextAppState <- handleAction appState action
writeIORef appStateRef nextAppState


handleAction :: (?context :: Context) => AppState -> Action -> IO AppState
Expand All @@ -88,7 +89,6 @@ handleAction state@(AppState { appGHCIState }) (UpdatePostgresState postgresStat
otherwise -> pure state { postgresState }
otherwise -> pure state { postgresState }
handleAction state (UpdateAppGHCIState appGHCIState) = pure state { appGHCIState }
handleAction state (UpdateToolServerState toolServerState) = pure state { toolServerState }
handleAction state@(AppState { statusServerState = StatusServerNotStarted }) (UpdateStatusServerState statusServerState) = pure state { statusServerState }
handleAction state@(AppState { statusServerState = StatusServerStarted { } }) (UpdateStatusServerState StatusServerNotStarted) = pure state { statusServerState = StatusServerNotStarted }
handleAction state@(AppState { statusServerState = StatusServerPaused { } }) (UpdateStatusServerState statusServerState) = pure state { statusServerState = StatusServerNotStarted }
Expand Down Expand Up @@ -186,7 +186,6 @@ handleAction state@(AppState { appGHCIState }) PauseApp =

start :: (?context :: Context) => IO ()
start = do
async startToolServer
async startStatusServer
async startAppGHCI
async startPostgres
Expand All @@ -198,7 +197,6 @@ stop AppState { .. } = do
stopAppGHCI appGHCIState
stopPostgres postgresState
stopStatusServer statusServerState
stopToolServer toolServerState

startGHCI :: IO ManagedProcess
startGHCI = do
Expand Down

0 comments on commit 00fb198

Please sign in to comment.