Archived
1
0
Fork 0

Implemented the clean killing of plugins' threads.

This commit is contained in:
Julien Dessaux 2010-05-29 00:55:14 +02:00
parent c506c58e92
commit daea135424
6 changed files with 70 additions and 33 deletions

View file

@ -31,9 +31,11 @@ hsbot config = do
, botResumeData = mvar } , botResumeData = mvar }
putStrLn "[Hsbot] Entering main loop... " putStrLn "[Hsbot] Entering main loop... "
(status, botState') <- runLoop botState (status, botState') <- runLoop botState
putStrLn "[Hsbot] Killing active plugins... "
resumeData <- takeMVar mvar resumeData <- takeMVar mvar
evalStateT (mapM_ killPlugin $ M.keys resumeData) botState'
if status == BotReboot if status == BotReboot
then resumeHsbot botState' resumeData then resumeHsbot resumeData
else return () else return ()
where where
runLoop :: BotState -> IO (BotStatus, BotState) runLoop :: BotState -> IO (BotStatus, BotState)
@ -43,8 +45,8 @@ hsbot config = do
BotContinue -> runLoop botState' BotContinue -> runLoop botState'
_ -> return (status, botState') _ -> return (status, botState')
resumeHsbot :: BotState -> BotResumeData -> IO () resumeHsbot :: BotResumeData -> IO ()
resumeHsbot botState resumeData = do resumeHsbot resumeData = do
print resumeData print resumeData
-- | Run the bot main loop -- | Run the bot main loop

View file

