From 1cf0c6d4ea7078920034df8591d803c07ce1d17a Mon Sep 17 00:00:00 2001 From: Denis Tereshkin Date: Tue, 4 Jan 2022 21:56:01 +0700 Subject: [PATCH] junction: startRobot refactoring --- src/ATrade/Driver/Junction.hs | 15 ++++++--- src/ATrade/Driver/Junction/JunctionMonad.hs | 34 +++++++++++++-------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/ATrade/Driver/Junction.hs b/src/ATrade/Driver/Junction.hs index f1ac727..f1eb142 100644 --- a/src/ATrade/Driver/Junction.hs +++ b/src/ATrade/Driver/Junction.hs @@ -146,19 +146,24 @@ junctionMain descriptors = do peBroker = bro, peRobots = robotsMap, peRemoteControlSocket = rcSocket, - peLogAction = junctionLogAction + peLogAction = junctionLogAction, + peProgramConfiguration = cfg, + peBarsMap = barsMap, + peTickerInfoMap = tickerInfoMap, + peBrokerService = broService, + peDescriptors = descriptors } withJunction env $ do - startRobots (hoistLogAction liftIO globalLogger) cfg barsMap tickerInfoMap broService + startRobots (hoistLogAction liftIO globalLogger) cfg forever $ do notifications <- liftIO $ getNotifications broService forM_ notifications (liftIO . handleBrokerNotification robotsMap ordersMap handledNotifications globalLogger) saveRobots handleRemoteControl 1000000 where - startRobots :: LogAction IO Message -> ProgramConfiguration -> IORef Bars -> IORef TickerInfoMap -> BrokerService -> JunctionM () - startRobots gLogger cfg barsMap tickerInfoMap broService = forM_ (instances cfg) $ \inst -> do - startRobot gLogger cfg barsMap tickerInfoMap broService descriptors inst + startRobots :: LogAction IO Message -> ProgramConfiguration -> JunctionM () + startRobots gLogger cfg = forM_ (instances cfg) $ \inst -> do + startRobot gLogger inst withJunction :: JunctionEnv -> JunctionM () -> IO () withJunction env = (`runReaderT` env) . unJunctionM diff --git a/src/ATrade/Driver/Junction/JunctionMonad.hs b/src/ATrade/Driver/Junction/JunctionMonad.hs index ca9886d..173c142 100644 --- a/src/ATrade/Driver/Junction/JunctionMonad.hs +++ b/src/ATrade/Driver/Junction/JunctionMonad.hs @@ -76,7 +76,8 @@ import Data.Time.Clock.POSIX (getPOSIXTime) import Database.Redis (Connection, get, mset, runRedis) import Dhall (auto, input) -import Prelude hiding (readFile) +import Prelude hiding (log, + readFile) import System.IO (BufferMode (LineBuffering), IOMode (AppendMode), hSetBuffering, @@ -86,13 +87,18 @@ import System.ZMQ4 (Rep, Socket) data JunctionEnv = JunctionEnv { - peRedisSocket :: Connection, - peConfigPath :: FilePath, - peQuoteThread :: QuoteThreadHandle, - peBroker :: BrokerClientHandle, - peRobots :: IORef (M.Map T.Text RobotDriverHandle), - peRemoteControlSocket :: Socket Rep, - peLogAction :: LogAction JunctionM Message + peRedisSocket :: Connection, + peConfigPath :: FilePath, + peQuoteThread :: QuoteThreadHandle, + peBroker :: BrokerClientHandle, + peRobots :: IORef (M.Map T.Text RobotDriverHandle), + peRemoteControlSocket :: Socket Rep, + peLogAction :: LogAction JunctionM Message, + peProgramConfiguration :: ProgramConfiguration, + peBarsMap :: IORef Bars, + peTickerInfoMap :: IORef TickerInfoMap, + peBrokerService :: BrokerService, + peDescriptors :: M.Map T.Text StrategyDescriptorE } newtype JunctionM a = JunctionM { unJunctionM :: ReaderT JunctionEnv IO a } @@ -145,11 +151,13 @@ instance QuoteStream JunctionM where qt <- asks peQuoteThread QT.removeSubscription qt subId -startRobot :: LogAction IO Message -> ProgramConfiguration -> IORef Bars -> IORef TickerInfoMap -> - BrokerService -> M.Map T.Text StrategyDescriptorE -> StrategyInstanceDescriptor -> JunctionM () -startRobot ioLogger cfg barsMap tickerInfoMap broService descriptors inst = do - logger <- asks peLogAction - let log = logWith logger +startRobot :: LogAction IO Message -> StrategyInstanceDescriptor -> JunctionM () +startRobot ioLogger inst = do + descriptors <- asks peDescriptors + cfg <- asks peProgramConfiguration + barsMap <- asks peBarsMap + tickerInfoMap <- asks peTickerInfoMap + broService <- asks peBrokerService now <- liftIO getCurrentTime let lLogger = hoistLogAction liftIO ioLogger logWith lLogger Info "Junction" $ "Starting strategy: " <> strategyBaseName inst