以下是我希望加入错误处理的代码概述。
worldHandler :: ProcessState -> JobCount -> IO ()
worldHandler world jCount = do
putStrLn "Entered worldHandler"
jcount <- takeMVar jCount
if jcount > 0
then incrementThenDone jcount
else doJobProcessing jcount
where incrementThenDone jcount = do
putMVar jCount (jcount+1)
doJobProcessing jcount = do
putMVar jCount (jcount+1)
preProcess world
initiateJob world
makeChart world
这里是
main
函数。main :: IO ()
main = do
world <- (newEmptyMVar :: IO ProcessState)
jCount <- (newMVar 0 :: IO JobCount)
installHandler userDefinedSignal1 (Catch $ worldHandler world jCount) Nothing
forever (threadDelay 10000)
函数
preProcess
、initiateJob
和makeChart
是我需要关注错误的地方。如果这些函数中的任何一个失败,我就会减少jCount
并调用logError
。然后,如果jCount > 0
,再次开始这三个函数。如果jCount == 0
,则继续等待下一个信号。每个这样的函数都将启动另一个进程,
readProcessWithExitCode
看起来是我想要的。然后我将使用退出代码来确定成功或失败。我想到的一个方法是创建一个
[preProcess,initiateJob,makeChart]
。然后,我将在此列表上映射函数jobProcessor
。如果其中一个函数从它们调用的程序接收到失败消息,我可以生成一个异常,并且映射将停止映射整个列表。这种方法行得通吗?
欢迎提出关于如何处理此问题的一般思路以及任何相关问题。
jCount
作为穷人版的 BoundedChan? - Thomas M. DuBuisson