Skip to content

Commit 02e379e

Browse files
committed
More correct progress updates
1 parent 76eed10 commit 02e379e

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

src/Control/Concurrent/Execute.hs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Control.Concurrent.Execute
1111
) where
1212

1313
import Control.Applicative
14-
import Control.Concurrent.Async (Concurrently (..))
14+
import Control.Concurrent.Async (Concurrently (..), async)
1515
import Control.Concurrent.STM
1616
import Control.Exception
1717
import Control.Monad (join)
@@ -37,10 +37,6 @@ data Action = Action
3737
data ActionContext = ActionContext
3838
{ acRemaining :: !(Set ActionId)
3939
-- ^ Does not include the current action
40-
, acCompleted :: !Int
41-
-- ^ Number of actions completed so far
42-
, acTotalActions :: !Int
43-
-- ^ Total number of actions to be performed
4440
}
4541
deriving Show
4642

@@ -49,7 +45,6 @@ data ExecuteState = ExecuteState
4945
, esExceptions :: TVar [SomeException]
5046
, esInAction :: TVar (Set ActionId)
5147
, esCompleted :: TVar Int
52-
, esTotalActions :: !Int
5348
}
5449

5550
data ExecuteException
@@ -63,14 +58,15 @@ instance Show ExecuteException where
6358

6459
runActions :: Int -- ^ threads
6560
-> [Action]
61+
-> (TVar Int -> IO ()) -- ^ progress updated
6662
-> IO [SomeException]
67-
runActions threads actions0 = do
63+
runActions threads actions0 withProgress = do
6864
es <- ExecuteState
6965
<$> newTVarIO actions0
7066
<*> newTVarIO []
7167
<*> newTVarIO Set.empty
7268
<*> newTVarIO 0
73-
<*> pure (length actions0)
69+
_ <- async $ withProgress $ esCompleted es
7470
if threads <= 1
7571
then runActions' es
7672
else runConcurrently $ sequenceA_ $ replicate threads $ Concurrently $ runActions' es
@@ -107,12 +103,9 @@ runActions' ExecuteState {..} =
107103
inAction
108104
writeTVar esActions as'
109105
modifyTVar esInAction (Set.insert $ actionId action)
110-
completed <- readTVar esCompleted
111106
return $ mask $ \restore -> do
112107
eres <- try $ restore $ actionDo action ActionContext
113108
{ acRemaining = remaining
114-
, acCompleted = completed
115-
, acTotalActions = esTotalActions
116109
}
117110
case eres of
118111
Left err -> atomically $ do

src/Stack/Build/Execute.hs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,21 @@ executePlan' plan ee = do
279279
(planTasks plan)
280280
(planFinals plan)
281281
threads <- asks $ configJobs . getConfig
282-
errs <- liftIO $ runActions threads actions
282+
errs <- liftIO $ runActions threads actions $ \doneVar -> do
283+
let total = length actions
284+
loop prev
285+
| prev == total =
286+
runInBase $ $logStickyDone ("Completed all " <> T.pack (show total) <> " actions.")
287+
| otherwise = do
288+
runInBase $ $logSticky ("Progress: " <> T.pack (show prev) <> "/" <> T.pack (show total))
289+
done <- atomically $ do
290+
done <- readTVar doneVar
291+
check $ done /= prev
292+
return done
293+
loop done
294+
if total > 1
295+
then loop 0
296+
else return ()
283297
unless (null errs) $ throwM $ ExecutionFailure errs
284298

285299
toActions :: M env m
@@ -303,15 +317,15 @@ toActions runInBase ee (mbuild, mfinal) =
303317
]
304318
afinal =
305319
case (,) <$> mfinal <*> mfunc of
306-
Nothing -> []
307-
Just (task@Task {..}, func) ->
320+
Just (task@Task {..}, (func, checkTask)) | checkTask task ->
308321
[ Action
309322
{ actionId = ActionId taskProvides ATFinal
310323
, actionDeps = addBuild taskProvides $
311324
(Set.map (\ident -> ActionId ident ATBuild) (tcoMissing taskConfigOpts))
312325
, actionDo = \ac -> runInBase $ func ac ee task
313326
}
314327
]
328+
_ -> []
315329
where
316330
addBuild ident =
317331
case mbuild of
@@ -321,22 +335,31 @@ toActions runInBase ee (mbuild, mfinal) =
321335
mfunc =
322336
case boptsFinalAction $ eeBuildOpts ee of
323337
DoNothing -> Nothing
324-
DoTests -> Just singleTest
325-
DoBenchmarks -> Just singleBench
326-
DoHaddock -> Just singleHaddock
338+
DoTests -> Just (singleTest, checkTest)
339+
DoBenchmarks -> Just (singleBench, checkBench)
340+
DoHaddock -> Just (singleHaddock, const True)
341+
342+
checkTest task =
343+
case taskType task of
344+
TTLocal lp -> not $ Set.null $ packageTests $ lpPackage lp
345+
_ -> assert False False
346+
347+
checkBench task =
348+
case taskType task of
349+
TTLocal lp -> not $ Set.null $ packageBenchmarks $ lpPackage lp
350+
_ -> assert False False
327351

