@@ -1,6 +1,7 @@
11module Katip.Scribes.RotatingHandler where
22
33import Control.Applicative
4+import Control.Concurrent
45import Control.Concurrent.Async
56import Control.Concurrent.STM
67import Control.Debounce
@@ -65,7 +66,7 @@ newFileOwner fp s = do
6566 <|> (Right <$> readAllData)
6667 debounce action = case fosDebounceFreq s of
6768 Nothing -> do
68− return action
69+ return $ return ()
6970 Just freq -> mkDebounce $ defaultDebounceSettings
7071 { debounceAction = action
7172 , debounceFreq = freq }
@@ -75,7 +76,7 @@ newFileOwner fp s = do
7576 Right bs -> do
7677 h <- readIORef ref
7778 BL.hPutStr h $ mconcat bs
78− flush
79+ flush -- auto debounced flush
7980 recur
8081 Left c -> case c of
8182 CloseMsg -> return () -- release will close the handler
@@ -90,7 +91,14 @@ newFileOwner fp s = do
9091 recur
9192 recur
9293 worker :: IO ()
93− worker = bracket ack release go
94+ worker = try (bracket ack release go) >>= \case
95+ Left (_ :: SomeException) -> do
96+ threadDelay 100000
97+ -- to not restart the worker function too often
98+ worker
99+ -- We dont want the worker thread become unavailable because
100+ -- of exceptions. E.g. could not create or write the file.
101+ Right () -> return ()
94102 asyncRet <- async worker
95103 return $ FileOwner
96104 { foDataQueune = dqueue