Skip to content

Commit

Permalink
[Chore] Extract initBotState
Browse files Browse the repository at this point in the history
Problem: Sometimes, during a debugging session, we may want to make
calls to Slack's API in a ghci repl. It would be useful to be able to
call `runBotM :: BotState -> BotM a -> IO a` with little hassle.

However, initializing a `BotState` is no easy task.

Solution: extract the `BotState` initialization logic into
`initBotState`.

Now, we can do something like this in the repl:

```
runManaged $ initBotState (Options Nothing) >>= \bs -> liftIO $ runBotM bs $ getUserCached "U036E339XBR" >>= print
```
  • Loading branch information
dcastro committed Sep 7, 2023
1 parent 015c099 commit afb7368
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions src/TzBot/BotMain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module TzBot.BotMain where

import TzPrelude

import Control.Monad.Managed (managed, runManaged)
import Control.Monad.Managed (Managed, managed, runManaged)
import Data.ByteString qualified as BS
import Network.HTTP.Client (newManager)
import Network.HTTP.Client.TLS (tlsManagerSettings)
Expand Down Expand Up @@ -59,42 +59,48 @@ dumpConfig = \case
use --force to overwrite|] >> exitFailure)
writeAction

run :: Options -> IO ()
run opts = do
initBotState :: Options -> Managed BotState
initBotState opts = do
let mbConfigFilePath = oConfigFile opts
bsConfig <- readConfig mbConfigFilePath
runManaged $ do
bsConfig <- liftIO $ readConfig mbConfigFilePath

let fifteenPercentAmplitudeSettings = defaultTzCacheSettings
{ tcsExpiryRandomAmplitudeFraction = Just 0.15
}
bsManager <- liftIO $ newManager tlsManagerSettings
bsFeedbackConfig <-
managed $ withFeedbackConfig bsConfig
bsUserInfoCache <-
managed $ withTzCache fifteenPercentAmplitudeSettings bsConfig.cCacheUsersInfo
bsConversationMembersCache <-
managed $ withTzCache fifteenPercentAmplitudeSettings bsConfig.cCacheConversationMembers
let defaultMessageInfoCachingTime = hour 1
bsMessageCache <-
managed $ withTzCacheDefault defaultMessageInfoCachingTime
bsMessageLinkCache <-
managed $ withTzCacheDefault defaultMessageInfoCachingTime
bsReportEntries <-
managed $ withTzCacheDefault bsConfig.cCacheReportDialog
(bsLogNamespace, bsLogContext, bsLogEnv) <- managed $ withLogger bsConfig.cLogLevel
pure BotState {..}

let fifteenPercentAmplitudeSettings = defaultTzCacheSettings
{ tcsExpiryRandomAmplitudeFraction = Just 0.15
}


run :: Options -> IO ()
run opts = do
runManaged $ do
botState <- initBotState opts
gracefulShutdownContainer <- liftIO $ newIORef $ (pure () :: IO ())
let extractShutdownFunction :: IO () -> IO ()
extractShutdownFunction = writeIORef gracefulShutdownContainer
let sCfg = defaultSlackConfig
& setApiToken (unBotToken bsConfig.cBotToken)
& setAppToken (unAppLevelToken bsConfig.cAppToken)
& setApiToken (unBotToken botState.bsConfig.cBotToken)
& setAppToken (unAppLevelToken botState.bsConfig.cAppToken)
& setOnException handleThreadExceptionSensibly -- auto-handle disconnects
& setGracefulShutdownHandler extractShutdownFunction

bsManager <- liftIO $ newManager tlsManagerSettings
bsFeedbackConfig <-
managed $ withFeedbackConfig bsConfig
bsUserInfoCache <-
managed $ withTzCache fifteenPercentAmplitudeSettings bsConfig.cCacheUsersInfo
bsConversationMembersCache <-
managed $ withTzCache fifteenPercentAmplitudeSettings bsConfig.cCacheConversationMembers
let defaultMessageInfoCachingTime = hour 1
bsMessageCache <-
managed $ withTzCacheDefault defaultMessageInfoCachingTime
bsMessageLinkCache <-
managed $ withTzCacheDefault defaultMessageInfoCachingTime
bsReportEntries <-
managed $ withTzCacheDefault bsConfig.cCacheReportDialog
-- auto-acknowledge received messages
(bsLogNamespace, bsLogContext, bsLogEnv) <- managed $ withLogger bsConfig.cLogLevel
liftIO $ runSocketMode sCfg $ handler gracefulShutdownContainer BotState {..}
liftIO $ runSocketMode sCfg $ handler gracefulShutdownContainer botState

withFeedbackConfig :: BotConfig -> (FeedbackConfig -> IO a) -> IO a
withFeedbackConfig config action = do
Expand Down

0 comments on commit afb7368

Please sign in to comment.