Changed the way I handled the Bot monad for more concurrency.
This commit is contained in:
parent
bf36db5488
commit
5d2c3cdeb2
4 changed files with 57 additions and 45 deletions
|
@ -10,29 +10,32 @@ import System.Log.Logger
|
|||
|
||||
import Hsbot.Types
|
||||
|
||||
loadPlugin :: PluginId -> Bot (Env IO) ()
|
||||
loadPlugin :: PluginId -> Env IO ()
|
||||
loadPlugin pId = do
|
||||
bot <- get
|
||||
chan <- liftIO (newChan :: IO (Chan Message))
|
||||
master <- lift $ asks envChan
|
||||
let name = pluginName pId
|
||||
loop = pluginEp pId
|
||||
oldPlugins = botPlugins bot
|
||||
pState = PluginState { pluginId = pId
|
||||
, pluginChan = chan
|
||||
, pluginMaster = master }
|
||||
-- We check for unicity
|
||||
case M.lookup name oldPlugins of
|
||||
Just _ -> liftIO . warningM "Hsbot.Core.LoadPlugin" $ "Not loading already loaded plugin : " ++ name
|
||||
Nothing -> do
|
||||
liftIO . infoM "Hsbot.Core.LoadPlugin" $ "Loading plugin : " ++ name
|
||||
env <- lift ask
|
||||
finalStateMVar <- liftIO newEmptyMVar
|
||||
threadId <- liftIO . forkIO $ runReaderT (execStateT loop pState >>= storeFinalState finalStateMVar) env
|
||||
let newPlugins = M.insert name (pState, finalStateMVar, threadId) oldPlugins
|
||||
put $ bot { botPlugins = newPlugins
|
||||
, botHooks = chan : botHooks bot }
|
||||
botMVar <- asks envBotState
|
||||
(liftIO $ takeMVar botMVar) >>= execStateT effectivelyLoadPlugin >>= liftIO . putMVar botMVar
|
||||
where
|
||||
effectivelyLoadPlugin :: Bot (Env IO) ()
|
||||
effectivelyLoadPlugin = do
|
||||
bot <- get
|
||||
chan <- liftIO (newChan :: IO (Chan Message))
|
||||
master <- lift $ asks envChan
|
||||
let name = pluginName pId
|
||||
loop = pluginEp pId
|
||||
oldPlugins = botPlugins bot
|
||||
pState = PluginState { pluginId = pId
|
||||
, pluginChan = chan
|
||||
, pluginMaster = master }
|
||||
case M.lookup name oldPlugins of
|
||||
Just _ -> liftIO . warningM "Hsbot.Core.LoadPlugin" $ "Not loading already loaded plugin : " ++ name
|
||||
Nothing -> do
|
||||
liftIO . infoM "Hsbot.Core.LoadPlugin" $ "Loading plugin : " ++ name
|
||||
env <- lift ask
|
||||
finalStateMVar <- liftIO newEmptyMVar
|
||||
threadId <- liftIO . forkIO $ runReaderT (execStateT loop pState >>= storeFinalState finalStateMVar) env
|
||||
let newPlugins = M.insert name (pState, finalStateMVar, threadId) oldPlugins
|
||||
put $ bot { botPlugins = newPlugins
|
||||
, botHooks = chan : botHooks bot }
|
||||
storeFinalState :: MVar PluginState -> PluginState -> Env IO ()
|
||||
storeFinalState finalStateMVar finalState = liftIO $ putMVar finalStateMVar finalState
|
||||
|
||||
|
|
Reference in a new issue