diff options
| author | Roman Smrž <roman.smrz@seznam.cz> | 2026-11-25 21:30:59 +0100 |
|---|---|---|
| committer | Roman Smrž <roman.smrz@seznam.cz> | 2026-11-27 21:32:10 +0100 |
| commit | 54bf3a5f5c6f660a461d65aea279e698a51c0f1d (patch) | |
| tree | 9d9d9dc235d034586c6c29bf6e23bd25bf8e5dd0 /src | |
| parent | 0e9a59771131d8d4325aafd4c48ff2ef1267077f (diff) | |
Monad type for job run to provide custom MonadFail instance
Diffstat (limited to 'src')
| -rw-r--r-- | src/Job.hs | 37 | ||||
| -rw-r--r-- | src/Repo.hs | 3 |
2 files changed, 31 insertions, 9 deletions
@@ -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 |