module RunEnvironment
  (
    -- types
    RunEnv(..)
    -- functions
  , setTimeBase
  , getRunTime
  ) where

import           RunAuxiliary
import           RunNode

import qualified Data.Map.Strict as Map
import           Data.Time.Clock (UTCTime)

import           Control.Concurrent

------------------------------------------------------------

-- environment, accessible via Reader Monad
data RunEnv = RunEnv {
      envLogFileWork    :: FilePath         -- name of the logfile for work output
    , envLogGrepWork    :: FilePath         -- name of script for checking work log files
                                            -- for each group, time (in seconds) after which
                                            -- a task should be aborted
    , envLogGrepTimeSig :: String           -- time signature for the logGrep command to form
                                            -- a unique log file for the grep output
    , envTimeout        :: Map.Map GroupId TimeoutSeconds
    , envNodes          :: Map.Map String AnyNode
    , envExecSorted     :: Bool             -- sorted or random execution
    , envLockIO         :: MVar ()
    , envTimeBase       :: UTCTime          -- base time for computing relative time
    }


------------------------------------------------------------

setTimeBase :: UTCTime -> RunEnv -> RunEnv
setTimeBase t e = e { envTimeBase = t }


getRunTime :: RunEnv -> IO Double
getRunTime = relativeTime . envTimeBase
