-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMainWorkerClient.hs
80 lines (67 loc) · 2.52 KB
/
MainWorkerClient.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeOperators #-}
-- |
-- Copyright: © 2018 Herbert Valerio Riedel
-- SPDX-License-Identifier: GPL-3.0-or-later
--
module Main where
import Prelude.Local
import Control.Monad.Except (ExceptT (..), runExceptT)
-- import Data.Aeson
import Servant.HttpStreams
-- import Text.Groom
import qualified Data.ByteString.Lazy as BL
import Log
import PkgId
import WorkerApi
import WorkerApi.Client
queries :: Maybe PkgIdxTs -> ([CompilerID],[PkgId]) -> BaseUrl -> ExceptT ClientError IO ()
queries idxts (ghcvers,pkgids) baseurl = do
logDebugShow ghcvers
logDebugShow pkgids
logDebugShow =<< runClientM'' getWorkerInfo
forM_ ghcvers $ \gv -> do
forM_ pkgids $ \pid -> do
CreateJobRes jobid <- runClientM'' $ createJob (CreateJobReq gv idxts pid)
res1 <- runClientM'' $ getJobSolveInfo jobid
logDebugShow res1
res2 <- runClientM'' $ getJobBuildDepsInfo jobid
logDebugShow res2
res3 <- runClientM'' $ getJobBuildInfo jobid
logDebugShow res3
runClientM'' $ destroyJob jobid
-- get (CreateJobReq gv Nothing pid) manager baseurl
return ()
where
runClientM'' :: NFData a => ClientM a -> ExceptT ClientError IO a
runClientM'' = runClientM' baseurl
main :: IO ()
main = do
getArgs >>= \case
hostport0:idxtss:ghcverstr:args
| Just hostport <- decodeHostPort hostport0 -> go hostport idxtss ghcverstr args
_ -> do
logError "usage: matrix-worker-client <host:port> <idxstate> <ghcversion(s>) <pkgid1> [<pkgid2> [ ... ] ]"
exitFailure
where
go (h,p) idxtss ghcverstr args = do
let Just ghcver = mapM simpleParse (words ghcverstr)
Just pkgs = mapM simpleParse args
Just idxts = PkgIdxTs <$> read idxtss
res <- runExceptT (queries (Just idxts) (ghcver,pkgs) (BaseUrl Http h p "/api"))
case res of
Left (FailureResponse _ (Response {..})) -> do
logDebugShow (responseStatusCode, responseHeaders)
BL.putStr responseBody
Left err -> do
logError (tshow err)
Right () -> logInfo "DONE"
decodeHostPort :: String -> Maybe (String,Int)
decodeHostPort s0 = do
(h,':':pstr) <- pure (break (==':') s0)
p <- readMaybe pstr
pure (h,p)