summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Job.hs37
-rw-r--r--src/Repo.hs3
2 files changed, 31 insertions, 9 deletions
diff --git a/src/Job.hs b/src/Job.hs
index b40f450..2bc596b 100644
--- a/src/Job.hs
+++ b/src/Job.hs
@@ -30,6 +30,7 @@ import Control.Monad
import Control.Monad.Catch
import Control.Monad.Except
import Control.Monad.IO.Class
+import Control.Monad.Reader
import Data.Bifunctor
import Data.List
@@ -192,6 +193,28 @@ data JobCancelledException = JobCancelledException
instance Exception JobCancelledException
+newtype JobRun a = JobRun (ReaderT (Maybe Handle) (ExceptT (JobStatus JobOutput) IO) a)
+ deriving
+ ( Functor, Applicative, Monad
+ , MonadReader (Maybe Handle)
+ , MonadError (JobStatus JobOutput)
+ , MonadThrow, MonadCatch, MonadMask
+ , MonadIO
+ )
+
+instance MonadFail JobRun where
+ fail err = do
+ ask >>= \case
+ Just logs -> do
+ liftIO $ hPutStrLn logs err
+ throwError JobFailed
+ Nothing -> do
+ throwError $ JobError $ T.pack err
+
+fromJobRun :: JobRun a -> IO (Either (JobStatus JobOutput) a)
+fromJobRun (JobRun act) = runExceptT $ flip runReaderT Nothing $ act
+
+
newJobManager :: FilePath -> Int -> IO JobManager
newJobManager jmDataDir jmMaxRunningTasks = do
jmJobs <- newTVarIO M.empty
@@ -331,7 +354,7 @@ runJobs mngr@JobManager {..} tout jobs rerun = do
handle handler $ do
putMVar handlerInstalled ()
mtid <- myThreadId
- res <- runExceptT $ do
+ res <- fromJobRun $ do
join $ liftIO $ atomically $ do
mjs <- readTVar taskManagedJob
if mjsTaskCount mjs <= 0
@@ -521,9 +544,9 @@ copyArtifact storageDir jid aname tpath = do
liftIO $ copyRecursive (adir </> "data") tpath
-runJob :: Job -> [ ( ArtifactSpec Evaluated, ArtifactOutput) ] -> FilePath -> FilePath -> ExceptT (JobStatus JobOutput) IO JobOutput
+runJob :: Job -> [ ( ArtifactSpec Evaluated, ArtifactOutput) ] -> FilePath -> FilePath -> JobRun JobOutput
runJob job uses checkoutPath jdir = do
- bracket (liftIO $ openFile (jdir </> "log") WriteMode) (liftIO . hClose) $ \logs -> do
+ bracket (liftIO $ openFile (jdir </> "log") WriteMode) (liftIO . hClose) $ \logs -> local (const $ Just logs) $ do
forM_ (fromMaybe [] $ jobRecipe job) $ \ep -> do
( p, input ) <- case ep of
Left p -> return ( p, "" )
@@ -553,11 +576,9 @@ runJob job uses checkoutPath jdir = do
let adir = jdir </> "artifacts" </> T.unpack tname
path <- liftIO (globDir1 pathPattern checkoutPath) >>= \case
[ path ] -> return path
- found -> do
- liftIO $ hPutStrLn logs $
- (if null found then "no file" else "multiple files") <> " found matching pattern ‘" <>
- decompile pathPattern <> "’ for artifact ‘" <> T.unpack tname <> "’"
- throwError JobFailed
+ found -> fail $
+ (if null found then "no file" else "multiple files") <> " found matching pattern ‘" <>
+ decompile pathPattern <> "’ for artifact ‘" <> T.unpack tname <> "’"
let target = adir </> "data"
workPath = makeRelative checkoutPath path
liftIO $ do
diff --git a/src/Repo.hs b/src/Repo.hs
index 9fab2f7..fbcded0 100644
--- a/src/Repo.hs
+++ b/src/Repo.hs
@@ -489,7 +489,8 @@ readCommittedFile Tree {..} path = do
pushToBranch :: (MonadIO m, MonadFail m) => Branch -> Commit -> m ()
pushToBranch Branch {..} Commit {..} = do
when (not $ gitWritable branchRepo) $ do
- fail $ "repo ‘" <> gitDir branchRepo <> "’ is not writable"
+ -- throw IO exception to generate JobError instead of JobFailed
+ liftIO $ fail $ "repo ‘" <> gitDir branchRepo <> "’ is not writable"
join $ liftIO $
withMVar (gitLock branchRepo) $ \_ ->
withMVar (gitLock commitRepo) $ \_ -> do