on master help sign in
add max-retries to waitOpen and gracefully handle remove/delete events
commite987485e2d6c3b73bf917890d8cf965173af3716 authorJorge Israel Peña <jorgepblank@gmail.com> dateWed 22 May 2013, 17:06 (13 years ago) parent2fba64c
1 file changed+22−11
src/Hakyll/Preview/Poll.hs+22−11
src/Hakyll/Preview/Poll.hs+22−11
@@ -8,7 +8,7 @@ module Hakyll.Preview.Poll
88
99--------------------------------------------------------------------------------
1010import Control.Concurrent.MVar (newMVar, putMVar, takeMVar)
11−import Control.Monad (when)
11+import Control.Monad (when, void)
1212import Filesystem.Path.CurrentOS (decodeString, encodeString)
1313import System.Directory (canonicalizePath)
1414import System.FilePath (pathSeparators, (</>))
@@ -16,10 +16,13 @@ import System.FSNotify (Event (..), WatchConfig (..),
1616 startManagerConf, watchTree)
1717
1818#ifdef mingw32_HOST_OS
19−import System.IO (IOMode(ReadMode), Handle, openFile, hClose)
20−import System.IO.Error (isPermissionError)
21−import Control.Concurrent (threadDelay)
22−import Control.Exception (IOException, throw, try)
19+import System.IO (IOMode(ReadMode), Handle, openFile,
20+ hClose)
21+import System.IO.Error (isPermissionError)
22+import Control.Concurrent (threadDelay)
23+import Control.Exception (IOException, throw, try)
24+import System.Exit (exitFailure)
25+import System.Directory (doesFileExist)
2326#endif
2427
2528--------------------------------------------------------------------------------
@@ -53,24 +56,32 @@ watchUpdates conf update = do
5356 watchTree manager providerDir (not . isRemove) $ \event -> do
5457 () <- takeMVar lock
5558 allowed' <- allowed event
56− when allowed' $ update' ((encodeString providerDir) </> (eventPath event))
59+ when allowed' $ update' event (encodeString providerDir)
5760 putMVar lock ()
5861 where
5962#ifndef mingw32_HOST_OS
60− update' _ = update >> return ()
63+ update' _ _ = void update
6164#else
62− update' path = waitOpen path ReadMode (\_ -> update) >> return ()
65+ update' event provider = do
66+ let path = provider </> eventPath event
67+ -- on windows
68+ fileExists <- doesFileExist path
69+
70+ when fileExists . void $ waitOpen path ReadMode (\_ -> update) 10
6371
6472 -- continuously attempts to open the file in between sleep intervals
6573 -- handler is run only once it is able to open the file
66− waitOpen :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
67− waitOpen path mode handler = do
74+ waitOpen :: FilePath -> IOMode -> (Handle -> IO r) -> Integer -> IO r
75+ waitOpen _ _ _ 0 = do
76+ putStrLn "[ERROR] Failed to retrieve modified file for regeneration"
77+ exitFailure
78+ waitOpen path mode handler retries = do
6879 res <- try $ openFile path mode :: IO (Either IOException Handle)
6980 case res of
7081 Left ex -> if isPermissionError ex
7182 then do
7283 threadDelay 100000
73− waitOpen path mode handler
84+ waitOpen path mode handler (retries - 1)
7485 else throw ex
7586 Right h -> do
7687 handled <- handler h