Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring the filesystem with Haskell

I'm using the FSNotify package to watch my filesystem for changes to markdown files, so I can run them through Pandoc automatically. However, I'm having trouble getting the manager to exit nicely.

As it stands, my code is based on Yesod's use of the same package:

module Main where

import System.FSNotify
import Filesystem (getWorkingDirectory)
import System.Exit
import Control.Concurrent

main = do
    cwd <- getWorkingDirectory
    putStrLn "Watching current directory, press RETURN to exit."
    withManager $ \man -> do
        _ <- forkIO $ do
            watchTree man cwd (const True) $ \fp -> print fp
        _ <- getLine
        exitSuccess

When I use runhaskell, the program works perfectly, but if I compile it to an executable (using ghc --make), when I press enter, my terminal becomes unresponsive. I'm compiling this on Windows 7 64-bit.

EDIT: Completed code. Well, not entirely completed - there's still more I want to do with this - but working.

like image 495
Daniel Buckmaster Avatar asked May 16 '13 07:05

Daniel Buckmaster


1 Answers

Whenever I see a question of the form "works in GHCi, hangs when compiled", I think to myself "you need the threaded runtime".

Try recompiling with -threaded and see if that helps.

(GHCi is compiled with this flag, which explains the difference. I do recall there's some difference in how external function calls work depending on whether you use the threaded runtime or not...)

like image 74
MathematicalOrchid Avatar answered Nov 02 '22 12:11

MathematicalOrchid