summaryrefslogtreecommitdiff
path: root/src/Job.hs
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2022-12-05 22:07:27 +0100
committerRoman Smrž <roman.smrz@seznam.cz>2022-12-06 20:37:37 +0100
commit39cf8f9919ba953f4e782609562781daf3d13868 (patch)
tree584d1b0741673489564d4775b82d7ad8025cb558 /src/Job.hs
parent9162ce8010893f3694e213295140be771a344a29 (diff)
Read job recipes from config file
Diffstat (limited to 'src/Job.hs')
-rw-r--r--src/Job.hs79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/Job.hs b/src/Job.hs
new file mode 100644
index 0000000..db3f4d0
--- /dev/null
+++ b/src/Job.hs
@@ -0,0 +1,79 @@
+module Job (
+ Job(..),
+ JobOutput(..),
+ JobName(..), stringJobName,
+ ArtifactName(..),
+ runJob,
+) where
+
+import Control.Monad
+import Control.Monad.Except
+
+import Data.Text (Text)
+import Data.Text qualified as T
+
+import System.Directory
+import System.Exit
+import System.FilePath
+import System.IO
+import System.Process
+
+data Job = Job
+ { jobName :: JobName
+ , jobRecipe :: [CreateProcess]
+ , jobArtifacts :: [(ArtifactName, CreateProcess)]
+ }
+
+data JobOutput = JobOutput
+ { outName :: JobName
+ , outStatus :: Bool
+ , outArtifacts :: [(ArtifactName, FilePath)]
+ }
+ deriving (Show)
+
+data JobName = JobName Text
+ deriving (Eq, Ord, Show)
+
+stringJobName :: JobName -> String
+stringJobName (JobName name) = T.unpack name
+
+data ArtifactName = ArtifactName Text
+ deriving (Eq, Ord, Show)
+
+
+runJob :: FilePath -> String -> Job -> IO JobOutput
+runJob dir cid job = do
+ [path] <- lines <$> readProcess "mktemp" ["-d", "-t", "minici.XXXXXXXXXX"] ""
+
+ "" <- readProcess "git" ["--work-tree=" <> path, "restore", "--source=" <> cid, "--", "."] ""
+ ["tree", tid]:_ <- map words . lines <$> readProcess "git" ["cat-file", "-p", cid] ""
+
+ let jdir = dir </> "jobs" </> tid </> stringJobName (jobName job)
+ createDirectoryIfMissing True jdir
+ logs <- openFile (jdir </> "log") WriteMode
+
+ res <- runExceptT $ do
+ forM_ (jobRecipe job) $ \p -> do
+ (Just hin, _, _, hp) <- liftIO $ createProcess_ "" p
+ { cwd = Just path
+ , std_in = CreatePipe
+ , std_out = UseHandle logs
+ , std_err = UseHandle logs
+ }
+ liftIO $ hClose hin
+ exit <- liftIO $ waitForProcess hp
+
+ when (exit /= ExitSuccess) $
+ throwError ()
+
+ hClose logs
+ removeDirectoryRecursive $ path
+
+ writeFile (jdir </> "status") $
+ if res == Right () then "success\n" else "failure\n"
+
+ return JobOutput
+ { outName = jobName job
+ , outStatus = res == Right ()
+ , outArtifacts = []
+ }