Commit 501b28b1 authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Propagate withLogger refactoring to CLI code

parent c448afb3
...@@ -12,31 +12,39 @@ Portability : POSIX ...@@ -12,31 +12,39 @@ Portability : POSIX
module CLI.Server where module CLI.Server where
import Data.Version (showVersion)
import CLI.Parsers (settings_p) import CLI.Parsers (settings_p)
import CLI.Types import CLI.Types
import CLI.Worker (runAllWorkers) import CLI.Worker (runAllWorkers)
import GHC.IO.Encoding (setLocaleEncoding, utf8) import Control.Monad.IO.Class
import Gargantext.API (startGargantext) import Data.Version (showVersion)
import Gargantext.API.Admin.EnvTypes (Mode(..)) import Gargantext.API.Admin.EnvTypes (Mode(..))
import Gargantext.API (startGargantext)
import Gargantext.Core.Config
import Gargantext.Core.Config.Types (_SettingsFile) import Gargantext.Core.Config.Types (_SettingsFile)
import Gargantext.Core.Config.Utils (readConfig)
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.System.Logging (withLogger, logMsg, LogLevel(..), Logger) import Gargantext.System.Logging
import GHC.IO.Encoding (setLocaleEncoding, utf8)
import Options.Applicative import Options.Applicative
import Paths_gargantext qualified as PG -- cabal magic build module import Paths_gargantext qualified as PG -- cabal magic build module
withServerCLILogger :: ServerArgs
-> (Logger IO -> IO a)
-> IO a
withServerCLILogger ServerArgs{..} f = do
cfg <- liftIO $ readConfig server_toml
withLogger (cfg ^. gc_logging) $ \logger -> f logger
serverCLI :: CLIServer -> IO () serverCLI :: CLIServer -> IO ()
serverCLI (CLIS_start serverArgs) = withLogger () $ \ioLogger -> serverCLI (CLIS_start serverArgs) = withServerCLILogger serverArgs $ \ioLogger ->
startServerCLI ioLogger serverArgs startServerCLI ioLogger serverArgs
serverCLI (CLIS_startAll serverArgs@(ServerArgs { .. })) = withLogger () $ \ioLogger -> do serverCLI (CLIS_startAll serverArgs@(ServerArgs { .. })) = withServerCLILogger serverArgs $ \ioLogger -> do
withAsync (startServerCLI ioLogger serverArgs) $ \aServer -> do withAsync (startServerCLI ioLogger serverArgs) $ \aServer -> do
runAllWorkers ioLogger server_toml runAllWorkers ioLogger server_toml
wait aServer wait aServer
serverCLI (CLIS_version) = withLogger () $ \ioLogger -> do serverCLI (CLIS_version) = withLogger (LogConfig Nothing DEBUG) $ \ioLogger -> do
-- Sets the locale to avoid encoding issues like in #284. -- Sets the locale to avoid encoding issues like in #284.
setLocaleEncoding utf8 setLocaleEncoding utf8
logMsg ioLogger INFO $ "Version: " <> showVersion PG.version logMsg ioLogger INFO $ "Version: " <> showVersion PG.version
...@@ -58,13 +66,13 @@ serverParser = hsubparser ( ...@@ -58,13 +66,13 @@ serverParser = hsubparser (
start_p :: Parser CLIServer start_p :: Parser CLIServer
start_p = fmap CLIS_start $ ServerArgs start_p = fmap CLIS_start $ ServerArgs
<$> mode_p <$> mode_p
<*> port_p <*> port_p
<*> settings_p <*> settings_p
start_all_p :: Parser CLIServer start_all_p :: Parser CLIServer
start_all_p = fmap CLIS_startAll $ ServerArgs start_all_p = fmap CLIS_startAll $ ServerArgs
<$> mode_p <$> mode_p
<*> port_p <*> port_p
<*> settings_p <*> settings_p
...@@ -81,7 +89,7 @@ port_p = option auto ( long "port" ...@@ -81,7 +89,7 @@ port_p = option auto ( long "port"
<> showDefault <> showDefault
<> value 8008 <> value 8008
<> help "Port" ) <> help "Port" )
version_p :: Parser CLIServer version_p :: Parser CLIServer
version_p = pure CLIS_version version_p = pure CLIS_version
......
...@@ -19,7 +19,7 @@ import CLI.Parsers ...@@ -19,7 +19,7 @@ import CLI.Parsers
import Control.Concurrent.Async (forConcurrently_) import Control.Concurrent.Async (forConcurrently_)
import Data.List qualified as List (cycle, concat, take) import Data.List qualified as List (cycle, concat, take)
import Data.Text qualified as T import Data.Text qualified as T
import Gargantext.Core.Config (hasConfig, gc_worker) import Gargantext.Core.Config (hasConfig, gc_worker, gc_logging)
import Gargantext.Core.Config.Types (SettingsFile(..)) import Gargantext.Core.Config.Types (SettingsFile(..))
import Gargantext.Core.Config.Utils (readConfig) import Gargantext.Core.Config.Utils (readConfig)
import Gargantext.Core.Config.Worker (WorkerDefinition(..), WorkerSettings(..), findDefinitionByName) import Gargantext.Core.Config.Worker (WorkerDefinition(..), WorkerSettings(..), findDefinitionByName)
...@@ -67,8 +67,9 @@ workerCLI (CLIW_run (WorkerArgs { .. })) = do ...@@ -67,8 +67,9 @@ workerCLI (CLIW_run (WorkerArgs { .. })) = do
withPGMQWorkerCtrlC env wd $ \a _state -> do withPGMQWorkerCtrlC env wd $ \a _state -> do
-- _ <- runReaderT (sendJob Ping) env -- _ <- runReaderT (sendJob Ping) env
wait a wait a
workerCLI (CLIW_runAll (WorkerAllArgs { .. })) = withLogger () $ \ioLogger -> do workerCLI (CLIW_runAll (WorkerAllArgs { .. })) = withWorkerEnv worker_toml $ \env -> do
runAllWorkers ioLogger worker_toml let log_cfg = env ^. hasConfig . gc_logging
withLogger log_cfg $ \ioLogger -> runAllWorkers ioLogger worker_toml
workerCLI (CLIW_stats (WorkerStatsArgs { .. })) = do workerCLI (CLIW_stats (WorkerStatsArgs { .. })) = do
putStrLn ("worker toml: " <> _SettingsFile ws_toml) putStrLn ("worker toml: " <> _SettingsFile ws_toml)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment