WIP for term cleanup

This commit is contained in:
Elliot Glaysher 2019-09-12 16:14:57 -07:00
parent 189f03e285
commit 9fcbc864b2
2 changed files with 20 additions and 20 deletions

View File

@ -148,7 +148,7 @@ pier pierPath mPort (serf, log, ss) = do
inst <- io (KingId . UV . fromIntegral <$> randomIO @Word16) inst <- io (KingId . UV . fromIntegral <$> randomIO @Word16)
terminalSystem <- initializeLocalTerminal terminalSystem <- initializeLocalTerminal
serf <- pure serf { sStderr = (tsStderr terminalSystem) } swapMVar (sStderr serf) (tsStderr terminalSystem)
let ship = who (Log.identity log) let ship = who (Log.identity log)

View File

@ -11,13 +11,10 @@ module Vere.Serf ( Serf, sStderr, SerfState(..), doJob
, Config(..), Flags, Flag(..) , Config(..), Flags, Flag(..)
) where ) where
import UrbitPrelude hiding (fail) import UrbitPrelude
import Arvo import Arvo
import Control.Monad.Fail (fail)
import Data.Conduit import Data.Conduit
import Data.Void
import Noun
import System.Process import System.Process
import System.ProgressBar import System.ProgressBar
import Vere.Pier.Types import Vere.Pier.Types
@ -28,7 +25,6 @@ import Data.ByteString.Unsafe (unsafeUseAsCString)
import Foreign.Marshal.Alloc (alloca) import Foreign.Marshal.Alloc (alloca)
import Foreign.Ptr (castPtr) import Foreign.Ptr (castPtr)
import Foreign.Storable (peek, poke) import Foreign.Storable (peek, poke)
import System.Directory (createDirectoryIfMissing)
import System.Exit (ExitCode) import System.Exit (ExitCode)
import qualified Data.ByteString.Unsafe as BS import qualified Data.ByteString.Unsafe as BS
@ -79,10 +75,8 @@ ssLastEv = pred . ssNextEv
data Serf e = Serf data Serf e = Serf
{ sendHandle :: Handle { sendHandle :: Handle
, recvHandle :: Handle , recvHandle :: Handle
, errThread :: Async ()
, process :: ProcessHandle , process :: ProcessHandle
, sState :: MVar SerfState , sStderr :: MVar (Text -> RIO e ())
, sStderr :: Text -> RIO e ()
} }
data ShipId = ShipId Ship Bool data ShipId = ShipId Ship Bool
@ -127,9 +121,9 @@ deriveNoun ''Plea
-- Utils ----------------------------------------------------------------------- -- Utils -----------------------------------------------------------------------
printTank :: HasLogFunc e => (Text -> RIO e ()) -> Word32 -> Tank -> RIO e () printTank :: HasLogFunc e => MVar (Text -> RIO e ()) -> Word32 -> Tank -> RIO e ()
printTank log _pri tank = printTank log _pri tank =
(log . unlines . fmap unTape . wash (WashCfg 0 80)) tank ((printErr log) . unlines . fmap unTape . wash (WashCfg 0 80)) tank
guardExn :: (Exception e, MonadIO m) => Bool -> e -> m () guardExn :: (Exception e, MonadIO m) => Bool -> e -> m ()
guardExn ok = io . unless ok . throwIO guardExn ok = io . unless ok . throwIO
@ -138,6 +132,10 @@ fromRightExn :: (Exception e, MonadIO m) => Either a b -> (a -> e) -> m b
fromRightExn (Left m) exn = throwIO (exn m) fromRightExn (Left m) exn = throwIO (exn m)
fromRightExn (Right x) _ = pure x fromRightExn (Right x) _ = pure x
printErr :: MVar (Text -> RIO e ()) -> Text -> RIO e ()
printErr m txt = do
f <- readMVar m
f txt
-- Process Management ---------------------------------------------------------- -- Process Management ----------------------------------------------------------
@ -153,9 +151,9 @@ startUp conf@(Config pierPath flags) = do
(Just i, Just o, Just e, p) <- createProcess pSpec (Just i, Just o, Just e, p) <- createProcess pSpec
pure (i, o, e, p) pure (i, o, e, p)
ss <- newEmptyMVar stderr <- newMVar putStrLn
et <- async (readStdErr e) async (readStdErr e stderr)
pure (Serf i o et p ss serf) pure (Serf i o p stderr)
where where
diskKey = "" diskKey = ""
config = show (compileFlags flags) config = show (compileFlags flags)
@ -166,11 +164,13 @@ startUp conf@(Config pierPath flags) = do
, std_err = CreatePipe , std_err = CreatePipe
} }
readStdErr :: e. HasLogFunc e => Handle -> RIO e () readStdErr :: e. HasLogFunc e => Handle -> MVar (Text -> RIO e ()) -> RIO e ()
readStdErr h = readStdErr h print =
untilEOFExn $ do untilEOFExn $ do
ln <- io $ IO.hGetLine h raw <- io $ IO.hGetLine h
serf ("[stderr] " <> T.strip (pack ln)) let ln = T.strip (pack raw)
printErr print ln
serf ("[stderr] " <> ln)
where where
eofMsg = "[Serf.readStdErr] serf stderr closed" eofMsg = "[Serf.readStdErr] serf stderr closed"
@ -296,7 +296,7 @@ recvPlea w = do
n <- fromRightExn (cue a) (const $ BadPleaAtom a) n <- fromRightExn (cue a) (const $ BadPleaAtom a)
p <- fromRightExn (fromNounErr n) (\(p,m) -> BadPleaNoun (traceShowId n) p m) p <- fromRightExn (fromNounErr n) (\(p,m) -> BadPleaNoun (traceShowId n) p m)
case p of PStdr e msg -> do (sStderr w) (cordText msg) case p of PStdr e msg -> do printErr (sStderr w) (cordText msg)
recvPlea w recvPlea w
PSlog _ pri t -> do printTank (sStderr w) pri t PSlog _ pri t -> do printTank (sStderr w) pri t
recvPlea w recvPlea w
@ -343,7 +343,7 @@ sendWork w job =
PPlay p -> throwIO (UnexpectedPlay eId p) PPlay p -> throwIO (UnexpectedPlay eId p)
PDone i m o -> produce (SerfState (i+1) m, o) PDone i m o -> produce (SerfState (i+1) m, o)
PWork work -> replace (DoWork work) PWork work -> replace (DoWork work)
PStdr _ cord -> (sStderr w) (cordText cord) >> loop PStdr _ cord -> printErr (sStderr w) (cordText cord) >> loop
PSlog _ pri t -> printTank (sStderr w) pri t >> loop PSlog _ pri t -> printTank (sStderr w) pri t >> loop