[Haskell-cafe] thespian thoughts
Simon Michael
simon at joyful.com
Sun Oct 9 18:45:19 CEST 2011
Hi Alexander,
I went looking for something to clean up rss2irc's thread management, and your recently released thespian package looks like the simplest, most practical actors/erlang/OTP-ish lib for haskell so far. Thanks!
I need to restart threads (actors) in a controlled way when they die or hang. I think some of all of that is also part of Erlang/OTP, and maybe it's already on your todo list. Here are some notes I made, for your interest and in case you have any further thoughts.
I thought of adding some actor-transforming combinators like these:
-- | Spawn the specified action and restart it whenever it dies, unless
-- the restart frequency exceeds any of the given maximum frequencies in
-- which case throw an exception.
restart :: [Frequency] -> IO a -> IO Address
restart fs action = spawn $ do
setFlag TrapRemoteExceptions
let start times = []
forever
t <- getCurrentTime
update start times
when (any fs exceeded by start times) throw error
worker <- spawn action
monitor worker
receive worker [Case _ -> return ()]
-- | Convert hangs to exceptions using a watchdog timer: spawn the
-- specified action under the control of a watcher which monitors it and
-- also kills it and throws an exception if the worker ever fails to
-- send (something) to the watcher within the specified interval.
watchdog :: Seconds -> (Address -> IO a) -> IO Address
watchdog t action = spawn $ do
watcher <- self
worker <- spawn $ action watcher
monitor worker
forever
receiveTimeout t [Case _ -> return ()
Default -> throw error
]
which I would use something like:
main = do
(feed, bot) <- ...
spawn $ do
reader <- restart [MaxPerHour 2] $ watchdog 60 $ feedReader feed bot
announcer <- restart [MaxPerHour 2] $ watchdog 60 $ ircAnnouncer bot
responder <- restart [MaxPerHour 2] $ watchdog 60 $ ircResponder bot
monitor reader
monitor announcer
monitor responder
setFlag TrapRemoteExceptions
receive [Case e :: RemoteException -> exitFailure (show e) -- one of the above died or hung too frequently
]
but the types aren't right. I'm not sure if these combinators are possible with the current thespian api. Also they would complicate things, eg what is the address of a restarted actor ?
On second thoughts it might be better if these features were built in to the library. I imagine something like:
main = do ...
reader <- spawnWith [RestartSpec [MinInterval (minutes 5), MaxPerHour 3, MaxPerWeek 6], Watchdog (seconds 60)] $ feedReader feed bot
feedReader = do
forever
poll feed, do stuff
resetWatchdog
What do you think ?
Best - Simon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20111009/b9d9b1b5/attachment.htm>
More information about the Haskell-Cafe
mailing list