Browse Source

Semi-working state

master
Denis Tereshkin 6 years ago
parent
commit
7729e11d64
  1. 10
      atrade-monitor.cabal
  2. 4
      package.yaml
  3. 26
      src/Main.hs
  4. 1
      stack.yaml

10
atrade-monitor.cabal

@ -1,8 +1,10 @@
-- This file has been generated from package.yaml by hpack version 0.28.2. cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.31.2.
-- --
-- see: https://github.com/sol/hpack -- see: https://github.com/sol/hpack
-- --
-- hash: a9a6073782b8b53d54331257ae4de0e9cdd1f47b1c1f1e95cdde7654134f1b22 -- hash: dab67d56afaa7dcc061e0130cc7ad1f8d8d8fc8b43f6d30a8662091a0a8bd9ac
name: atrade-monitor name: atrade-monitor
version: 0.1.0.0 version: 0.1.0.0
@ -14,7 +16,6 @@ copyright: 2019 Denis Tereshkin
license: BSD3 license: BSD3
license-file: LICENSE license-file: LICENSE
build-type: Simple build-type: Simple
cabal-version: >= 1.10
extra-source-files: extra-source-files:
README.md README.md
@ -25,12 +26,15 @@ executable atrade-monitor
hs-source-dirs: hs-source-dirs:
src src
default-extensions: OverloadedStrings default-extensions: OverloadedStrings
ghc-options: -threaded
build-depends: build-depends:
aeson aeson
, base >=4.7 && <5 , base >=4.7 && <5
, process , process
, safe , safe
, safe-exceptions
, text , text
, time
, unagi-chan , unagi-chan
, unordered-containers , unordered-containers
default-language: Haskell2010 default-language: Haskell2010

4
package.yaml

@ -19,6 +19,10 @@ dependencies:
- text - text
- safe - safe
- unagi-chan - unagi-chan
- safe-exceptions
- time
ghc-options: -threaded
default-extensions: default-extensions:
- OverloadedStrings - OverloadedStrings

26
src/Main.hs

@ -3,11 +3,14 @@ module Main where
import Control.Concurrent hiding (newChan, readChan, import Control.Concurrent hiding (newChan, readChan,
writeChan) writeChan)
import Control.Concurrent.Chan.Unagi import Control.Concurrent.Chan.Unagi
import Control.Exception.Safe
import Control.Monad import Control.Monad
import Data.Aeson import Data.Aeson
import qualified Data.HashMap.Strict as HM import qualified Data.HashMap.Strict as HM
import qualified Data.Text as T import qualified Data.Text as T
import qualified Data.Text.IO as TIO import qualified Data.Text.IO as TIO
import Data.Time.Clock
import Data.Time.Format
import Safe import Safe
import System.Environment import System.Environment
import System.IO import System.IO
@ -76,15 +79,21 @@ runWithConfig config = do
(inChan, outChan) <- newChan (inChan, outChan) <- newChan
threadIds <- forM (zip [0..] (cExecutables config)) $ \(i, execcfg) -> do threadIds <- forM (zip [0..] (cExecutables config)) $ \(i, execcfg) -> do
threadDelay (1000000 * i * cDelay config) threadDelay (1000000 * i * cDelay config)
forkIO $ do
putStrLn $ "Creating process: " ++ cExecutablePath execcfg
(_, Just stdOut, Just stdErr, ph) <- createProcess $ (proc (cExecutablePath execcfg) (makeArgs (cGlobalArgs config) (cArgs execcfg))) { std_out = CreatePipe, std_err = CreatePipe } (_, Just stdOut, Just stdErr, ph) <- createProcess $ (proc (cExecutablePath execcfg) (makeArgs (cGlobalArgs config) (cArgs execcfg))) { std_out = CreatePipe, std_err = CreatePipe }
withFile (cLogPath execcfg) WriteMode $ \log -> do withFile (cLogPath execcfg) AppendMode $ \log -> handleIO (exceptionHandler inChan) $ do
stdErrLogThread <- forkIO $ forever $ do stdErrLogThread <- forkIO $ forever $ do
line <- TIO.hGetLine stdErr now <- getCurrentTime
line <- lineWithTime now . T.pack <$> hGetLine stdErr
TIO.hPutStrLn log line TIO.hPutStrLn log line
hFlush log
writeChan inChan line writeChan inChan line
forever $ do forever $ do
line <- TIO.hGetLine stdOut now <- getCurrentTime
line <- lineWithTime now . T.pack <$> hGetLine stdOut
TIO.hPutStrLn log line TIO.hPutStrLn log line
hFlush log
writeChan inChan line writeChan inChan line
globalLogger outChan globalLogger outChan
@ -92,7 +101,14 @@ runWithConfig config = do
where where
makeArgs globalArgs localArgs = fmap makeArg (HM.toList . unArgsMap $ globalArgs) ++ fmap makeArg (HM.toList . unArgsMap $ localArgs) makeArgs globalArgs localArgs = fmap makeArg (HM.toList . unArgsMap $ globalArgs) ++ fmap makeArg (HM.toList . unArgsMap $ localArgs)
makeArg (k, v) = T.unpack $ if v /= "" then "--" <> k <> "=" <> v else "--" <> k makeArg (k, v) = T.unpack $ if v /= "" then "--" <> k <> "=" <> v else "--" <> k
globalLogger chan = withFile (cGlobalLog config) WriteMode $ \log -> do globalLogger chan = withFile (cGlobalLog config) AppendMode $ \log -> forever $ do
line <- readChan chan line <- readChan chan
TIO.hPutStrLn log line TIO.hPutStrLn log line
TIO.putStrLn line putStrLn $ T.unpack line
hFlush log
hFlush stdout
hFlush stderr
exceptionHandler inChan exception = do
writeChan inChan $ "Exception: " `T.append` (T.pack . show $ exception)
lineWithTime time line = ("[" `T.append` (T.pack $ formatTime defaultTimeLocale (iso8601DateFormat (Just "%T.%q")) time) `T.append` "] > " `T.append` line)

1
stack.yaml

@ -40,6 +40,7 @@ packages:
# (e.g., acme-missiles-0.3) # (e.g., acme-missiles-0.3)
extra-deps: extra-deps:
- unagi-chan-0.4.1.0 - unagi-chan-0.4.1.0
- posix-pty-0.2.1.1
# Override default flag values for local packages and extra-deps # Override default flag values for local packages and extra-deps
# flags: {} # flags: {}

Loading…
Cancel
Save