[NGRAMS-REPO] add file locking

parent 4ddc86ca
...@@ -99,6 +99,7 @@ library: ...@@ -99,6 +99,7 @@ library:
- fullstop - fullstop
- fclabels - fclabels
- fast-logger - fast-logger
- filelock
- full-text-search - full-text-search
- http-client - http-client
- http-client-tls - http-client-tls
......
...@@ -82,6 +82,7 @@ import Gargantext.Prelude ...@@ -82,6 +82,7 @@ import Gargantext.Prelude
-- import Gargantext.Core.Types (ListTypeId, listTypeId) -- import Gargantext.Core.Types (ListTypeId, listTypeId)
import Gargantext.Core.Types (ListType(..), NodeId, ListId, CorpusId, Limit, Offset) import Gargantext.Core.Types (ListType(..), NodeId, ListId, CorpusId, Limit, Offset)
import Servant hiding (Patch) import Servant hiding (Patch)
import System.FileLock (FileLock)
import Test.QuickCheck (elements) import Test.QuickCheck (elements)
import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary) import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
...@@ -604,6 +605,7 @@ initMockRepo = Repo 1 s [] ...@@ -604,6 +605,7 @@ initMockRepo = Repo 1 s []
data RepoEnv = RepoEnv data RepoEnv = RepoEnv
{ _renv_var :: !(MVar NgramsRepo) { _renv_var :: !(MVar NgramsRepo)
, _renv_saver :: !(IO ()) , _renv_saver :: !(IO ())
, _renv_lock :: !FileLock
} }
deriving (Generic) deriving (Generic)
......
...@@ -32,6 +32,7 @@ import Prelude (Bounded(), fail) ...@@ -32,6 +32,7 @@ import Prelude (Bounded(), fail)
import System.Environment (lookupEnv) import System.Environment (lookupEnv)
import System.IO (FilePath, hClose) import System.IO (FilePath, hClose)
import System.IO.Temp (withTempFile) import System.IO.Temp (withTempFile)
import System.FileLock (tryLockFile, unlockFile, SharedExclusive(Exclusive))
import Database.PostgreSQL.Simple (Connection, connect) import Database.PostgreSQL.Simple (Connection, connect)
import Network.HTTP.Client (Manager) import Network.HTTP.Client (Manager)
import Network.HTTP.Client.TLS (newTlsManager) import Network.HTTP.Client.TLS (newTlsManager)
...@@ -60,7 +61,7 @@ import Control.Monad.Reader ...@@ -60,7 +61,7 @@ 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(..), HasRepoSaver(..), HasRepo(..), RepoEnv(..), r_version, saveRepo, initRepo) import Gargantext.API.Ngrams (NgramsRepo, HasRepoVar(..), HasRepoSaver(..), HasRepo(..), RepoEnv(..), r_version, saveRepo, initRepo, renv_lock)
import Gargantext.API.Orchestrator.Types import Gargantext.API.Orchestrator.Types
type PortNumber = Int type PortNumber = Int
...@@ -197,6 +198,9 @@ readRepoEnv = do ...@@ -197,6 +198,9 @@ readRepoEnv = do
then (>0) <$> getFileSize repoSnapshot then (>0) <$> getFileSize repoSnapshot
else pure False else pure False
mlock <- tryLockFile repoSnapshot Exclusive
lock <- maybe (panic "Repo file already locked") pure mlock
mvar <- newMVar =<< mvar <- newMVar =<<
if repoExists if repoExists
then do then do
...@@ -209,7 +213,7 @@ readRepoEnv = do ...@@ -209,7 +213,7 @@ readRepoEnv = do
pure initRepo pure initRepo
saver <- mkRepoSaver mvar saver <- mkRepoSaver mvar
pure $ RepoEnv { _renv_var = mvar, _renv_saver = saver } pure $ RepoEnv { _renv_var = mvar, _renv_saver = saver, _renv_lock = lock }
newEnv :: PortNumber -> FilePath -> IO Env newEnv :: PortNumber -> FilePath -> IO Env
newEnv port file = do newEnv port file = do
...@@ -264,9 +268,14 @@ newDevEnvWith file = do ...@@ -264,9 +268,14 @@ newDevEnvWith file = do
, _dev_env_repo = repo , _dev_env_repo = repo
} }
withDevEnv :: (DevEnv -> IO a) -> IO a
withDevEnv k = do
env <- newDevEnv
k env `finally` unlockFile (env ^. repoEnv . renv_lock)
-- | Run Cmd Sugar for the Repl (GHCI) -- | Run Cmd Sugar for the Repl (GHCI)
runCmdRepl :: Show err => Cmd' DevEnv err a -> IO a runCmdRepl :: Show err => Cmd' DevEnv err a -> IO a
runCmdRepl f = newDevEnv >>= \env -> runCmdDev env f runCmdRepl f = withDevEnv $ \env -> runCmdDev env f
newDevEnv :: IO DevEnv newDevEnv :: IO DevEnv
newDevEnv = newDevEnvWith "gargantext.ini" newDevEnv = newDevEnvWith "gargantext.ini"
......
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