diff options
| author | tv <tv@krebsco.de> | 2026-02-09 03:50:35 +0100 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-02-10 06:21:34 +0100 |
| commit | 726f659e41382227296bb257e54693c5bc307d40 (patch) | |
| tree | 8e6d1a92be27bd2a44e9e37eb0fa722a7b618b85 /Process.hs | |
| parent | 6edeb752c80bb4a9cd7e27672f773fe3d66b2039 (diff) | |
Diffstat (limited to 'Process.hs')
| -rw-r--r-- | Process.hs | 52 |
1 files changed, 0 insertions, 52 deletions
diff --git a/Process.hs b/Process.hs deleted file mode 100644 index e4f55d1..0000000 --- a/Process.hs +++ /dev/null @@ -1,52 +0,0 @@ -module Process (mkProcess) where - -import Control.Concurrent.Async (async, cancel) -import Control.Concurrent.Chan.Unagi (newChan, readChan, writeChan) -import Control.Exception (IOException) -import Control.Monad (forever, unless) -import Data.Bifunctor (bimap) -import Data.ByteString (ByteString) -import Data.ByteString.Char8 qualified as BS8 -import System.IO (hClose, hIsEOF) -import System.Process qualified as P - -mkProcess :: FilePath -> [String] -> IO (IO (Either IOException ByteString), IO ()) -mkProcess prog args = do - (Just hin, Just hout, Just herr, ph) <- - P.createProcess (P.proc prog args) - { P.std_in = P.CreatePipe - , P.std_out = P.CreatePipe - , P.std_err = P.CreatePipe - } - - (putOutput, takeOutput) <- bimap writeChan readChan <$> newChan - - let reader h = forever do - eof <- hIsEOF h - unless eof do - BS8.hGetLine h >>= putOutput -- TODO mark as stdout/stderr - - outReader <- async (reader hout) - errReader <- async (reader herr) - - let - -- normal termination path: wait for process, then kill readers - waitProcessAndReaders = do - _ <- P.waitForProcess ph - shutdown - --cancel outReader - --cancel errReader - - -- explicit shutdown: kill everything - shutdown = do - P.terminateProcess ph - cancel outReader - cancel errReader - hClose hin - hClose hout - hClose herr - - -- optionally run waitProcessAndReaders in background: - _ <- async waitProcessAndReaders - - pure (Right <$> takeOutput, shutdown) |