@ -3,7 +3,7 @@ module Hsbot.Irc.Core
) where ) where
import Control.Concurrent import Control.Concurrent
import Control.Exception (IOException, catch) import Control.Exception (AsyncException, Handler (..), IOException, catch, catches)
import Control.Monad.State import Control.Monad.State
import qualified Data.Map as M import qualified Data.Map as M
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
@ -34,7 +34,7 @@ startIrcbot config masterChan myChan = do
putStrLn "[IrcBot] Spawning reader threads..." putStrLn "[IrcBot] Spawning reader threads..."
myOwnThreadId <- myThreadId myOwnThreadId <- myThreadId
readerThreadId <- forkIO $ ircBotReader handle chan myOwnThreadId readerThreadId <- forkIO $ ircBotReader handle chan myOwnThreadId
masterReaderThreadId <- forkIO $ ircBotMasterReader masterChan chan masterReaderThreadId <- forkIO $ ircBotMasterReader myChan chan
putStrLn "[IrcBot] Initializing server connection..." putStrLn "[IrcBot] Initializing server connection..."
let ircServerState = IrcServerState { ircServerId = ircConfigAddress config let ircServerState = IrcServerState { ircServerId = ircConfigAddress config
, ircServerChannels = [] , ircServerChannels = []
@ -46,18 +46,23 @@ startIrcbot config masterChan myChan = do
, ircBotCommands = M.empty , ircBotCommands = M.empty
, ircBotChan = chan , ircBotChan = chan
, ircBotMasterChan = masterChan , ircBotMasterChan = masterChan
, ircBotMyChan = myChan
, ircBotServerState = ircServerState , ircBotServerState = ircServerState
, ircBotHandle = handle , ircBotHandle = handle
, ircBotConfig = config , ircBotConfig = config
, ircBotReaderThreadId = readerThreadId
, ircBotMasterReaderThreadId = masterReaderThreadId
, ircBotResumeData = M.singleton "HANDLE" (show fd) } , ircBotResumeData = M.singleton "HANDLE" (show fd) }
ircBotState' <- execStateT (initBotServerConnection config) ircBotState ircBotState' <- execStateT (initBotServerConnection config) ircBotState
putStrLn "[IrcBot] Spawning plugins..." putStrLn "[IrcBot] Spawning plugins..."
ircBotState'' <- execStateT spawnIrcPlugins ircBotState' ircBotState'' <- execStateT spawnIrcPlugins ircBotState'
putStrLn "[IrcBot] Entering Core loop... " putStrLn "[IrcBot] Entering Core loop... "
(evalStateT ircBotLoop ircBotState'') `catch` (\(_ :: IOException) -> return ()) ircBotState''' <- (execStateT ircBotLoop ircBotState'') `catches` [ Handler (\ (_ :: IOException) -> return (ircBotState''))
, Handler (\ (_ :: AsyncException) -> return (ircBotState'')) ]
putStrLn "[IrcBot] Killing reader threads..."
killThread readerThreadId
killThread masterReaderThreadId
putStrLn "[IrcBot] Killing active plugins... "
let resumeData = ircBotResumeData ircBotState'''
ircPlugins = read (fromMaybe [] (M.lookup "PLUGINS" resumeData)) :: [String]
evalStateT (mapM_ killIrcPlugin ircPlugins) ircBotState'''
return () return ()
--resumeIrcBot --resumeIrcBot

View file

@ -1,6 +1,7 @@
module Hsbot.Irc.Plugin module Hsbot.Irc.Plugin
( IrcPlugin ( IrcPlugin
, IrcPluginState (..) , IrcPluginState (..)
, killIrcPlugin
, listPlugins , listPlugins
, loadIrcPlugin , loadIrcPlugin
, sendToPlugin , sendToPlugin
@ -72,16 +73,23 @@ listPlugins originalRequest dest = do
-- | Unloads a plugin -- | Unloads a plugin
unloadIrcPlugin :: String -> IrcBot () unloadIrcPlugin :: String -> IrcBot ()
unloadIrcPlugin name = do unloadIrcPlugin name = do
killIrcPlugin name
ircbot <- get
let oldResumeData = ircBotResumeData ircbot
newPlugins = M.keys $ ircBotPlugins ircbot
newResumeData = M.insert "PLUGINS" (show newPlugins) oldResumeData
put $ ircbot { ircBotResumeData = newResumeData }
-- | kills a plugin
killIrcPlugin :: String -> IrcBot ()
killIrcPlugin name = do
ircbot <- get ircbot <- get
let oldPlugins = ircBotPlugins ircbot let oldPlugins = ircBotPlugins ircbot
oldResumeData = ircBotResumeData ircbot
-- We check if the plugin exists -- We check if the plugin exists
case M.lookup name oldPlugins of case M.lookup name oldPlugins of
Just (_, threadId) -> do Just (_, threadId) -> do
let newPlugins = M.delete name oldPlugins let newPlugins = M.delete name oldPlugins
newResumeData = M.insert "PLUGINS" (show $ M.keys newPlugins) oldResumeData
liftIO $ throwTo threadId UserInterrupt liftIO $ throwTo threadId UserInterrupt
put $ ircbot { ircBotPlugins = newPlugins put $ ircbot { ircBotPlugins = newPlugins }
, ircBotResumeData = newResumeData }
Nothing -> return () Nothing -> return ()

View file

@ -26,12 +26,9 @@ data IrcBotState = IrcBotState
, ircBotCommands :: M.Map String [String] -- Loaded plugins , ircBotCommands :: M.Map String [String] -- Loaded plugins
, ircBotChan :: Chan IrcBotMsg -- The IrcBot's communication channel , ircBotChan :: Chan IrcBotMsg -- The IrcBot's communication channel
, ircBotMasterChan :: Chan BotMsg -- The Hsbot communication channel , ircBotMasterChan :: Chan BotMsg -- The Hsbot communication channel
, ircBotMyChan :: Chan BotMsg -- The Hsbot communication channel
, ircBotServerState :: IrcServerState -- The state of the IrcServer , ircBotServerState :: IrcServerState -- The state of the IrcServer
, ircBotHandle :: Handle -- The server's socket/handle , ircBotHandle :: Handle -- The server's socket/handle
, ircBotConfig :: IrcConfig -- The starting configuration , ircBotConfig :: IrcConfig -- The starting configuration
, ircBotReaderThreadId :: ThreadId -- the thread that process inputs from the socket
, ircBotMasterReaderThreadId :: ThreadId -- the thread that process inputs from the master bot
, ircBotResumeData :: ResumeData -- the necessary data to resume the bot's operations on reboot , ircBotResumeData :: ResumeData -- the necessary data to resume the bot's operations on reboot
} }

View file

@ -1,11 +1,16 @@
module Hsbot.Plugin module Hsbot.Plugin
( spawnIrcPlugins ( killPlugin
, spawnIrcPlugins
, spawnIrcPlugin
, unloadPlugin
) where ) where
import Control.Concurrent import Control.Concurrent
import Control.Concurrent.Chan () import Control.Concurrent.Chan ()
import Control.Exception
import Control.Monad.State import Control.Monad.State
import qualified Data.Map as M import qualified Data.Map as M
import Prelude hiding (catch)
import Hsbot.Config import Hsbot.Config
import Hsbot.Irc.Config import Hsbot.Irc.Config
@ -17,9 +22,10 @@ spawnIrcPlugins :: Bot ()
spawnIrcPlugins = do spawnIrcPlugins = do
config <- gets botConfig config <- gets botConfig
mapM_ (spawnIrcPlugin) (ircConfigs config) mapM_ (spawnIrcPlugin) (ircConfigs config)
where
spawnIrcPlugin :: IrcConfig -> Bot () -- | spawns a single irc plugin
spawnIrcPlugin config = do spawnIrcPlugin :: IrcConfig -> Bot ()
spawnIrcPlugin config = do
bot <- get bot <- get
let chan = botChan bot let chan = botChan bot
pchan <- liftIO (newChan :: IO (Chan BotMsg)) pchan <- liftIO (newChan :: IO (Chan BotMsg))
@ -30,3 +36,23 @@ spawnIrcPlugins = do
plugins = botPlugins bot plugins = botPlugins bot
put $ bot { botPlugins = M.insert (pluginName plugin) (plugin, threadId) plugins } put $ bot { botPlugins = M.insert (pluginName plugin) (plugin, threadId) plugins }
-- | Unloads a plugin
unloadPlugin :: String -> Bot ()
unloadPlugin name = do
killPlugin name
resumeData <- gets botResumeData
liftIO $ modifyMVar_ resumeData (\oldData -> return $ M.delete name oldData)
-- | kills a plugin
killPlugin :: String -> Bot ()
killPlugin name = do
bot <- get
let oldPlugins = botPlugins bot
-- We check if the plugin exists
case M.lookup name oldPlugins of
Just (_, threadId) -> do
let newPlugins = M.delete name oldPlugins
liftIO $ throwTo threadId UserInterrupt
put $ bot { botPlugins = newPlugins }
Nothing -> return ()

1
TODO
View file

@ -1,6 +1,5 @@
:julien!~julien@ogu21.corp PRIVMSG #shbot :@quote graou snif :julien!~julien@ogu21.corp PRIVMSG #shbot :@quote graou snif
* Handle clean termination of all thread on reboot
* 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) * Find a way to cleanly handle termination/reboot requests (signals, IOExceptions, plugins' requests)