assistant: Make --autostart --foreground wait for the children it starts.

Before, the --foreground was ignored when autostarting.

This commit was sponsored by Denis Dzyubenko on Patreon.
This commit is contained in:
Joey Hess 2017-02-07 13:31:45 -04:00
parent 571eca4680
commit 3439f3cc87
No known key found for this signature in database
GPG key ID: C910D9222512E3C7
3 changed files with 38 additions and 14 deletions

View file

@ -28,6 +28,8 @@ git-annex (6.20170102) UNRELEASED; urgency=medium
case, this change will need to be cherry-picked into the git-annex in case, this change will need to be cherry-picked into the git-annex in
Debian stable, or its wormhole pairing will break. Debian stable, or its wormhole pairing will break.
* Fix build with aws 0.16. Thanks, aristidb. * Fix build with aws 0.16. Thanks, aristidb.
* assistant: Make --autostart --foreground wait for the children it
starts. Before, the --foreground was ignored when autostarting.
-- Joey Hess <id@joeyh.name> Fri, 06 Jan 2017 15:22:06 -0400 -- Joey Hess <id@joeyh.name> Fri, 06 Jan 2017 15:22:06 -0400

View file

@ -1,6 +1,6 @@
{- git-annex assistant {- git-annex assistant
- -
- Copyright 2012-2015 Joey Hess <id@joeyh.name> - Copyright 2012-2017 Joey Hess <id@joeyh.name>
- -
- Licensed under the GNU GPL version 3 or higher. - Licensed under the GNU GPL version 3 or higher.
-} -}
@ -16,6 +16,8 @@ import qualified Build.SysConfig
import Utility.HumanTime import Utility.HumanTime
import Assistant.Install import Assistant.Install
import Control.Concurrent.Async
cmd :: Command cmd :: Command
cmd = dontCheck repoExists $ notBareRepo $ cmd = dontCheck repoExists $ notBareRepo $
noRepo (startNoRepo <$$> optParser) $ noRepo (startNoRepo <$$> optParser) $
@ -68,6 +70,7 @@ startNoRepo o
| autoStopOption o = autoStop | autoStopOption o = autoStop
| otherwise = giveup "Not in a git repository." | otherwise = giveup "Not in a git repository."
-- Does not return
autoStart :: AssistantOptions -> IO () autoStart :: AssistantOptions -> IO ()
autoStart o = do autoStart o = do
dirs <- liftIO readAutoStartFile dirs <- liftIO readAutoStartFile
@ -76,28 +79,47 @@ autoStart o = do
giveup $ "Nothing listed in " ++ f giveup $ "Nothing listed in " ++ f
program <- programPath program <- programPath
haveionice <- pure Build.SysConfig.ionice <&&> inPath "ionice" haveionice <- pure Build.SysConfig.ionice <&&> inPath "ionice"
forM_ dirs $ \d -> do pids <- forM dirs $ \d -> do
putStrLn $ "git-annex autostart in " ++ d putStrLn $ "git-annex autostart in " ++ d
ifM (catchBoolIO $ go haveionice program d) mpid <- catchMaybeIO $ go haveionice program d
if foregroundDaemonOption (daemonOptions o)
then return mpid
else do
case mpid of
Nothing -> putStrLn "failed"
Just pid -> ifM (checkSuccessProcess pid)
( putStrLn "ok" ( putStrLn "ok"
, putStrLn "failed" , putStrLn "failed"
) )
return Nothing
-- Wait for any foreground jobs to finish and propigate exit status.
ifM (all (== True) <$> mapConcurrently checkSuccessProcess (catMaybes pids))
( exitSuccess
, exitFailure
)
where where
go haveionice program dir = do go haveionice program dir = do
setCurrentDirectory dir setCurrentDirectory dir
-- First stop any old daemon running in this directory, which -- First stop any old daemon running in this directory, which
-- might be a leftover from an old login session. Such a -- might be a leftover from an old login session. Such a
-- leftover might be left in an environment where it is -- leftover might be left in an environment where it is
-- unavble to use the ssh agent or other login session -- unable to use the ssh agent or other login session
-- resources. -- resources.
void $ boolSystem program [Param "assistant", Param "--stop"] void $ boolSystem program [Param "assistant", Param "--stop"]
if haveionice (Nothing, Nothing, Nothing, pid) <- createProcess p
then boolSystem "ionice" (Param "-c3" : Param program : baseparams) return pid
else boolSystem program baseparams
where where
baseparams = p
[ Param "assistant" | haveionice = proc "ionice"
, Param $ "--startdelay=" ++ fromDuration (fromMaybe (Duration 5) (startDelayOption o)) (toCommand $ Param "-c3" : Param program : baseparams)
| otherwise = proc program
(toCommand baseparams)
baseparams = catMaybes
[ Just $ Param "assistant"
, Just $ Param $ "--startdelay=" ++ fromDuration (fromMaybe (Duration 5) (startDelayOption o))
, if foregroundDaemonOption (daemonOptions o)
then Just $ Param "--foreground"
else Nothing
] ]
autoStop :: IO () autoStop :: IO ()

View file

@ -32,4 +32,4 @@ It seems to be connected to the autostart option, without --autostart it does no
### Have you had any luck using git-annex before? (Sometimes we get tired of reading bug reports all day and a lil' positive end note does wonders) ### Have you had any luck using git-annex before? (Sometimes we get tired of reading bug reports all day and a lil' positive end note does wonders)
> Made it support this combination of switches. [[done]] --[[Joey]]