on 1c8ce3a help sign in
11 files changed+34−143
since splitsnapshots
unifiedsplit
app/Main.hs+6−23 package.yaml−1 src/Control/HasGithubStatus.hs−1 src/Control/Task/Scheduler.hs+1−4 src/Control/Task/Scheduler/Query.hs+1−6 src/Data/Submission.hs−1 src/Server.hs+22−52 src/Server/Config.hs+2−2 src/Server/Html.hs+1−4 src/Server/Schema.hs+1−3 src/Server/Schema/V2.hs−46
app/Main.hs+6−23
@@ -1,30 +1,13 @@
11module Main (main) where
22
33import qualified Data.ByteString.Char8 as BS
4−import Data.Yaml (encode)
5−import Options.Applicative
4+import Data.Yaml
65import Server
76import Server.Config
7+import System.Environment
88
99main :: IO ()
10−main =
11− execParser (info (getOptions <**> helper) fullDesc) >>= \case
12− PrintDefaultConfig -> putStrLn . BS.unpack . encode $ defaultConfig
13− RunServer config -> runServer config
14−
15−data Run
16− = PrintDefaultConfig
17− | RunServer FilePath
18−
19−getOptions :: Parser Run
20−getOptions =
21− subparser
22− ( command "default" (info (pure PrintDefaultConfig <**> helper) fullDesc)
23− )
24− <|> ( RunServer
25− <$> strOption
26− ( long "config"
27− <> help "Path to yaml config file."
28− <> value "config.yaml"
29− )
30− )
10+main = getArgs >>= \case
11+ [] -> runServer
12+ ["default"] -> putStrLn . BS.unpack . encode $ defaultConfig
13+ xs -> error . show $ xs
package.yaml−1
@@ -66,7 +66,6 @@ executables:
6666 - sc-build
6767 - bytestring
6868 - yaml
69− - optparse-applicative
7069 sc-build-migrations:
7170 main: Main.hs
7271 source-dirs: migrations
src/Control/HasGithubStatus.hs−1
@@ -11,4 +11,3 @@ class HasGithubStatus m where
1111 schedulePendingStatus :: WithLog env Message m => Name Owner -> Name Repo -> Name Commit -> m ()
1212 scheduleTestedStatus :: WithLog env Message m => TestResult -> Name Owner -> Name Repo -> Name Commit -> m ()
1313 scheduleFailedStatus :: WithLog env Message m => String -> Name Owner -> Name Repo -> Name Commit -> m ()
14− deadlineHasPassedStatus :: WithLog env Message m => Name Owner -> Name Repo -> Name Commit -> m ()
src/Control/Task/Scheduler.hs+1−4
@@ -21,7 +21,6 @@ import Data.Word
2121import GHC.TypeLits
2222import Server.Schema
2323import Squeal.PostgreSQL
24−import UnliftIO.Exception
2524
2625scheduleTask ::
2726 (StaticPQ m, WithLog env Message m, Task t s, MonadUnliftIO m) =>
@@ -67,9 +66,7 @@ instance
6766 <> e
6867 A.Success t -> do
6968 logInfo . T.pack $ "running task " <> task <> " with " <> show t <> " created at " <> show initialStartTime
70− r <- performTask @t @ts @m t `catchAny` \e -> do
71− logError . T.pack . displayException $ e
72− return Failure
69+ r <- performTask @t @ts @m t
7370 when (r == Failure) $ logWarning . T.pack $ "Task " <> task <> " failed"
7471 case r of
7572 Success -> completeTaskQuery t
src/Control/Task/Scheduler/Query.hs+1−6
@@ -41,7 +41,6 @@ scheduleTaskQuery task = do
4141 :* Set (param @2) `as` #payload
4242 :* Default `as` #creation_time
4343 :* Default `as` #started
44− :* Default `as` #failures
4544 )
4645 ( OnConflict
4746 (OnConstraint #pk_task_payload)
@@ -88,7 +87,6 @@ rescheduleTaskQuery payload = do
8887 #tasks
8988 ( Default `as` #creation_time
9089 :* Default `as` #started
91− :* Set (#failures + 1) `as` #failures
9290 )
9391 (param @1 .== #task .&& param @2 .== #payload)
9492 dbWrite (const $ return ()) $
@@ -103,7 +101,7 @@ pickTasksQuery ::
103101 Word64 ->
104102 m [PickedTask]
105103pickTasksQuery tasks limitCount = do
106− logDebug $ "reading tasks " <> T.unwords (T.pack <$> tasks)
104+ logDebug "reading tasks"
107105 let query :: Query_ Schema () PickedTask
108106 query =
109107 select_
@@ -114,10 +112,7 @@ pickTasksQuery tasks limitCount = do
114112 ( from (table #tasks)
115113 & where_ (#task `in_` (literal <$> tasks))
116114 & where_ (not_ . notNull $ #started)
117− & where_ (#failures .< 20)
118− & orderBy [#creation_time & Asc]
119115 & limit limitCount
120− & orderBy [#creation_time & Desc]
121116 )
122117 startedMan :: Manipulation_ Schema (String, Jsonb Value) ()
123118 startedMan =
src/Data/Submission.hs−1
@@ -29,7 +29,6 @@ data SubmissionStatus
2929 = SubmissionRun TestResult
3030 | SubmissionFailed String
3131 | BuildScheduled
32− | PastDeadline
3332 deriving (Generic, FromJSON, ToJSON, Eq, Show)
3433
3534newtype TestResult
src/Server.hs+22−52
@@ -30,7 +30,6 @@ import Data.Submission.Query
3030import Data.Tasks.Build
3131import Data.Tasks.StatusUpdate
3232import qualified Data.Text as T
33−import Data.Time.Clock
3433import Data.Yaml
3534import GHC.Generics
3635import GitHub hiding (Accept)
@@ -50,6 +49,7 @@ import Server.Config
5049import Server.Html
5150import Server.Schema
5251import Squeal.PostgreSQL
52+import System.IO
5353import Text.Blaze.Html
5454import UnliftIO.Exception (catch, displayException, handleAny, throwIO, try)
5555
@@ -75,18 +75,14 @@ type API =
7575 :> Post '[HTML] Markup
7676 :<|> "results" :> Capture "task name" String :> Get '[CSV, HTML] [Score]
7777
78−webhookInstallation :: WithLog env Message m => RepoWebhookEvent -> ((), InstallationEvent) -> m ()
78+webhookInstallation :: MonadIO m => RepoWebhookEvent -> ((), InstallationEvent) -> m ()
7979webhookInstallation _ ((), ev) =
80− logInfo . T.pack . show $ ev
80+ liftIO $ do
81+ print ev
82+ hFlush stdout
8183
8284webhookPushEvent ::
83− ( StaticPQ m,
84− MonadReader (ServerData m) m,
85− MonadUnliftIO m,
86− HasTasks m,
87− MonadError ServerError m,
88− WithLog env Message m
89− ) =>
85+ (StaticPQ m, MonadReader (ServerData m) m, MonadUnliftIO m, HasTasks m, MonadError ServerError m) =>
9086 RepoWebhookEvent ->
9187 ((), PushEvent) ->
9288 m ()
@@ -104,28 +100,18 @@ webhookPushEvent _ ((), ev) = do
104100 M.takeWhileAntitone (repoName >) tasks
105101 case task of
106102 Just (name, TaskConfig {..}) | T.take (T.length name) repoName == name -> do
107− nowTime <- liftIO getCurrentTime
108− if nowTime > deadline
109− then do
110− deadlineHasPassedStatus (N owner) (N repoName) (N sha)
111− createSubmission $
112− Submission
113− user
114− (T.unpack fullRepoName)
115− (T.unpack name)
116− (T.unpack sha)
117− (Jsonb PastDeadline)
118− else do
119− schedulePendingStatus (N owner) (N repoName) (N sha)
120− createSubmission $
121− Submission
122− user
123− (T.unpack fullRepoName)
124− (T.unpack name)
125− (T.unpack sha)
126− (Jsonb BuildScheduled)
127− scheduleTask $ Build prebuild dockerfile (N owner) (N repoName) (N sha) timeoutMinutes
128− logInfo . T.pack . show $ ev
103+ schedulePendingStatus (N owner) (N repoName) (N sha)
104+ createSubmission $
105+ Submission
106+ user
107+ (T.unpack fullRepoName)
108+ (T.unpack name)
109+ (T.unpack sha)
110+ (Jsonb BuildScheduled)
111+ scheduleTask $ Build prebuild dockerfile (N owner) (N repoName) (N sha) timeoutMinutes
112+ liftIO $ do
113+ print ev
114+ hFlush stdout
129115 _ -> return ()
130116
131117retestSubmission ::
@@ -175,9 +161,9 @@ instance MonadError ServerError (ServerM schema) where
175161server :: ServerT API (ServerM Schema)
176162server = (webhookInstallation :<|> webhookPushEvent) :<|> getSubmissionR :<|> retestSubmission :<|> getResults
177163
178−runServer :: FilePath -> IO ()
179−runServer config = do
180− c@Config {..} <- getConfig config
164+runServer :: IO ()
165+runServer = do
166+ c@Config {..} <- getConfig
181167 print c
182168 pem <- BS.readFile appPkPemPath
183169 appPkPem <- case readRsaPem pem of
@@ -329,21 +315,6 @@ instance (MonadReader (ServerData m) m, StaticPQ m, MonadUnliftIO m) => HasGithu
329315 newStatusContext = Just c
330316 }
331317 scheduleTask status
332− deadlineHasPassedStatus nOwner@(N owner) nRepo@(N repo) nCommit@(N sha) = do
333− h <- asks baseUrl
334− c <- asks context
335− let status =
336− StatusUpdate
337− nOwner
338− nRepo
339− nCommit
340− NewStatus
341− { newStatusState = StatusError,
342− newStatusTargetUrl = Just . URL $ h <> "/submission/" <> owner <> "/" <> repo <> "/" <> sha,
343− newStatusDescription = Just "The deadline has passed ⏰",
344− newStatusContext = Just c
345− }
346− scheduleTask status
347318
348319instance (MonadReader (ServerData n) m) => HasDockerInfo m where
349320 getDockerUrl = asks dockerUrl
@@ -354,8 +325,7 @@ data TaskConfig
354325 = TaskConfig
355326 { prebuild :: String,
356327 dockerfile :: String,
357− timeoutMinutes :: Int,
358− deadline :: UTCTime
328+ timeoutMinutes :: Int
359329 }
360330 deriving stock (Show, Eq, Generic)
361331 deriving anyclass (FromJSON, ToJSON)
src/Server/Config.hs+2−2
@@ -71,8 +71,8 @@ defaultConfig =
7171 githubContext = "MY CI NAME"
7272 }
7373
74−getConfig :: FilePath -> IO Config
75−getConfig = decodeFileThrow
74+getConfig :: IO Config
75+getConfig = decodeFileThrow "config.yaml"
7676
7777-- getConfig :: IO Config
7878-- getConfig =
src/Server/Html.hs+1−4
@@ -39,10 +39,9 @@ getSubmissionR user repo sha' = do
3939 Nothing ->
4040 return . ("Not found",) $
4141 H.h1 "404. not found. go away."
42− Just Submission {..} -> return . (problem,) $ do
42+ Just Submission {..} -> return . (repo,) $ do
4343 H.h1 $ toHtml problem
4444 H.h2 $ toHtml userName
45− H.h3 . H.code . toHtml . take 7 $ sha
4645 case status of
4746 Jsonb BuildScheduled -> do
4847 H.h3 "Waiting to build ⌛"
@@ -61,8 +60,6 @@ getSubmissionR user repo sha' = do
6160 H.table $ flip M.foldMapWithKey tests $ \testName testPassed -> H.tr $ do
6261 (H.td H.! A.align "right") . H.p $ toHtml testName
6362 H.td $ if testPassed then "✅" else "❌"
64− Jsonb PastDeadline ->
65− H.h3 "This was submitted after the deadline ⏰"
6663 return . docTypeHtml $ do
6764 H.head $ do
6865 H.title $ toHtml titleText
src/Server/Schema.hs+1−3
@@ -6,15 +6,13 @@ module Server.Schema
66where
77
88import Server.Schema.V1
9−import Server.Schema.V2
109import Squeal.PostgreSQL
1110
1211type StaticPQ m = MonadPQ Schema m
1312
14−type Schema = SchemaV2
13+type Schema = SchemaV1
1514
1615migration :: AlignedList (Migration (Terminally PQ IO)) (Public '[]) Schema
1716migration =
1817 pureMigration schemaMigrationV1
19− :>> pureMigration schemaMigrationV2
2018 :>> Done
src/Server/Schema/V2.hsdeleted−46
@@ -1,46 +0,0 @@
1−module Server.Schema.V2
2− ( SchemaV2,
3− schemaMigrationV2,
4− )
5−where
6−
7−import Data.Aeson
8−import Data.Int
9−import Data.Submission
10−import Data.Text (Text)
11−import Data.Time
12−import Server.Schema.V1
13−import Squeal.PostgreSQL
14−
15−type SchemaV2 =
16− Public
17− '[ "tasks"
18− ::: 'Table
19− ( '[ "pk_task_payload" :=> 'PrimaryKey '["task", "payload"]
20− ]
21− :=> '[ "task" ::: 'NoDef :=> 'NotNull (PG Text),
22− "payload" ::: 'NoDef :=> 'NotNull (PG (Jsonb Value)),
23− "creation_time" ::: 'Def :=> 'NotNull (PG UTCTime),
24− "started" ::: 'Def :=> 'NotNull (PG Bool),
25− "failures" ::: 'Def :=> 'NotNull (PG Int32)
26− ]
27− ),
28− "submissions"
29− ::: 'Table
30− ( '["pk_submission_repo_full_name_sha" :=> 'PrimaryKey '["repo_full_name", "sha"]]
31− :=> '[ "user_name" ::: 'NoDef :=> 'NotNull (PG String),
32− "repo_full_name" ::: 'NoDef :=> 'NotNull (PG String),
33− "problem" ::: 'NoDef :=> 'NotNull (PG String),
34− "sha" ::: 'NoDef :=> 'NotNull (PG String),
35− "status" ::: 'NoDef :=> 'NotNull (PG (Jsonb SubmissionStatus))
36− ]
37− )
38− ]
39−
40−schemaMigrationV2 :: Migration Definition SchemaV1 SchemaV2
41−schemaMigrationV2 =
42− Migration
43− { name = "v2",
44− up = alterTable #tasks (addColumn #failures (default_ 0 $ notNullable int)),
45− down = alterTable #tasks (dropColumn #failures)
46− }