summaryrefslogtreecommitdiff
path: root/Hsbot/Irc/Core.hs
diff options
context:
space:
mode:
authorJulien Dessaux2010-05-29 00:55:14 +0200
committerJulien Dessaux2010-05-29 01:01:31 +0200
commitdaea135424857594622573fa1a900b436d7ee0ae (patch)
tree586338270b862a7c44e597828413e918b2cd24a0 /Hsbot/Irc/Core.hs
parentCleaned further the bot updates handling. (diff)
downloadhsbot-daea135424857594622573fa1a900b436d7ee0ae.tar.gz
hsbot-daea135424857594622573fa1a900b436d7ee0ae.tar.bz2
hsbot-daea135424857594622573fa1a900b436d7ee0ae.zip
Implemented the clean killing of plugins' threads.
Diffstat (limited to 'Hsbot/Irc/Core.hs')
-rw-r--r--Hsbot/Irc/Core.hs17
1 files changed, 11 insertions, 6 deletions
diff --git a/Hsbot/Irc/Core.hs b/Hsbot/Irc/Core.hs
index 2fb8386..229fef8 100644
--- a/Hsbot/Irc/Core.hs
+++ b/Hsbot/Irc/Core.hs
@@ -3,7 +3,7 @@ module Hsbot.Irc.Core
) where
import Control.Concurrent
-import Control.Exception (IOException, catch)
+import Control.Exception (AsyncException, Handler (..), IOException, catch, catches)
import Control.Monad.State
import qualified Data.Map as M
import Data.Maybe (fromMaybe)
@@ -34,7 +34,7 @@ startIrcbot config masterChan myChan = do
putStrLn "[IrcBot] Spawning reader threads..."
myOwnThreadId <- myThreadId
readerThreadId <- forkIO $ ircBotReader handle chan myOwnThreadId
- masterReaderThreadId <- forkIO $ ircBotMasterReader masterChan chan
+ masterReaderThreadId <- forkIO $ ircBotMasterReader myChan chan
putStrLn "[IrcBot] Initializing server connection..."
let ircServerState = IrcServerState { ircServerId = ircConfigAddress config
, ircServerChannels = []
@@ -46,18 +46,23 @@ startIrcbot config masterChan myChan = do
, ircBotCommands = M.empty
, ircBotChan = chan
, ircBotMasterChan = masterChan
- , ircBotMyChan = myChan
, ircBotServerState = ircServerState
, ircBotHandle = handle
, ircBotConfig = config
- , ircBotReaderThreadId = readerThreadId
- , ircBotMasterReaderThreadId = masterReaderThreadId
, ircBotResumeData = M.singleton "HANDLE" (show fd) }
ircBotState' <- execStateT (initBotServerConnection config) ircBotState
putStrLn "[IrcBot] Spawning plugins..."
ircBotState'' <- execStateT spawnIrcPlugins ircBotState'
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 ()
--resumeIrcBot