1 {-# OPTIONS_GHC -fno-warn-unused-imports #-}
2 {-# LANGUAGE DeriveAnyClass #-}
3 {-# LANGUAGE DuplicateRecordFields #-}
5 module Gargantext.API.GraphQL.AsyncTask where
7 import Control.Concurrent.Async (poll)
8 import Control.Concurrent.MVar (readMVar)
11 import qualified Data.Map as Map
12 import Control.Monad.Base (liftBase)
13 import Control.Monad.Reader (ask, liftIO)
14 import Data.Either (Either(..))
15 import qualified Data.IntMap.Strict as IntMap
16 import Data.Maybe (Maybe(..), catMaybes)
17 import Data.Morpheus.Types
24 import Data.Text (Text)
25 import qualified Data.Text as T
26 import Gargantext.API.Admin.Orchestrator.Types (JobLog(..))
27 import Gargantext.API.Prelude (GargM, GargError, HasJobEnv')
28 import Gargantext.Core.Mail.Types (HasMail)
29 import Gargantext.Database.Admin.Types.Node (NodeId(..))
30 import Gargantext.Database.Prelude (HasConnectionPool, HasConfig)
31 import Gargantext.Prelude
32 import GHC.Generics (Generic)
33 import Servant.Job.Async (HasJobEnv(job_env), jenv_jobs, job_async)
34 import Servant.Job.Core (env_item, env_map, env_state_mvar)
39 } deriving (Generic, GQLType)
41 type GqlM e env = Resolver QUERY e (GargM env GargError)
44 :: (HasConnectionPool env, HasConfig env, HasJobEnv' env)
45 => JobLogArgs -> GqlM e env (Map Int JobLog)
46 resolveJobLogs JobLogArgs { job_log_id } = dbJobLogs job_log_id
49 :: (HasConnectionPool env, HasConfig env, HasJobEnv' env)
50 => Int -> GqlM e env (Map Int JobLog)
51 dbJobLogs job_log_id = do
52 --getJobLogs job_log_id
55 --val <- liftBase $ readMVar $ env ^. job_env . jenv_jobs . env_state_mvar
56 var <- liftIO $ readMVar (env ^. job_env . jenv_jobs . env_state_mvar)
57 let envItems = var ^. env_map
58 printDebug "[dbJobLogs] env ^. job_env ^. jenv_jobs" $ length $ IntMap.keys envItems
59 printDebug "[dbJobLogs] job_log_id" job_log_id
60 --pure $ IntMap.elems val
62 let jobsList = IntMap.toList $ IntMap.map (\e -> e ^. env_item . job_async) envItems
63 results <- mapM (\(k, v) -> do
69 Right p'' -> Just (k, p'')
71 pure $ Map.fromList $ catMaybes results