@@ -3,9 +3,7 @@ module Test.Utils.Docker
33 , withDocker
44 ) where
55
6- import Control.Concurrent.Async (race , wait , withAsync )
76import Control.Concurrent (MVar , newMVar , modifyMVar )
8- import Control.Concurrent.Async (race_ )
97import Control.Exception (ErrorCall (.. ), throwIO )
108import Control.Monad (forM_ )
119import System.Exit (ExitCode (.. ))
@@ -24,15 +22,13 @@ import System.Process
2422 ( CreateProcess (.. )
2523 , StdStream (.. )
2624 , proc
27- , waitForProcess
25+ , getProcessExitCode
2826 , createPipe
29- , cleanupProcess
30- , terminateProcess
3127 , withCreateProcess
3228 )
3329import System.IO.Unsafe (unsafePerformIO )
3430import Utils.Async (withAsyncThrow )
35- import Utils.Delay (delay , seconds )
31+ import Utils.Delay (seconds , every )
3632
3733data DockerCommand
3834 = DockerRun
@@ -64,34 +60,24 @@ withDocker debug tag cmd act =
6460 }
6561 withCreateProcess create $ \ stdin stdout stderr p -> do
6662 let pinfo = (stdin, stdout, stderr, p)
67- withAsync (waitFor name pinfo) $ \ a -> do
68- r <- race (wait a) $ if debug
69- then tracing name hread act
70- else act hread
71- terminate pinfo
72- case r of
73- Left _ -> error " impossible"
74- Right v -> return v
63+ withAsyncThrow (waitFor name pinfo) $ do
64+ if debug
65+ then tracing name hread act
66+ else act hread
7567 where
76- terminate pinfo = race_ (cleanupProcess pinfo) (forceEnd pinfo)
77-
78- forceEnd (_,_,_,p) = do
79- putStrLn $ " Forcing end of container: " <> tag
80- delay (seconds 5 )
81- terminateProcess p
82-
8368 withPipe f = do
8469 (hread, hwrite) <- createPipe
8570 hSetBuffering hread LineBuffering
8671 hSetBuffering hwrite LineBuffering
8772 f hread hwrite
8873
89- waitFor name (_,_,_,p) = do
90- exit <- waitForProcess p
91- throwIO $ ErrorCall $ case exit of
92- ExitSuccess -> " unexpected successful termination of container " <> name
93- ExitFailure code ->
94- " docker failed with exit code" <> show code <> " for container " <> name
74+ waitFor name (_,_,_,p) = every (seconds 1 ) $ do
75+ mexit <- getProcessExitCode p
76+ forM_ mexit $ \ exit ->
77+ throwIO $ ErrorCall $ case exit of
78+ ExitSuccess -> " unexpected successful termination of container " <> name
79+ ExitFailure code ->
80+ " docker failed with exit code" <> show code <> " for container " <> name
9581
9682 mkName = do
9783 number <- modifyMVar dockerImageNumber $ \ n -> return (n + 1 , n)
0 commit comments