Added signal handling and a better semantic for bot status.
This commit is contained in:
parent
57bec4921b
commit
9dda7dc1b9
4 changed files with 39 additions and 10 deletions
|
@ -10,6 +10,7 @@ import qualified Data.Map as M
|
||||||
import Data.Time
|
import Data.Time
|
||||||
import Prelude hiding (catch)
|
import Prelude hiding (catch)
|
||||||
import System.IO()
|
import System.IO()
|
||||||
|
import System.Posix.Signals
|
||||||
|
|
||||||
import Hsbot.Config
|
import Hsbot.Config
|
||||||
import Hsbot.Message
|
import Hsbot.Message
|
||||||
|
@ -23,6 +24,9 @@ hsbot config = do
|
||||||
putStrLn "[Hsbot] Opening communication channel... "
|
putStrLn "[Hsbot] Opening communication channel... "
|
||||||
chan <- newChan :: IO (Chan BotMsg)
|
chan <- newChan :: IO (Chan BotMsg)
|
||||||
mvar <- newMVar M.empty :: IO (MVar BotResumeData)
|
mvar <- newMVar M.empty :: IO (MVar BotResumeData)
|
||||||
|
putStrLn "[Hsbot] Installing signal handlers... "
|
||||||
|
_ <- installHandler sigHUP (Catch $ sigHupHandler chan) Nothing
|
||||||
|
_ <- installHandler sigTERM (Catch $ sigTermHandler chan) Nothing
|
||||||
putStrLn "[Hsbot] Spawning IrcBot plugins... "
|
putStrLn "[Hsbot] Spawning IrcBot plugins... "
|
||||||
botState <- execStateT spawnPlugins BotState { botStartTime = startTime
|
botState <- execStateT spawnPlugins BotState { botStartTime = startTime
|
||||||
, botPlugins = M.empty
|
, botPlugins = M.empty
|
||||||
|
@ -40,7 +44,8 @@ hsbot config = do
|
||||||
where
|
where
|
||||||
runLoop :: BotState -> IO (BotStatus, BotState)
|
runLoop :: BotState -> IO (BotStatus, BotState)
|
||||||
runLoop botState = do
|
runLoop botState = do
|
||||||
(status, botState') <- (runStateT botCore botState) `catch` (\(_ :: IOException) -> return (BotExit, botState))
|
(status, botState') <- (runStateT botCore botState) `catches` [ Handler (\ (_ :: IOException) -> return (BotExit, botState))
|
||||||
|
, Handler (\ (_ :: AsyncException) -> return (BotExit, botState)) ]
|
||||||
case status of
|
case status of
|
||||||
BotContinue -> runLoop botState'
|
BotContinue -> runLoop botState'
|
||||||
_ -> return (status, botState')
|
_ -> return (status, botState')
|
||||||
|
@ -55,10 +60,10 @@ botCore = do
|
||||||
chan <- gets botChan
|
chan <- gets botChan
|
||||||
msg <- liftIO $ readChan chan
|
msg <- liftIO $ readChan chan
|
||||||
case msg of
|
case msg of
|
||||||
InMsg _ -> return BotContinue
|
IntMsg intMsg -> processInternalMessage intMsg
|
||||||
OutMsg _ -> return BotContinue
|
|
||||||
IntMsg intMsg -> processInternalMessage $ IntMsg intMsg
|
|
||||||
UpdMsg updMsg -> processUpdateMessage updMsg
|
UpdMsg updMsg -> processUpdateMessage updMsg
|
||||||
|
RebMsg rebMsg -> processRebootMessage rebMsg
|
||||||
|
ExiMsg exiMsg -> processExitMessage exiMsg
|
||||||
|
|
||||||
-- | Process an update command
|
-- | Process an update command
|
||||||
processUpdateMessage :: ResumeMsg -> Bot (BotStatus)
|
processUpdateMessage :: ResumeMsg -> Bot (BotStatus)
|
||||||
|
@ -69,3 +74,11 @@ processUpdateMessage msg = do
|
||||||
liftIO $ modifyMVar_ resumeData (\oldData -> return $ M.insert from stuff oldData)
|
liftIO $ modifyMVar_ resumeData (\oldData -> return $ M.insert from stuff oldData)
|
||||||
return BotContinue
|
return BotContinue
|
||||||
|
|
||||||
|
-- | signals handlers
|
||||||
|
sigHupHandler :: Chan BotMsg -> IO ()
|
||||||
|
sigHupHandler chan = writeChan chan $ RebMsg RebootMsg { rebMsgFrom = "HUP handler" }
|
||||||
|
|
||||||
|
-- | signals handlers
|
||||||
|
sigTermHandler :: Chan BotMsg -> IO ()
|
||||||
|
sigTermHandler chan = writeChan chan $ ExiMsg ExitMsg { exiMsgFrom = "TERM handler" }
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
module Hsbot.Message
|
module Hsbot.Message
|
||||||
( processInternalMessage
|
( processInternalMessage
|
||||||
|
, processRebootMessage
|
||||||
|
, processExitMessage
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Monad.State
|
import Control.Monad.State
|
||||||
|
@ -9,16 +11,15 @@ import Hsbot.PluginUtils
|
||||||
import Hsbot.Types
|
import Hsbot.Types
|
||||||
|
|
||||||
-- | Processes an internal message
|
-- | Processes an internal message
|
||||||
processInternalMessage :: BotMsg -> Bot (BotStatus)
|
processInternalMessage :: Msg -> Bot (BotStatus)
|
||||||
processInternalMessage (IntMsg msg)
|
processInternalMessage msg
|
||||||
| msgTo msg == "CORE" = processCoreMessage msg
|
| msgTo msg == "CORE" = processCoreMessage msg
|
||||||
| otherwise = do
|
| otherwise = do
|
||||||
plugins <- gets botPlugins
|
plugins <- gets botPlugins
|
||||||
case M.lookup (msgTo msg) plugins of
|
case M.lookup (msgTo msg) plugins of
|
||||||
Just (plugin, _) -> sendToPlugin (IntMsg msg) plugin
|
Just (plugin, _, _) -> sendToPlugin (IntMsg msg) plugin
|
||||||
Nothing -> return ()
|
Nothing -> return ()
|
||||||
return BotContinue
|
return BotContinue
|
||||||
processInternalMessage _ = return BotContinue
|
|
||||||
|
|
||||||
processCoreMessage :: Msg -> Bot (BotStatus)
|
processCoreMessage :: Msg -> Bot (BotStatus)
|
||||||
processCoreMessage msg = do
|
processCoreMessage msg = do
|
||||||
|
@ -26,3 +27,9 @@ processCoreMessage msg = do
|
||||||
"REBOOT" -> return BotReboot
|
"REBOOT" -> return BotReboot
|
||||||
_ -> return BotContinue
|
_ -> return BotContinue
|
||||||
|
|
||||||
|
processRebootMessage :: RebootMsg -> Bot (BotStatus)
|
||||||
|
processRebootMessage _ = return BotReboot -- TODO : check who is sending that to us
|
||||||
|
|
||||||
|
processExitMessage :: ExitMsg -> Bot (BotStatus)
|
||||||
|
processExitMessage _ = return BotExit -- TODO : check who is sending that to us
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,11 @@ module Hsbot.Types
|
||||||
, BotResumeData
|
, BotResumeData
|
||||||
, BotState (..)
|
, BotState (..)
|
||||||
, BotStatus (..)
|
, BotStatus (..)
|
||||||
|
, ExitMsg (..)
|
||||||
, Msg (..)
|
, Msg (..)
|
||||||
, Plugin
|
, Plugin
|
||||||
, PluginState (..)
|
, PluginState (..)
|
||||||
|
, RebootMsg (..)
|
||||||
, ResumeData
|
, ResumeData
|
||||||
, ResumeMsg (..)
|
, ResumeMsg (..)
|
||||||
) where
|
) where
|
||||||
|
@ -61,5 +63,13 @@ data ResumeMsg = ResMsg
|
||||||
, resMsgData :: ResumeData
|
, resMsgData :: ResumeData
|
||||||
} deriving (Show)
|
} deriving (Show)
|
||||||
|
|
||||||
data BotMsg = InMsg Msg | OutMsg Msg | IntMsg Msg | UpdMsg ResumeMsg deriving (Show)
|
data RebootMsg = RebootMsg
|
||||||
|
{ rebMsgFrom :: String
|
||||||
|
} deriving (Show)
|
||||||
|
|
||||||
|
data ExitMsg = ExitMsg
|
||||||
|
{ exiMsgFrom :: String
|
||||||
|
} deriving (Show)
|
||||||
|
|
||||||
|
data BotMsg = IntMsg Msg | UpdMsg ResumeMsg | RebMsg RebootMsg | ExiMsg ExitMsg deriving (Show)
|
||||||
|
|
||||||
|
|
1
TODO
1
TODO
|
@ -1,7 +1,6 @@
|
||||||
:julien!~julien@ogu21.corp PRIVMSG #shbot :@quote graou snif
|
:julien!~julien@ogu21.corp PRIVMSG #shbot :@quote graou snif
|
||||||
|
|
||||||
* Find a way to handle bot reloading threw exec
|
* Find a way to handle bot reloading threw exec
|
||||||
* Find a way to cleanly handle termination/reboot requests (signals, IOExceptions, plugins' requests)
|
|
||||||
|
|
||||||
* write the vote system for the quote module
|
* write the vote system for the quote module
|
||||||
* only the quote reporter should be able to edit it
|
* only the quote reporter should be able to edit it
|
||||||
|
|
Reference in a new issue