Fixed jobs to terminate if interrupted by exception.

This commit is contained in:
Martin Sosic 2020-10-29 18:11:39 +01:00
parent 4889fefd0f
commit 0c4ae72495

View File

@ -1,3 +1,5 @@
{-# LANGUAGE ScopedTypeVariables #-}
module Generator.Job.Process module Generator.Job.Process
( runProcessAsJob ( runProcessAsJob
, runNodeCommandAsJob , runNodeCommandAsJob
@ -5,6 +7,7 @@ module Generator.Job.Process
import Control.Concurrent (writeChan) import Control.Concurrent (writeChan)
import Control.Concurrent.Async (Concurrently (..)) import Control.Concurrent.Async (Concurrently (..))
import Control.Exception (bracket)
import qualified Data.ByteString.Char8 as BS import qualified Data.ByteString.Char8 as BS
import Data.Conduit (runConduit, (.|)) import Data.Conduit (runConduit, (.|))
import qualified Data.Conduit.List as CL import qualified Data.Conduit.List as CL
@ -22,10 +25,14 @@ import qualified StrongPath as SP
-- | Runs a given process while streaming its stderr and stdout to provided channel. -- | Runs a given process while streaming its stderr and stdout to provided channel.
-- Returns exit code of the process once it finishes, and also sends it to he channel. -- Returns exit code of the process once it finishes, and also sends it to he channel.
-- Makes sure to terminate the process if exception occurs.
runProcessAsJob :: P.CreateProcess -> J.JobType -> J.Job runProcessAsJob :: P.CreateProcess -> J.JobType -> J.Job
runProcessAsJob process jobType chan = do runProcessAsJob process jobType chan = bracket
(CP.ClosedStream, stdoutStream, stderrStream, processHandle) <- CP.streamingProcess process (CP.streamingProcess process)
(\(_, _, _, sph) -> terminateStreamingProcess sph)
runStreamingProcessAsJob
where
runStreamingProcessAsJob (CP.ClosedStream, stdoutStream, stderrStream, processHandle) = do
let forwardStdoutToChan = runConduit $ stdoutStream .| CL.mapM_ let forwardStdoutToChan = runConduit $ stdoutStream .| CL.mapM_
(\bs -> writeChan chan $ J.JobMessage { J._data = J.JobOutput (BS.unpack bs) J.Stdout (\bs -> writeChan chan $ J.JobMessage { J._data = J.JobOutput (BS.unpack bs) J.Stdout
, J._jobType = jobType }) , J._jobType = jobType })
@ -44,6 +51,11 @@ runProcessAsJob process jobType chan = do
return exitCode return exitCode
terminateStreamingProcess streamingProcessHandle = do
let processHandle = CP.streamingProcessHandleRaw streamingProcessHandle
P.terminateProcess processHandle
return $ ExitFailure 1
runNodeCommandAsJob :: Path Abs (Dir a) -> String -> [String] -> J.JobType -> J.Job runNodeCommandAsJob :: Path Abs (Dir a) -> String -> [String] -> J.JobType -> J.Job
runNodeCommandAsJob fromDir command args jobType chan = do runNodeCommandAsJob fromDir command args jobType chan = do
errorOrNodeVersion <- getNodeVersion errorOrNodeVersion <- getNodeVersion