LoginSignup
2
0

More than 3 years have passed since last update.

System.FSNotifyでファイルイベントが一切検出されない時

Posted at

解決策: withManagerを実行しているスレッド自体を起動させ続ける

haddockの例プログラムのコメントに

-- start a watching job (in the background)

と書かれているので「これ自身でforkIOしてくれている」と思ってしまいがちだ(思ってしまった)が、
それを実行している側のスレッドも生かしておく必要があるらしい。
自分の場合、メインスレッドでは他のプログラムを動かしていたので、forever $ threadDelay 10000を抜かしてしまいこれにはまった。
なので、同じようにメインスレッドは別でやることがある場合以下のようにforkIOしてあげるといい。

watchUpdate ... = forkIO . withManager . \mng -> do
    -- `watchDir`等で監視する
    forever $ threadDelay 10000

ちなみに、自分が使った例はこちら(github: Cj-bc/brick-shgif)

-- | Watch specified file and send FileUpdated event
-- when it's updated.
watchUpdate :: BChan CustomEvent -> FilePath -> IO ()
watchUpdate ch fn = void . forkIO . withManager $ \mng -> do
    watchDir
        mng
        (takeDirectory fn)
        isWatchedFile
        sendEvent
    forever $ threadDelay 10000
    where
        isWatchedFile :: FSN.Event -> Bool
        isWatchedFile e = case e of
                 Modified fn _ _ -> True
                 otherwise       -> False

        sendEvent :: FSN.Event -> IO ()
        sendEvent = \_ -> writeBChan ch FileUpdated
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0