Commit 98bbb7b4 authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Bind periodic actions to the main loop

parent 044ae180
Pipeline #4007 failed with stage
in 28 minutes and 29 seconds
...@@ -29,10 +29,8 @@ import GHC.IO.Exception (IOException) ...@@ -29,10 +29,8 @@ import GHC.IO.Exception (IOException)
import Gargantext.API (startGargantext, Mode(..)) -- , startGargantextMock) import Gargantext.API (startGargantext, Mode(..)) -- , startGargantextMock)
import Gargantext.API.Admin.EnvTypes (DevEnv) import Gargantext.API.Admin.EnvTypes (DevEnv)
import Gargantext.API.Dev (withDevEnv, runCmdDev) import Gargantext.API.Dev (withDevEnv, runCmdDev)
import Gargantext.Database.Prelude (Cmd'', Cmd, execPGSQuery)
import Gargantext.Prelude import Gargantext.Prelude
import Options.Generic import Options.Generic
import System.Cron.Schedule
import System.Exit (exitSuccess) import System.Exit (exitSuccess)
import qualified Paths_gargantext as PG -- cabal magic build module import qualified Paths_gargantext as PG -- cabal magic build module
...@@ -83,22 +81,3 @@ main = do ...@@ -83,22 +81,3 @@ main = do
putStrLn $ "Starting with " <> show myMode <> " mode." putStrLn $ "Starting with " <> show myMode <> " mode."
start start
--------------------------------------------------------------- ---------------------------------------------------------------
putStrLn $ "Starting Schedule Jobs"
withDevEnv (unpack myIniFile') $ \env -> do
tids <- execSchedule $ do
addJob (runCmdDev env refreshIndex) "5 * * * *"
putStrLn ("Refresh Index Cron Job started" <> show tids)
refreshIndex :: Cmd'' DevEnv IOException ()
refreshIndex = do
_ <- execPGSQuery [sql| refresh materialized view context_node_ngrams_view; |] ()
pure ()
...@@ -56,6 +56,7 @@ library ...@@ -56,6 +56,7 @@ library
Gargantext.Core.Text.Corpus.Parsers Gargantext.Core.Text.Corpus.Parsers
Gargantext.Core.Text.Corpus.Parsers.CSV Gargantext.Core.Text.Corpus.Parsers.CSV
Gargantext.Core.Text.Corpus.Parsers.Date.Parsec Gargantext.Core.Text.Corpus.Parsers.Date.Parsec
Gargantext.Core.Text.Corpus.Parsers.JSON
Gargantext.Core.Text.List.Formats.CSV Gargantext.Core.Text.List.Formats.CSV
Gargantext.Core.Text.Metrics Gargantext.Core.Text.Metrics
Gargantext.Core.Text.Metrics.CharByChar Gargantext.Core.Text.Metrics.CharByChar
...@@ -393,6 +394,7 @@ library ...@@ -393,6 +394,7 @@ library
, crawlerISTEX , crawlerISTEX
, crawlerIsidore , crawlerIsidore
, crawlerPubMed , crawlerPubMed
, cron
, cryptohash , cryptohash
, data-time-segment , data-time-segment
, deepseq , deepseq
...@@ -793,7 +795,6 @@ executable gargantext-server ...@@ -793,7 +795,6 @@ executable gargantext-server
base base
, cassava , cassava
, containers , containers
, cron
, extra , extra
, full-text-search , full-text-search
, gargantext , gargantext
......
...@@ -178,6 +178,7 @@ library: ...@@ -178,6 +178,7 @@ library:
- crawlerISTEX - crawlerISTEX
- crawlerIsidore - crawlerIsidore
- crawlerPubMed - crawlerPubMed
- cron
- cryptohash - cryptohash
- data-time-segment - data-time-segment
- deepseq - deepseq
...@@ -333,7 +334,6 @@ executables: ...@@ -333,7 +334,6 @@ executables:
- base - base
- cassava - cassava
- containers - containers
- cron
- full-text-search - full-text-search
- gargantext - gargantext
- gargantext-prelude - gargantext-prelude
......
...@@ -26,16 +26,20 @@ Pouillard (who mainly made it). ...@@ -26,16 +26,20 @@ Pouillard (who mainly made it).
-} -}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-} {-# LANGUAGE TypeOperators #-}
module Gargantext.API module Gargantext.API
where where
import Control.Exception (catch, finally, SomeException) import Control.Concurrent
import Control.Exception (catch, finally, SomeException, displayException)
import Control.Lens import Control.Lens
import Control.Monad.Except import Control.Monad.Except
import Control.Monad.Reader (runReaderT) import Control.Monad.Reader (runReaderT)
import Data.Either import Data.Either
import Data.Foldable (foldlM)
import Data.List (lookup) import Data.List (lookup)
import Data.Text (pack) import Data.Text (pack)
import Data.Text.Encoding (encodeUtf8) import Data.Text.Encoding (encodeUtf8)
...@@ -52,7 +56,7 @@ import Gargantext.API.Ngrams (saveNodeStoryImmediate) ...@@ -52,7 +56,7 @@ import Gargantext.API.Ngrams (saveNodeStoryImmediate)
import Gargantext.API.Routes import Gargantext.API.Routes
import Gargantext.API.Server (server) import Gargantext.API.Server (server)
import Gargantext.Core.NodeStory import Gargantext.Core.NodeStory
import qualified Gargantext.Database.Prelude as DB import Gargantext.Database.GargDB (refreshNgramsMaterializedView)
import Gargantext.Prelude hiding (putStrLn) import Gargantext.Prelude hiding (putStrLn)
import Network.HTTP.Types hiding (Query) import Network.HTTP.Types hiding (Query)
import Network.Wai import Network.Wai
...@@ -62,6 +66,8 @@ import Network.Wai.Middleware.RequestLogger ...@@ -62,6 +66,8 @@ import Network.Wai.Middleware.RequestLogger
import Paths_gargantext (getDataDir) import Paths_gargantext (getDataDir)
import Servant import Servant
import System.FilePath import System.FilePath
import qualified Gargantext.Database.Prelude as DB
import qualified System.Cron.Schedule as Cron
data Mode = Dev | Mock | Prod data Mode = Dev | Mock | Prod
deriving (Show, Read, Generic) deriving (Show, Read, Generic)
...@@ -74,7 +80,8 @@ startGargantext mode port file = do ...@@ -74,7 +80,8 @@ startGargantext mode port file = do
portRouteInfo port portRouteInfo port
app <- makeApp env app <- makeApp env
mid <- makeDevMiddleware mode mid <- makeDevMiddleware mode
run port (mid app) `finally` stopGargantext env periodicActions <- schedulePeriodicActions env
run port (mid app) `finally` stopGargantext env periodicActions
where runDbCheck env = do where runDbCheck env = do
r <- runExceptT (runReaderT DB.dbCheck env) `catch` r <- runExceptT (runReaderT DB.dbCheck env) `catch`
...@@ -91,9 +98,12 @@ portRouteInfo port = do ...@@ -91,9 +98,12 @@ portRouteInfo port = do
putStrLn $ "http://localhost:" <> toUrlPiece port <> "/index.html" putStrLn $ "http://localhost:" <> toUrlPiece port <> "/index.html"
putStrLn $ "http://localhost:" <> toUrlPiece port <> "/swagger-ui" putStrLn $ "http://localhost:" <> toUrlPiece port <> "/swagger-ui"
-- | Stops the gargantext server and cancels all the periodic actions
-- scheduled to run up to that point.
-- TODO clean this Monad condition (more generic) ? -- TODO clean this Monad condition (more generic) ?
stopGargantext :: HasNodeStoryImmediateSaver env => env -> IO () stopGargantext :: HasNodeStoryImmediateSaver env => env -> [ThreadId] -> IO ()
stopGargantext env = do stopGargantext env scheduledPeriodicActions = do
forM_ scheduledPeriodicActions killThread
putStrLn "----- Stopping gargantext -----" putStrLn "----- Stopping gargantext -----"
runReaderT saveNodeStoryImmediate env runReaderT saveNodeStoryImmediate env
...@@ -105,6 +115,27 @@ startGargantextMock port = do ...@@ -105,6 +115,27 @@ startGargantextMock port = do
run port application run port application
-} -}
-- | Schedules all sorts of useful periodic actions to be run while
-- the server is alive accepting requests.
schedulePeriodicActions :: DB.CmdCommon env => env -> IO [ThreadId]
schedulePeriodicActions env =
-- Add your scheduled actions here.
let actions = [
refreshDBViews
]
in foldlM (\ !acc action -> (`mappend` acc) <$> Cron.execSchedule action) [] actions
where
refreshDBViews :: Cron.Schedule ()
refreshDBViews = do
let doRefresh = do
res <- DB.runCmd env refreshNgramsMaterializedView
case res of
Left e -> liftIO $ putStrLn $ pack ("Refreshing Ngrams materialized view failed: " <> displayException e)
Right () -> pure ()
Cron.addJob doRefresh "5 * * * *"
---------------------------------------------------------------------- ----------------------------------------------------------------------
fireWall :: Applicative f => Request -> FireWall -> f Bool fireWall :: Applicative f => Request -> FireWall -> f Bool
......
...@@ -12,17 +12,21 @@ TODO_2: quantitative tests (coded) ...@@ -12,17 +12,21 @@ TODO_2: quantitative tests (coded)
-} -}
{-# LANGUAGE QuasiQuotes #-}
module Gargantext.Database.GargDB module Gargantext.Database.GargDB
where where
import Control.Exception import Control.Exception
import Control.Lens (view) import Control.Lens (view)
import Control.Monad (void)
import Control.Monad.Reader (MonadReader) import Control.Monad.Reader (MonadReader)
import Database.PostgreSQL.Simple.SqlQQ (sql)
import Data.Aeson (ToJSON, toJSON) import Data.Aeson (ToJSON, toJSON)
import Data.Text (Text) import Data.Text (Text)
import Data.Tuple.Extra (both) import Data.Tuple.Extra (both)
import GHC.IO (FilePath) import GHC.IO (FilePath)
import Gargantext.Database.Prelude (HasConfig(..)) import Gargantext.Database.Prelude (HasConfig(..), Cmd, execPGSQuery)
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.Prelude.Config import Gargantext.Prelude.Config
import Gargantext.Prelude.Crypto.Hash import Gargantext.Prelude.Crypto.Hash
...@@ -205,3 +209,9 @@ onDisk_2 action fp1 fp2 = do ...@@ -205,3 +209,9 @@ onDisk_2 action fp1 fp2 = do
| isDoesNotExistError e = return () | isDoesNotExistError e = return ()
| otherwise = throwIO e | otherwise = throwIO e
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- | Refreshes the \"context_node_ngrams_view\" materialized view. This
-- function will be run periodically.
refreshNgramsMaterializedView :: Cmd IOException ()
refreshNgramsMaterializedView =
void $ execPGSQuery [sql| refresh materialized view context_node_ngrams_view; |] ()
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