[NGRAMS-REPO] Save the repo regularly (using json-state)

parent c77dd73e
...@@ -111,6 +111,7 @@ library: ...@@ -111,6 +111,7 @@ library:
- ini - ini
- insert-ordered-containers - insert-ordered-containers
- jose-jwt - jose-jwt
- json-state
# - kmeans-vector # - kmeans-vector
- KMP - KMP
- lens - lens
...@@ -161,6 +162,7 @@ library: ...@@ -161,6 +162,7 @@ library:
- text-metrics - text-metrics
- time - time
- time-locale-compat - time-locale-compat
- time-units
- timezone-series - timezone-series
- transformers - transformers
- transformers-base - transformers-base
......
...@@ -73,7 +73,7 @@ import Gargantext.Prelude ...@@ -73,7 +73,7 @@ import Gargantext.Prelude
import Gargantext.API.FrontEnd (FrontEndAPI, frontEndServer) import Gargantext.API.FrontEnd (FrontEndAPI, frontEndServer)
import Gargantext.API.Auth (AuthRequest, AuthResponse, auth) import Gargantext.API.Auth (AuthRequest, AuthResponse, auth)
import Gargantext.API.Ngrams (HasRepoVar(..)) import Gargantext.API.Ngrams (HasRepoVar(..), HasRepoSaver(..), saveRepo)
import Gargantext.API.Node ( GargServer import Gargantext.API.Node ( GargServer
, Roots , roots , Roots , roots
, NodeAPI , nodeAPI , NodeAPI , nodeAPI
...@@ -370,10 +370,10 @@ portRouteInfo port = do ...@@ -370,10 +370,10 @@ portRouteInfo port = do
T.putStrLn $ "http://localhost:" <> toUrlPiece port <> "/index.html" T.putStrLn $ "http://localhost:" <> toUrlPiece port <> "/index.html"
T.putStrLn $ "http://localhost:" <> toUrlPiece port <> "/swagger-ui" T.putStrLn $ "http://localhost:" <> toUrlPiece port <> "/swagger-ui"
stopGargantext :: HasRepoVar env => env -> IO () stopGargantext :: HasRepoSaver env => env -> IO ()
stopGargantext env = do stopGargantext env = do
T.putStrLn "----- Stopping gargantext -----" T.putStrLn "----- Stopping gargantext -----"
cleanEnv env runReaderT saveRepo env
-- | startGargantext takes as parameters port number and Ini file. -- | startGargantext takes as parameters port number and Ini file.
startGargantext :: PortNumber -> FilePath -> IO () startGargantext :: PortNumber -> FilePath -> IO ()
......
...@@ -604,14 +604,25 @@ class HasRepoVar env where ...@@ -604,14 +604,25 @@ class HasRepoVar env where
instance HasRepoVar (MVar NgramsRepo) where instance HasRepoVar (MVar NgramsRepo) where
repoVar = identity repoVar = identity
class HasRepoSaver env where
repoSaver :: Getter env (IO ())
instance HasRepoSaver (IO ()) where
repoSaver = identity
type RepoCmdM env err m = type RepoCmdM env err m =
( MonadReader env m ( MonadReader env m
, MonadError err m , MonadError err m
, MonadIO m , MonadIO m
, HasRepoVar env , HasRepoVar env
, HasRepoSaver env
) )
------------------------------------------------------------------------ ------------------------------------------------------------------------
saveRepo :: ( MonadReader env m, MonadIO m, HasRepoSaver env )
=> m ()
saveRepo = liftIO =<< view repoSaver
listTypeConflictResolution :: ListType -> ListType -> ListType listTypeConflictResolution :: ListType -> ListType -> ListType
listTypeConflictResolution _ _ = undefined -- TODO Use Map User ListType listTypeConflictResolution _ _ = undefined -- TODO Use Map User ListType
...@@ -653,6 +664,7 @@ putListNgrams listId ngramsType nes = do ...@@ -653,6 +664,7 @@ putListNgrams listId ngramsType nes = do
var <- view repoVar var <- view repoVar
liftIO $ modifyMVar_ var $ liftIO $ modifyMVar_ var $
pure . (r_state . at ngramsType %~ (Just . (at listId %~ insertNewOnly m) . something)) pure . (r_state . at ngramsType %~ (Just . (at listId %~ insertNewOnly m) . something))
saveRepo
where where
m = Map.fromList $ (\n -> (n ^. ne_ngrams, n)) <$> nes m = Map.fromList $ (\n -> (n ^. ne_ngrams, n)) <$> nes
...@@ -687,6 +699,7 @@ tableNgramsPatch _corpusId maybeTabType listId (Versioned p_version p_table) = d ...@@ -687,6 +699,7 @@ tableNgramsPatch _corpusId maybeTabType listId (Versioned p_version p_table) = d
in in
pure (r', (p'_applicable, Versioned (r' ^. r_version) q'_table)) pure (r', (p'_applicable, Versioned (r' ^. r_version) q'_table))
saveRepo
assertValid p'_applicable assertValid p'_applicable
pure vq' pure vq'
......
...@@ -46,7 +46,7 @@ import Data.Time (UTCTime) ...@@ -46,7 +46,7 @@ import Data.Time (UTCTime)
import GHC.Generics (Generic) import GHC.Generics (Generic)
import Servant import Servant
import Gargantext.API.Ngrams (TabType(..), TableNgramsApi, TableNgramsApiGet, tableNgramsPatch, getTableNgrams, HasRepoVar(..)) import Gargantext.API.Ngrams (TabType(..), TableNgramsApi, TableNgramsApiGet, tableNgramsPatch, getTableNgrams, HasRepoVar, HasRepoSaver)
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.Database.Types.Node import Gargantext.Database.Types.Node
import Gargantext.Database.Utils -- (Cmd, CmdM) import Gargantext.Database.Utils -- (Cmd, CmdM)
......
...@@ -38,8 +38,10 @@ import Network.HTTP.Client.TLS (newTlsManager) ...@@ -38,8 +38,10 @@ import Network.HTTP.Client.TLS (newTlsManager)
import Data.Aeson import Data.Aeson
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
import Data.Either (either) import Data.Either (either)
import Data.JsonState
import Data.Text import Data.Text
import Data.Text.Encoding (encodeUtf8) import Data.Text.Encoding (encodeUtf8)
import Data.Time.Units
import Data.ByteString.Lazy.Internal import Data.ByteString.Lazy.Internal
import Servant import Servant
...@@ -52,10 +54,11 @@ import qualified Jose.Jwa as Jose ...@@ -52,10 +54,11 @@ import qualified Jose.Jwa as Jose
import Control.Concurrent import Control.Concurrent
import Control.Exception (finally) import Control.Exception (finally)
import Control.Monad.Logger import Control.Monad.Logger
import Control.Monad.Reader
import Control.Lens import Control.Lens
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.Database.Utils (databaseParameters, HasConnection(..), Cmd', runCmd) import Gargantext.Database.Utils (databaseParameters, HasConnection(..), Cmd', runCmd)
import Gargantext.API.Ngrams (NgramsRepo, HasRepoVar(..), initMockRepo, r_version) import Gargantext.API.Ngrams (NgramsRepo, HasRepoVar(..), HasRepoSaver(..), initMockRepo, r_version, saveRepo)
import Gargantext.API.Orchestrator.Types import Gargantext.API.Orchestrator.Types
type PortNumber = Int type PortNumber = Int
...@@ -132,13 +135,14 @@ optSetting name d = do ...@@ -132,13 +135,14 @@ optSetting name d = do
data FireWall = FireWall { unFireWall :: Bool } data FireWall = FireWall { unFireWall :: Bool }
data Env = Env data Env = Env
{ _env_settings :: !Settings { _env_settings :: !Settings
, _env_logger :: !LoggerSet , _env_logger :: !LoggerSet
, _env_conn :: !Connection , _env_conn :: !Connection
, _env_repo_var :: !(MVar NgramsRepo) , _env_repo_var :: !(MVar NgramsRepo)
, _env_manager :: !Manager , _env_repo_saver :: !(IO ())
, _env_self_url :: !BaseUrl , _env_manager :: !Manager
, _env_scrapers :: !ScrapersEnv , _env_self_url :: !BaseUrl
, _env_scrapers :: !ScrapersEnv
} }
deriving (Generic) deriving (Generic)
...@@ -150,6 +154,9 @@ instance HasConnection Env where ...@@ -150,6 +154,9 @@ instance HasConnection Env where
instance HasRepoVar Env where instance HasRepoVar Env where
repoVar = env_repo_var repoVar = env_repo_var
instance HasRepoSaver Env where
repoSaver = env_repo_saver
data MockEnv = MockEnv data MockEnv = MockEnv
{ _menv_firewall :: !FireWall { _menv_firewall :: !FireWall
} }
...@@ -174,6 +181,11 @@ readRepo = do ...@@ -174,6 +181,11 @@ readRepo = do
else else
pure initMockRepo pure initMockRepo
mkRepoSaver :: MVar NgramsRepo -> IO (IO ())
mkRepoSaver repo_var = do
saveAction <- mkSaveState (10 :: Second) repoSnapshot
pure $ readMVar repo_var >>= saveAction
newEnv :: PortNumber -> FilePath -> IO Env newEnv :: PortNumber -> FilePath -> IO Env
newEnv port file = do newEnv port file = do
manager <- newTlsManager manager <- newTlsManager
...@@ -184,21 +196,24 @@ newEnv port file = do ...@@ -184,21 +196,24 @@ newEnv port file = do
param <- databaseParameters file param <- databaseParameters file
conn <- connect param conn <- connect param
repo_var <- readRepo repo_var <- readRepo
repo_saver <- mkRepoSaver repo_var
scrapers_env <- newJobEnv defaultSettings manager scrapers_env <- newJobEnv defaultSettings manager
logger <- newStderrLoggerSet defaultBufSize logger <- newStderrLoggerSet defaultBufSize
pure $ Env pure $ Env
{ _env_settings = settings { _env_settings = settings
, _env_logger = logger , _env_logger = logger
, _env_conn = conn , _env_conn = conn
, _env_repo_var = repo_var , _env_repo_var = repo_var
, _env_manager = manager , _env_repo_saver = repo_saver
, _env_scrapers = scrapers_env , _env_manager = manager
, _env_self_url = self_url , _env_scrapers = scrapers_env
, _env_self_url = self_url
} }
data DevEnv = DevEnv data DevEnv = DevEnv
{ _dev_env_conn :: !Connection { _dev_env_conn :: !Connection
, _dev_env_repo_var :: !(MVar NgramsRepo) , _dev_env_repo_var :: !(MVar NgramsRepo)
, _dev_env_repo_saver :: !(IO ())
} }
makeLenses ''DevEnv makeLenses ''DevEnv
...@@ -209,34 +224,34 @@ instance HasConnection DevEnv where ...@@ -209,34 +224,34 @@ instance HasConnection DevEnv where
instance HasRepoVar DevEnv where instance HasRepoVar DevEnv where
repoVar = dev_env_repo_var repoVar = dev_env_repo_var
instance HasRepoSaver DevEnv where
repoSaver = dev_env_repo_saver
newDevEnvWith :: FilePath -> IO DevEnv newDevEnvWith :: FilePath -> IO DevEnv
newDevEnvWith file = do newDevEnvWith file = do
param <- databaseParameters file param <- databaseParameters file
conn <- connect param conn <- connect param
repo_var <- newMVar initMockRepo repo_var <- newMVar initMockRepo
repo_saver <- mkRepoSaver repo_var
pure $ DevEnv pure $ DevEnv
{ _dev_env_conn = conn { _dev_env_conn = conn
, _dev_env_repo_var = repo_var , _dev_env_repo_var = repo_var
, _dev_env_repo_saver = repo_saver
} }
newDevEnv :: IO DevEnv newDevEnv :: IO DevEnv
newDevEnv = newDevEnvWith "gargantext.ini" newDevEnv = newDevEnvWith "gargantext.ini"
-- So far `cleanEnv` is just writing the repo file.
-- Therefor it is called in `runCmdDev*` for convenience.
cleanEnv :: HasRepoVar env => env -> IO ()
cleanEnv env = encodeFile repoSnapshot =<< readMVar (env ^. repoVar)
-- Use only for dev -- Use only for dev
-- In particular this writes the repo file after running -- In particular this writes the repo file after running
-- the command. -- the command.
-- This function is constrained to the DevEnv rather than -- This function is constrained to the DevEnv rather than
-- using HasConnection and HasRepoVar. -- using HasConnection and HasRepoVar.
-- This is to avoid calling cleanEnv unintentionally on a prod env.
runCmdDev :: Show err => DevEnv -> Cmd' DevEnv err a -> IO a runCmdDev :: Show err => DevEnv -> Cmd' DevEnv err a -> IO a
runCmdDev env f = do runCmdDev env f =
(either (fail . show) pure =<< runCmd env f) (either (fail . show) pure =<< runCmd env f)
`finally` cleanEnv env `finally`
runReaderT saveRepo env
-- Use only for dev -- Use only for dev
runCmdDevNoErr :: DevEnv -> Cmd' DevEnv () a -> IO a runCmdDevNoErr :: DevEnv -> Cmd' DevEnv () a -> IO a
......
...@@ -12,6 +12,8 @@ packages: ...@@ -12,6 +12,8 @@ packages:
allow-newer: true allow-newer: true
extra-deps: extra-deps:
- json-state-0.1.0.1
- time-units-1.0.0
- git: https://github.com/delanoe/data-time-segment.git - git: https://github.com/delanoe/data-time-segment.git
commit: 4e3d57d80e9dfe6624c8eeaa8595fc8fe64d8723 commit: 4e3d57d80e9dfe6624c8eeaa8595fc8fe64d8723
- git: https://gitlab.iscpif.fr/gargantext/hlcm.git - git: https://gitlab.iscpif.fr/gargantext/hlcm.git
......
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