Commit 098e87bf authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Add MonadJob instance for ReaderT

This commit also moves the JobHandle into the `Gargantext.Utils.Jobs.Monad` module.
parent be496999
...@@ -36,7 +36,7 @@ serveJobsAPI ...@@ -36,7 +36,7 @@ serveJobsAPI
, m ~ (GargM env GargError) , m ~ (GargM env GargError)
) )
=> JobType m => JobType m
-> (Internal.JobHandle -> input -> Logger (JobEventType m) -> m (JobOutputType m)) -> (JobHandle -> input -> Logger (JobEventType m) -> m (JobOutputType m))
-> SJ.AsyncJobsServerT' ctI ctO callbacks (JobEventType m) input (JobOutputType m) m -> SJ.AsyncJobsServerT' ctI ctO callbacks (JobEventType m) input (JobOutputType m) m
serveJobsAPI jobType f = Internal.serveJobsAPI ask jobType jobErrorToGargError $ \env jHandle i l -> do serveJobsAPI jobType f = Internal.serveJobsAPI ask jobType jobErrorToGargError $ \env jHandle i l -> do
putStrLn ("Running job of type: " ++ show jobType) putStrLn ("Running job of type: " ++ show jobType)
......
{-# LANGUAGE TypeFamilies, ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies, ScopedTypeVariables #-}
module Gargantext.Utils.Jobs.Internal ( module Gargantext.Utils.Jobs.Internal (
serveJobsAPI serveJobsAPI
, JobHandle -- opaque
) where ) where
import Control.Concurrent import Control.Concurrent
...@@ -28,12 +27,6 @@ import qualified Servant.Job.Async as SJ ...@@ -28,12 +27,6 @@ import qualified Servant.Job.Async as SJ
import qualified Servant.Job.Client as SJ import qualified Servant.Job.Client as SJ
import qualified Servant.Job.Types as SJ import qualified Servant.Job.Types as SJ
-- | An opaque handle that abstracts over the concrete identifier for
-- a job. The constructor for this type is deliberately not exported.
newtype JobHandle =
JobHandle { _jh_id :: SJ.JobID 'SJ.Safe }
deriving (Eq, Ord)
serveJobsAPI serveJobsAPI
:: ( Ord t, Exception e, MonadError e m :: ( Ord t, Exception e, MonadError e m
, MonadJob m t (Seq event) output , MonadJob m t (Seq event) output
...@@ -94,7 +87,7 @@ newJob getenv jobkind f input = do ...@@ -94,7 +87,7 @@ newJob getenv jobkind f input = do
logF e logF e
f' jId inp logF = do f' jId inp logF = do
r <- f env (JobHandle jId) inp (pushLog logF . Seq.singleton) r <- f env (unsafeMkJobHandle jId) inp (pushLog logF . Seq.singleton)
case r of case r of
Left e -> postCallback (SJ.mkChanError e) >> throwIO e Left e -> postCallback (SJ.mkChanError e) >> throwIO e
Right a -> postCallback (SJ.mkChanResult a) >> return a Right a -> postCallback (SJ.mkChanResult a) >> return a
......
...@@ -4,6 +4,7 @@ module Gargantext.Utils.Jobs.Monad ( ...@@ -4,6 +4,7 @@ module Gargantext.Utils.Jobs.Monad (
JobEnv(..) JobEnv(..)
, NumRunners , NumRunners
, JobError(..) , JobError(..)
, JobHandle(..)
, MonadJob(..) , MonadJob(..)
, MonadJobStatus(..) , MonadJobStatus(..)
...@@ -22,6 +23,8 @@ module Gargantext.Utils.Jobs.Monad ( ...@@ -22,6 +23,8 @@ module Gargantext.Utils.Jobs.Monad (
, withJob , withJob
, handleIDError , handleIDError
, removeJob , removeJob
, unsafeMkJobHandle
, getLatestJobStatus
) where ) where
import Gargantext.Utils.Jobs.Settings import Gargantext.Utils.Jobs.Settings
...@@ -32,9 +35,11 @@ import Gargantext.Utils.Jobs.State ...@@ -32,9 +35,11 @@ import Gargantext.Utils.Jobs.State
import Control.Concurrent.STM import Control.Concurrent.STM
import Control.Exception import Control.Exception
import Control.Monad.Except import Control.Monad.Except
import Control.Monad.Reader
import Data.Functor ((<&>))
import Data.Kind (Type) import Data.Kind (Type)
import Data.Map.Strict (Map) import Data.Map.Strict (Map)
import Data.Sequence (Seq) import Data.Sequence (Seq, viewr, ViewR(..))
import Data.Time.Clock import Data.Time.Clock
import Network.HTTP.Client (Manager) import Network.HTTP.Client (Manager)
import Prelude import Prelude
...@@ -73,6 +78,9 @@ genSecret = SJ.generateSecretKey ...@@ -73,6 +78,9 @@ genSecret = SJ.generateSecretKey
class MonadIO m => MonadJob m t w a | m -> t w a where class MonadIO m => MonadJob m t w a | m -> t w a where
getJobEnv :: m (JobEnv t w a) getJobEnv :: m (JobEnv t w a)
instance MonadIO m => MonadJob (ReaderT (JobEnv t w a) m) t w a where
getJobEnv = ask
getJobsSettings :: MonadJob m t w a => m JobSettings getJobsSettings :: MonadJob m t w a => m JobSettings
getJobsSettings = jeSettings <$> getJobEnv getJobsSettings = jeSettings <$> getJobEnv
...@@ -166,9 +174,39 @@ removeJob queued t jid = do ...@@ -166,9 +174,39 @@ removeJob queued t jid = do
-- Tracking jobs status -- Tracking jobs status
-- --
-- | An opaque handle that abstracts over the concrete identifier for
-- a job. The constructor for this type is deliberately not exported.
newtype JobHandle =
JobHandle { _jh_id :: SJ.JobID 'SJ.Safe }
deriving (Eq, Ord)
unsafeMkJobHandle :: SJ.JobID 'SJ.Safe -> JobHandle
unsafeMkJobHandle = JobHandle
-- | A monad to query for the status of a particular job /and/ submit updates for in-progress jobs. -- | A monad to query for the status of a particular job /and/ submit updates for in-progress jobs.
class MonadJob m (JobType m) (Seq (JobEventType m)) (JobOutputType m) => MonadJobStatus m where class MonadJob m (JobType m) (Seq (JobEventType m)) (JobOutputType m) => MonadJobStatus m where
type JobType m :: Type type JobType m :: Type
type JobOutputType m :: Type type JobOutputType m :: Type
type JobEventType m :: Type type JobEventType m :: Type
type JobErrorType m :: Type type JobErrorType m :: Type
--
-- Tracking jobs status API
--
-- | Retrevies the latest 'JobEventType' from the underlying monad. It can be
-- used to query the latest status for a particular job, given its 'JobHandle' as input.
getLatestJobStatus :: MonadJobStatus m => JobHandle -> m (Maybe (JobEventType m))
getLatestJobStatus (JobHandle jId) = do
mb_jb <- findJob jId
case mb_jb of
Nothing -> pure Nothing
Just j -> case jTask j of
QueuedJ _ -> pure Nothing
RunningJ rj -> liftIO (rjGetLog rj) <&>
\lgs -> case viewr lgs of
EmptyR -> Nothing
_ :> l -> Just l
DoneJ lgs _ -> pure $ case viewr lgs of
EmptyR -> Nothing
_ :> l -> Just l
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