Commit 5c4f3ebd authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

reverse order of logs in jobLog (fixes #192)

parent df6f1dde
Pipeline #3876 failed with stage
in 28 minutes and 57 seconds
...@@ -18,11 +18,11 @@ module Gargantext.API.Admin.EnvTypes ( ...@@ -18,11 +18,11 @@ module Gargantext.API.Admin.EnvTypes (
, ConcreteJobHandle -- opaque , ConcreteJobHandle -- opaque
) where ) where
import Control.Lens hiding ((:>)) import Control.Lens hiding ((:<))
import Control.Monad.Except import Control.Monad.Except
import Control.Monad.Reader import Control.Monad.Reader
import Data.Pool (Pool) import Data.Pool (Pool)
import Data.Sequence (Seq, ViewR(..), viewr) import Data.Sequence (Seq, ViewL(..), viewl)
import Database.PostgreSQL.Simple (Connection) import Database.PostgreSQL.Simple (Connection)
import GHC.Generics (Generic) import GHC.Generics (Generic)
import Network.HTTP.Client (Manager) import Network.HTTP.Client (Manager)
...@@ -159,12 +159,12 @@ instance Jobs.MonadJobStatus (GargM Env err) where ...@@ -159,12 +159,12 @@ instance Jobs.MonadJobStatus (GargM Env err) where
Just j -> case jTask j of Just j -> case jTask j of
QueuedJ _ -> pure noJobLog QueuedJ _ -> pure noJobLog
RunningJ rj -> liftIO (rjGetLog rj) <&> RunningJ rj -> liftIO (rjGetLog rj) <&>
\lgs -> case viewr lgs of \lgs -> case viewl lgs of
EmptyR -> noJobLog EmptyL -> noJobLog
_ :> l -> l l :< _ -> l
DoneJ lgs _ -> pure $ case viewr lgs of DoneJ lgs _ -> pure $ case viewl lgs of
EmptyR -> noJobLog EmptyL -> noJobLog
_ :> l -> l l :< _ -> l
withTracer extraLogger (JobHandle jId logger) n = n (JobHandle jId (\w -> logger w >> liftIO (extraLogger w))) withTracer extraLogger (JobHandle jId logger) n = n (JobHandle jId (\w -> logger w >> liftIO (extraLogger w)))
......
...@@ -20,6 +20,7 @@ module Gargantext.API.Node.Corpus.New ...@@ -20,6 +20,7 @@ module Gargantext.API.Node.Corpus.New
import Conduit import Conduit
import Control.Lens hiding (elements, Empty) import Control.Lens hiding (elements, Empty)
import Control.Monad
import Data.Aeson import Data.Aeson
import Data.Aeson.TH (deriveJSON) import Data.Aeson.TH (deriveJSON)
import qualified Data.ByteString.Base64 as BSB64 import qualified Data.ByteString.Base64 as BSB64
...@@ -193,7 +194,6 @@ addToCorpusWithQuery user cid (WithQuery { _wq_query = q ...@@ -193,7 +194,6 @@ addToCorpusWithQuery user cid (WithQuery { _wq_query = q
, _wq_lang = l , _wq_lang = l
, _wq_flowListWith = flw }) maybeLimit jobHandle = do , _wq_flowListWith = flw }) maybeLimit jobHandle = do
-- TODO ... -- TODO ...
markStarted 3 jobHandle
-- printDebug "[addToCorpusWithQuery] (cid, dbs)" (cid, dbs) -- printDebug "[addToCorpusWithQuery] (cid, dbs)" (cid, dbs)
-- printDebug "[addToCorpusWithQuery] datafield" datafield -- printDebug "[addToCorpusWithQuery] datafield" datafield
-- printDebug "[addToCorpusWithQuery] flowListWith" flw -- printDebug "[addToCorpusWithQuery] flowListWith" flw
...@@ -202,49 +202,40 @@ addToCorpusWithQuery user cid (WithQuery { _wq_query = q ...@@ -202,49 +202,40 @@ addToCorpusWithQuery user cid (WithQuery { _wq_query = q
Just Web -> do Just Web -> do
-- printDebug "[addToCorpusWithQuery] processing web request" datafield -- printDebug "[addToCorpusWithQuery] processing web request" datafield
markStarted 1 jobHandle
_ <- triggerSearxSearch user cid q l jobHandle _ <- triggerSearxSearch user cid q l jobHandle
markComplete jobHandle markComplete jobHandle
_ -> do _ -> do
markStarted 3 jobHandle
-- TODO add cid -- TODO add cid
-- TODO if cid is folder -> create Corpus -- TODO if cid is folder -> create Corpus
-- if cid is corpus -> add to corpus -- if cid is corpus -> add to corpus
-- if cid is root -> create corpus in Private -- if cid is root -> create corpus in Private
-- printDebug "[G.A.N.C.New] getDataText with query" q -- printDebug "[G.A.N.C.New] getDataText with query" q
databaseOrigin <- database2origin dbs db <- database2origin dbs
eTxts <- mapM (\db -> getDataText db (Multi l) q maybeLimit) [databaseOrigin] eTxt <- getDataText db (Multi l) q maybeLimit
let lTxts = lefts eTxts
-- printDebug "[G.A.N.C.New] lTxts" lTxts -- printDebug "[G.A.N.C.New] lTxts" lTxts
case lTxts of case eTxt of
[] -> do Right txt -> do
let txts = rights eTxts
-- TODO Sum lenghts of each txt elements -- TODO Sum lenghts of each txt elements
-- NOTE(adinapoli) Some other weird arithmetic to have the markProgress 1 jobHandle
-- following 'JobLog' as output:
-- JobLog
-- { _scst_succeeded = Just 2
-- , _scst_failed = Just 0
-- , _scst_remaining = Just $ 1 + length txts
-- , _scst_events = Just []
-- }
markStarted (3 + length txts) jobHandle
markProgress 2 jobHandle
_cids <- mapM (\txt -> do void $ flowDataText user txt (Multi l) cid (Just flw) jobHandle
flowDataText user txt (Multi l) cid (Just flw) jobHandle) txts
-- printDebug "corpus id" cids -- printDebug "corpus id" cids
-- printDebug "sending email" ("xxxxxxxxxxxxxxxxxxxxx" :: Text) -- printDebug "sending email" ("xxxxxxxxxxxxxxxxxxxxx" :: Text)
sendMail user sendMail user
-- TODO ... -- TODO ...
markComplete jobHandle markComplete jobHandle
(err:_) -> do Left err -> do
-- printDebug "Error: " err -- printDebug "Error: " err
markFailure 1 (Just $ T.pack (show err)) jobHandle markFailed (Just $ T.pack (show err)) jobHandle
type AddWithForm = Summary "Add with FormUrlEncoded to corpus endpoint" type AddWithForm = Summary "Add with FormUrlEncoded to corpus endpoint"
:> "corpus" :> "corpus"
......
...@@ -118,6 +118,15 @@ pollJob limit offset jid je = do ...@@ -118,6 +118,15 @@ pollJob limit offset jid je = do
let st = either (const SJ.IsFailure) (const SJ.IsFinished) r let st = either (const SJ.IsFailure) (const SJ.IsFinished) r
me = either (Just . T.pack . show) (const Nothing) r me = either (Just . T.pack . show) (const Nothing) r
in pure (ls, st, me) in pure (ls, st, me)
-- /NOTE/: We need to be careful with the ordering of the logs here:
-- we want to return the logs ordered from the newest to the oldest,
-- because the API will use 'limit' to show only the newest ones,
-- taking 'limit' of them from the front of the list.
--
-- Due to the fact we do not force any 'Ord' constraint on an 'event' type,
-- and it would be inefficient to reverse the list here, it's important
-- that the concrete implementation of 'rjGetLog' returns the logs in the
-- correct order.
pure $ SJ.jobStatus jid limit offset (toList logs) status merr pure $ SJ.jobStatus jid limit offset (toList logs) status merr
waitJob waitJob
...@@ -176,4 +185,5 @@ killJob t limit offset jid je = do ...@@ -176,4 +185,5 @@ killJob t limit offset jid je = do
me = either (Just . T.pack . show) (const Nothing) r me = either (Just . T.pack . show) (const Nothing) r
removeJob False t jid removeJob False t jid
pure (lgs, st, me) pure (lgs, st, me)
-- /NOTE/: Same proviso as in 'pollJob' applies here.
pure $ SJ.jobStatus jid limit offset (toList logs) status merr pure $ SJ.jobStatus jid limit offset (toList logs) status merr
...@@ -13,7 +13,6 @@ module Gargantext.Utils.Jobs.Map ( ...@@ -13,7 +13,6 @@ module Gargantext.Utils.Jobs.Map (
, newJobMap , newJobMap
, lookupJob , lookupJob
, gcThread , gcThread
, jobLog
, addJobEntry , addJobEntry
, deleteJob , deleteJob
, runJob , runJob
...@@ -117,8 +116,10 @@ gcThread js (JobMap mvar) = go ...@@ -117,8 +116,10 @@ gcThread js (JobMap mvar) = go
_ -> False _ -> False
-- | Make a 'Logger' that 'mappend's monoidal values in a 'TVar'. -- | Make a 'Logger' that 'mappend's monoidal values in a 'TVar'.
-- /IMPORTANT/: The new value is appended in front. The ordering is important later on
-- when consuming logs from the API (see for example 'pollJob').
jobLog :: Semigroup w => TVar w -> Logger w -- w -> IO () jobLog :: Semigroup w => TVar w -> Logger w -- w -> IO ()
jobLog logvar = \w -> atomically $ modifyTVar' logvar (\old_w -> old_w <> w) jobLog logvar = \w -> atomically $ modifyTVar' logvar (\old_w -> w <> old_w)
-- | Generating new 'JobEntry's. -- | Generating new 'JobEntry's.
addJobEntry addJobEntry
......
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