<commit_before>module Job.Manage where

import Control.Concurrent
import Control.Exception
import Job.Types
import System.Exit
import System.IO
import System.Process
import Text.Regex.Posix

type Signaller = String -> Status -> String -> IO ()

runAllJobs :: Signaller -> Jobs -> IO Jobs
runAllJobs signalResult (Jobs jobs) = fmap Jobs (mapM (reRunJob signalResult) jobs)

reRunJobs :: FilePath -> Signaller -> Jobs -> IO Jobs
reRunJobs fileChanged signalResult (Jobs jobs) = fmap Jobs (mapM reRunIfMatch jobs)
    where
        reRunIfMatch job =
            if fileChanged =~ matchExpr job
                then reRunJob signalResult job
                else return job

reRunJob :: Signaller -> Job -> IO Job
reRunJob signalResult job = do
    cancel job
    -- NOTE: signalResult must be called asynchronoulsy, otherwise the lock for
    -- jobsRef will deadlock.
    threadId <- runThread job signalResult
    return $ job { runningInfo = RunningJobInfo Working "" (Just threadId) }

cancel :: Job -> IO ()
cancel Job { runningInfo = RunningJobInfo _ _ (Just id) } = killThread id
cancel _                                                  = return ()

runThread :: Job -> Signaller -> IO ThreadId
runThread job signalResult = do
    (_, Just hOut, Just hErr, pid) <-<commit_msg>Pass RunningJobInfo to cancel, as it only needs threadid<commit_after>module Job.Manage where

import Control.Concurrent
import Control.Exception
import Job.Types
import System.Exit
import System.IO
import System.Process
import Text.Regex.Posix

type Signaller = String -> Status -> String -> IO ()

runAllJobs :: Signaller -> Jobs -> IO Jobs
runAllJobs signalResult (Jobs jobs) = fmap Jobs (mapM (reRunJob signalResult) jobs)

reRunJobs :: FilePath -> Signaller -> Jobs -> IO Jobs
reRunJobs fileChanged signalResult (Jobs jobs) = fmap Jobs (mapM reRunIfMatch jobs)
    where
        reRunIfMatch job =
            if fileChanged =~ matchExpr job
                then reRunJob signalResult job
                else return job

reRunJob :: Signaller -> Job -> IO Job
reRunJob signalResult job = do
    cancel $ runningInfo job
    -- NOTE: signalResult must be called asynchronously, otherwise the lock for
    -- jobsRef will deadlock.
    threadId <- runThread job signalResult
    return $ job { runningInfo = RunningJobInfo Working "" (Just threadId) }

cancel :: RunningJobInfo -> IO ()
cancel (RunningJobInfo _ _ (Just id) ) = killThread id
cancel _                               = return ()

runThread :: Job -> Signaller -> IO ThreadId
runThread job signalResult = do
    (_, Just hOut, Just hErr, pid) <-
{'fraction_non_alphanumeric': 0.07979953739398613, 'fraction_numerical': 0.0, 'mean_word_length': 2.8847305389221556, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6465528', 'n_tokens_mistral': 723, 'n_tokens_neox': 710, 'n_words': 267}