328352
-- | Ensure that the configuration for the package matches what is given
329353
ensureConfig :: M env m
330354
=> Path Abs Dir -- ^ package directory
331-
-> ActionContext
332355
-> ExecuteEnv
333356
-> Task
334357
-> m () -- ^ announce
335358
-> (Bool -> [String] -> m ()) -- ^ cabal
336359
-> Path Abs File -- ^ .cabal file
337360
-> [Text]
338361
-> m (ConfigCache, Bool)
339-
ensureConfig pkgDir ac ExecuteEnv {..} Task {..} announce cabal cabalfp extra = do
362+
ensureConfig pkgDir ExecuteEnv {..} Task {..} announce cabal cabalfp extra = do
340363
-- Determine the old and new configuration in the local directory, to
341364
-- determine if we need to reconfigure.
342365
mOldConfigCache <- tryGetConfigCache pkgDir
@@ -363,8 +386,6 @@ ensureConfig pkgDir ac ExecuteEnv {..} Task {..} announce cabal cabalfp extra =
363386
|| mOldCabalMod /= Just newCabalMod
364387
when needConfig $ withMVar eeConfigureLock $ \_ -> do
365388
deleteCaches pkgDir
366-
withMVar eeInstallLock $ \() ->
367-
$logSticky ("Progress: " <> T.pack (show (acCompleted ac)) <> "/" <> T.pack (show (acTotalActions ac)))
368389
announce
369390
cabal False $ "configure" : map T.unpack configOpts
370391
$logDebug $ T.pack $ show configOpts
@@ -506,7 +527,7 @@ singleBuild :: M env m
506527
-> m ()
507528
singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} =
508529
withSingleContext ac ee task $ \package cabalfp pkgDir cabal announce console _mlogFile -> do
509-
(cache, _neededConfig) <- ensureConfig pkgDir ac ee task (announce "configure") cabal cabalfp []
530+
(cache, _neededConfig) <- ensureConfig pkgDir ee task (announce "configure") cabal cabalfp []
510531

511532
fileModTimes <- getPackageFileModTimes package cabalfp
512533
writeBuildCache pkgDir fileModTimes
@@ -518,11 +539,6 @@ singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} =
518539
withMVar eeInstallLock $ \() -> do
519540
announce "install"
520541
cabal False ["install"]
521-
unless (acTotalActions == 1) $ do
522-
let done' = acCompleted + 1
523-
$logSticky ("Progress: " <> T.pack (show done') <> "/" <> T.pack (show acTotalActions))
524-
when (done' == acTotalActions)
525-
($logStickyDone ("Completed all " <> T.pack (show acTotalActions) <> " packages."))
526542

527543
let pkgDbs =
528544
case taskLocation task of
@@ -549,9 +565,8 @@ singleTest :: M env m
549565
-> Task
550566
-> m ()
551567
singleTest ac ee task =
552-
withSingleContext ac ee task $ \package cabalfp pkgDir cabal announce console mlogFile ->
553-
unless (Set.null $ packageTests package) $ do
554-
(_cache, neededConfig) <- ensureConfig pkgDir ac ee task (announce "configure (test)") cabal cabalfp ["--enable-tests"]
568+
withSingleContext ac ee task $ \package cabalfp pkgDir cabal announce console mlogFile -> do
569+
(_cache, neededConfig) <- ensureConfig pkgDir ee task (announce "configure (test)") cabal cabalfp ["--enable-tests"]
555570
config <- asks getConfig
556571

557572
let needBuild = neededConfig ||
@@ -622,9 +637,8 @@ singleBench :: M env m
622637
-> Task
623638
-> m ()
624639
singleBench ac ee task =
625-
withSingleContext ac ee task $ \package cabalfp pkgDir cabal announce console _mlogFile ->
626-
unless (Set.null $ packageBenchmarks package) $ do
627-
(_cache, neededConfig) <- ensureConfig pkgDir ac ee task (announce "configure (benchmarks)") cabal cabalfp ["--enable-benchmarks"]
640+
withSingleContext ac ee task $ \_package cabalfp pkgDir cabal announce console _mlogFile -> do
641+
(_cache, neededConfig) <- ensureConfig pkgDir ee task (announce "configure (benchmarks)") cabal cabalfp ["--enable-benchmarks"]
628642

629643
let needBuild = neededConfig ||
630644
(case taskType task of

0 commit comments

Comments
 (0)