effectful-config-file 0.1.2.4 → 0.1.3.0
raw patch · 4 files changed
+140/−2 lines, 4 filesdep +asyncdep +fsnotifyPVP ok
version bump matches the API change (PVP)
Dependencies added: async, fsnotify
API changes (from Hackage documentation)
+ Effectful.Config.Watch: watchConfig :: forall (es :: [Effect]) c. (IOE :> es, Log :> es, Config c :> es, FromValue c) => FilePath -> (c -> c -> Eff es ()) -> Eff es ()
Files
- CHANGELOG.md +6/−0
- effectful-config-file.cabal +7/−2
- src/Effectful/Config/Watch.hs +65/−0
- test/Effectful/Config/WatchSpec.hs +62/−0
CHANGELOG.md view
@@ -1,3 +1,9 @@+## [1.3.0](https://github.com/haskell-github-trust/effectful-config-file/compare/1.2.4...1.3.0) (2026-12-03)++### Features++* add file watcher with auto-reload ([1cb3e8c](https://github.com/haskell-github-trust/effectful-config-file/commit/1cb3e8ce7588c2837a189405c08bd37abe1ea4e6))+ ## [1.2.4](https://github.com/haskell-github-trust/effectful-config-file/compare/1.2.3...1.2.4) (2026-11-16) ### Bug Fixes
effectful-config-file.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: effectful-config-file-version: 0.1.2.4+version: 0.1.3.0 synopsis: Config-file effect for effectful description: Config-file parser and utilities to use in conjunction with effectful category: Configuration@@ -27,6 +27,7 @@ Effectful.Config Effectful.Config.Log Effectful.Config.Secrets+ Effectful.Config.Watch other-modules: Paths_effectful_config_file autogen-modules:@@ -42,6 +43,7 @@ , directory >=1.3 && <2 , effectful >=2.6 && <3 , filepath >=1.5 && <2+ , fsnotify >=0.4 && <1 , hs-opentelemetry-api ==1.* , lens >=5.3 && <6 , megaparsec >=9.7 && <10@@ -55,6 +57,7 @@ type: exitcode-stdio-1.0 main-is: Spec.hs other-modules:+ Effectful.Config.WatchSpec Effectful.ConfigSpec Paths_effectful_config_file autogen-modules:@@ -65,12 +68,14 @@ build-tool-depends: hspec-discover:hspec-discover build-depends:- base >=4.7 && <5+ async+ , base >=4.7 && <5 , bytestring >=0.12 && <1 , directory >=1.3 && <2 , effectful >=2.6 && <3 , effectful-config-file , filepath >=1.5 && <2+ , fsnotify >=0.4 && <1 , hs-opentelemetry-api , hs-opentelemetry-exporter-in-memory , hs-opentelemetry-instrumentation-monad-logger
+ src/Effectful/Config/Watch.hs view
@@ -0,0 +1,65 @@+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TypeApplications #-}+{-# LANGUAGE TypeOperators #-}++-- | Reload a configuration when its file changes.+module Effectful.Config.Watch (+ watchConfig,+) where++import Control.Concurrent (threadDelay)+import Control.Concurrent.MVar (+ modifyMVar,+ newEmptyMVar,+ takeMVar,+ tryPutMVar,+ tryTakeMVar,+ )+import Control.Monad (forever, void)+import Data.Error.Config (ConfigError)+import qualified Data.Text as T (pack)+import Effectful (Eff, IOE, liftIO, withSeqEffToIO, type (:>))+import Effectful.Config (Config, readConfig')+import Effectful.Config.Log (Log, logWarn)+import Effectful.Error.Static (runErrorNoCallStack)+import Effectful.Reader.Static (ask)+import System.Directory (canonicalizePath)+import System.FSNotify (eventPath, watchDir, withManager)+import System.FilePath (takeDirectory, takeFileName, (</>))+import Toml.Schema.FromValue (FromValue)++-- | Watch a configuration file and reload it into the shared configuration+-- on change, calling the callback with the old and new values. Changes are+-- debounced; a file that fails to parse is logged and the old value kept.+-- Blocks forever.+--+-- @since 0.1.3.0+watchConfig+ :: IOE :> es+ => Log :> es+ => Config c :> es+ => FromValue c+ => FilePath+ -> (c -> c -> Eff es ())+ -- ^ called with old and new configuration after each reload+ -> Eff es ()+watchConfig path callback = do+ mvar <- ask+ dir <- liftIO (canonicalizePath (takeDirectory path))+ let file = dir </> takeFileName path+ withSeqEffToIO $ \runInIO -> do+ trigger <- newEmptyMVar+ withManager $ \mgr -> do+ _ <- watchDir mgr dir ((== file) . eventPath) (\_ -> void (tryPutMVar trigger ()))+ forever $ do+ takeMVar trigger+ threadDelay 200000+ void (tryTakeMVar trigger)+ runInIO $+ runErrorNoCallStack @ConfigError (readConfig' file) >>= \r -> case r of+ Left e -> logWarn ("config reload failed: " <> T.pack (show e))+ Right new -> do+ old <- liftIO (modifyMVar mvar (\o -> pure (new, o)))+ callback old new
+ test/Effectful/Config/WatchSpec.hs view
@@ -0,0 +1,62 @@+{-# LANGUAGE OverloadedStrings #-}++module Effectful.Config.WatchSpec (+ spec,+) where++import Control.Concurrent (threadDelay)+import Control.Concurrent.Async (withAsync)+import Control.Concurrent.MVar (+ MVar,+ newEmptyMVar,+ newMVar,+ putMVar,+ readMVar,+ takeMVar,+ tryTakeMVar,+ )+import Effectful (liftIO, runEff)+import Effectful.Config.Log (runLog)+import Effectful.Config.Watch (watchConfig)+import Effectful.Reader.Static (runReader)+import System.FilePath ((</>))+import System.IO.Temp (withSystemTempDirectory)+import System.Timeout (timeout)+import Test.Hspec (Spec, describe, it, shouldBe)+import Toml.Schema (FromValue (..), parseTableFromValue, reqKey)++newtype C = C Int+ deriving (Eq, Show)++instance FromValue C where+ fromValue = parseTableFromValue (C <$> reqKey "n")++spec :: Spec+spec = describe "watchConfig" $ do+ it "reloads on change and calls back" $+ withWatcher $ \file cfg seen -> do+ writeFile file "n = 2\n"+ timeout 5000000 (takeMVar seen) `shouldReturnJust` (C 1, C 2)+ readMVar cfg `shouldReturnC` C 2++ it "keeps old value on invalid TOML" $+ withWatcher $ \file cfg seen -> do+ writeFile file "n = = nope\n"+ threadDelay 1000000+ readMVar cfg `shouldReturnC` C 1+ writeFile file "n = 3\n"+ timeout 5000000 (takeMVar seen) `shouldReturnJust` (C 1, C 3)+ where+ shouldReturnJust act v = act >>= (`shouldBe` Just v)+ shouldReturnC act v = act >>= (`shouldBe` v)++withWatcher :: (FilePath -> MVar C -> MVar (C, C) -> IO ()) -> IO ()+withWatcher body = withSystemTempDirectory "watch" $ \dir -> do+ let file = dir </> "c.toml"+ writeFile file "n = 1\n"+ cfg <- newMVar (C 1)+ seen <- newEmptyMVar+ let run =+ runEff . runLog (\_ _ _ _ -> pure ()) . runReader cfg $+ watchConfig file (\o n -> liftIO (putMVar seen (o, n)))+ withAsync run $ \_ -> threadDelay 500000 >> tryTakeMVar seen >> body file cfg seen