summaryrefslogtreecommitdiff
path: root/Hsbot/Plugin.hs
diff options
context:
space:
mode:
authorJulien Dessaux2010-05-29 23:48:47 +0200
committerJulien Dessaux2010-05-29 23:55:20 +0200
commit3410caa6f2e1fd74d55f7a1717420bb2e1052b12 (patch)
tree214cb220de340eada4141479ae6415e654a4fdc3 /Hsbot/Plugin.hs
parentAdded signal handling and a better semantic for bot status. (diff)
downloadhsbot-3410caa6f2e1fd74d55f7a1717420bb2e1052b12.tar.gz
hsbot-3410caa6f2e1fd74d55f7a1717420bb2e1052b12.tar.bz2
hsbot-3410caa6f2e1fd74d55f7a1717420bb2e1052b12.zip
Fixed the plugin termination.
The master of a plugin will now wait for the plugin it kills to terminate.
Diffstat (limited to 'Hsbot/Plugin.hs')
-rw-r--r--Hsbot/Plugin.hs13
1 files changed, 8 insertions, 5 deletions
diff --git a/Hsbot/Plugin.hs b/Hsbot/Plugin.hs
index e545e8d..3feffa8 100644
--- a/Hsbot/Plugin.hs
+++ b/Hsbot/Plugin.hs
@@ -5,8 +5,9 @@ module Hsbot.Plugin
, unloadPlugin
) where
-import Control.Concurrent
-import Control.Concurrent.Chan ()
+import Control.Concurrent (forkIO)
+import Control.Concurrent.Chan
+import Control.Concurrent.MVar
import Control.Exception
import Control.Monad.State
import qualified Data.Map as M
@@ -29,12 +30,13 @@ spawnPlugin (IrcBotConfig ircConfig) = do
bot <- get
let chan = botChan bot
pchan <- liftIO (newChan :: IO (Chan BotMsg))
- threadId <- liftIO $ forkIO (startIrcbot ircConfig chan pchan)
+ mvar <- liftIO newEmptyMVar
+ threadId <- liftIO . forkIO $ finally (startIrcbot ircConfig chan pchan) (putMVar mvar ())
let plugin = PluginState { pluginName = ircConfigName ircConfig
, pluginChan = pchan
, pluginHandles = M.empty }
plugins = botPlugins bot
- put $ bot { botPlugins = M.insert (pluginName plugin) (plugin, threadId) plugins }
+ put $ bot { botPlugins = M.insert (pluginName plugin) (plugin, mvar, threadId) plugins }
resumeData <- gets botResumeData
liftIO $ modifyMVar_ resumeData (\oldData -> return $ M.insert (ircConfigName ircConfig) M.empty oldData)
@@ -52,9 +54,10 @@ killPlugin name = do
let oldPlugins = botPlugins bot
-- We check if the plugin exists
case M.lookup name oldPlugins of
- Just (_, threadId) -> do
+ Just (_, mvar, threadId) -> do
let newPlugins = M.delete name oldPlugins
liftIO $ throwTo threadId UserInterrupt
put $ bot { botPlugins = newPlugins }
+ liftIO $ takeMVar mvar
Nothing -> return ()