git-annex/Command/WebApp.hs

243 lines
7.6 KiB
Haskell
Raw Normal View History

{- git-annex webapp launcher
-
- Copyright 2012 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU AGPL version 3 or higher.
-}
2013-05-02 23:00:50 +00:00
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
2013-05-02 23:00:50 +00:00
module Command.WebApp where
import Command
import Assistant
import Assistant.Common
2012-10-30 18:34:48 +00:00
import Assistant.NamedThread
import Assistant.Threads.WebApp
import Assistant.WebApp
import Assistant.Install
import Annex.Environment
import Utility.WebApp
import Utility.Daemon (checkDaemon)
import Utility.UserInfo
2014-01-26 20:36:31 +00:00
import Annex.Init
import Annex.Path
import qualified Git
import Git.Types (fromConfigValue)
import qualified Git.Config
import qualified Git.CurrentRepo
import qualified Annex
import Config.Files.AutoStart
2013-11-17 18:58:35 +00:00
import Upgrade
import Annex.Version
avoid flushing keys db queue after each Annex action The flush was only done Annex.run' to make sure that the queue was flushed before git-annex exits. But, doing it there means that as soon as one change gets queued, it gets flushed soon after, which contributes to excessive writes to the database, slowing git-annex down. (This does not yet speed git-annex up, but it is a stepping stone to doing so.) Database queues do not autoflush when garbage collected, so have to be flushed explicitly. I don't think it's possible to make them autoflush (except perhaps if git-annex sqitched to using ResourceT..). The comment in Database.Keys.closeDb used to be accurate, since the automatic flushing did mean that all writes reached the database even when closeDb was not called. But now, closeDb or flushDb needs to be called before stopping using an Annex state. So, removed that comment. In Remote.Git, change to using quiesce everywhere that it used to use stopCoProcesses. This means that uses on onLocal in there are just as slow as before. I considered only calling closeDb on the local git remotes when git-annex exits. But, the reason that Remote.Git calls stopCoProcesses in each onLocal is so as not to leave git processes running that have files open on the remote repo, when it's on removable media. So, it seemed to make sense to also closeDb after each one, since sqlite may also keep files open. Although that has not seemed to cause problems with removable media so far. It was also just easier to quiesce in each onLocal than once at the end. This does likely leave performance on the floor, so could be revisited. In Annex.Content.saveState, there was no reason to close the db, flushing it is enough. The rest of the changes are from auditing for Annex.new, and making sure that quiesce is called, after any action that might possibly need it. After that audit, I'm pretty sure that the change to Annex.run' is safe. The only concern might be that this does let more changes get queued for write to the db, and if git-annex is interrupted, those will be lost. But interrupting git-annex can obviously already prevent it from writing the most recent change to the db, so it must recover from such lost data... right? Sponsored-by: Dartmouth College's Datalad project
2022-10-12 17:50:46 +00:00
import Annex.Action
import Utility.Android
import Control.Concurrent
import Control.Concurrent.STM
cmd :: Command
cmd = noCommit $ dontCheck repoExists $ notBareRepo $
noRepo (startNoRepo <$$> optParser) $
command "webapp" SectionCommon "launch webapp"
paramNothing (seek <$$> optParser)
data WebAppOptions = WebAppOptions
{ listenAddress :: Maybe String
}
optParser :: CmdParamsDesc -> Parser WebAppOptions
optParser _ = WebAppOptions
<$> optional (strOption
( long "listen" <> metavar paramAddress
<> help "accept connections to this address"
))
seek :: WebAppOptions -> CommandSeek
seek = commandAction . start
start :: WebAppOptions -> CommandStart
start = start' True
start' :: Bool -> WebAppOptions -> CommandStart
start' allowauto o = do
2013-09-25 07:09:06 +00:00
liftIO ensureInstalled
ifM (isInitialized <&&> notHome)
( maybe notinitialized (go <=< needsUpgrade) =<< getVersion
, if allowauto
then liftIO $ startNoRepo o
else notinitialized
2013-11-17 18:58:35 +00:00
)
stop
2012-11-12 05:05:04 +00:00
where
go cannotrun = do
2012-11-12 05:05:04 +00:00
browser <- fromRepo webBrowser
f <- liftIO . absPath =<< fromRepo gitAnnexHtmlShim
listenAddress' <- if isJust (listenAddress o)
then pure (listenAddress o)
else annexListen <$> Annex.getGitConfig
ifM (checkpid <&&> checkshim (fromRawFilePath f))
( if isJust (listenAddress o)
then giveup "The assistant is already running, so --listen cannot be used."
2013-05-03 00:38:57 +00:00
else do
url <- liftIO . readFile . fromRawFilePath
2013-05-03 00:38:57 +00:00
=<< fromRepo gitAnnexUrlFile
liftIO $ if isJust listenAddress'
then putStrLn url
else liftIO $ openBrowser browser (fromRawFilePath f) url Nothing Nothing
, do
startDaemon True True Nothing cannotrun listenAddress' $ Just $
\origout origerr url htmlshim ->
if isJust listenAddress'
then maybe noop (`hPutStrLn` url) origout
else openBrowser browser htmlshim url origout origerr
2012-11-12 05:05:04 +00:00
)
checkpid = do
pidfile <- fromRepo gitAnnexPidFile
liftIO $ isJust <$> checkDaemon (fromRawFilePath pidfile)
2012-11-12 05:05:04 +00:00
checkshim f = liftIO $ doesFileExist f
notinitialized = do
g <- Annex.gitRepo
liftIO $ cannotStartIn (Git.repoLocation g) "repository has not been initialized by git-annex"
liftIO $ firstRun o
{- If HOME is a git repo, even if it's initialized for git-annex,
2023-03-14 02:39:16 +00:00
- the user almost certainly does not want to run the assistant there. -}
notHome :: Annex Bool
notHome = do
g <- Annex.gitRepo
d <- liftIO $ absPath (Git.repoPath g)
h <- liftIO $ absPath . toRawFilePath =<< myHomeDir
return (d /= h)
{- When run without a repo, start the first available listed repository in
- the autostart file. If none, it's our first time being run! -}
startNoRepo :: WebAppOptions -> IO ()
startNoRepo o = go =<< liftIO (filterM doesDirectoryExist =<< readAutoStartFile)
where
go [] = firstRun o
go (d:ds) = do
v <- tryNonAsync $ do
setCurrentDirectory d
Annex.new =<< Git.CurrentRepo.get
case v of
Left e -> do
cannotStartIn d (show e)
go ds
Right state -> void $ Annex.eval state $ do
whenM (fromRepo Git.repoIsLocalBare) $
giveup $ d ++ " is a bare git repository, cannot run the webapp in it"
avoid flushing keys db queue after each Annex action The flush was only done Annex.run' to make sure that the queue was flushed before git-annex exits. But, doing it there means that as soon as one change gets queued, it gets flushed soon after, which contributes to excessive writes to the database, slowing git-annex down. (This does not yet speed git-annex up, but it is a stepping stone to doing so.) Database queues do not autoflush when garbage collected, so have to be flushed explicitly. I don't think it's possible to make them autoflush (except perhaps if git-annex sqitched to using ResourceT..). The comment in Database.Keys.closeDb used to be accurate, since the automatic flushing did mean that all writes reached the database even when closeDb was not called. But now, closeDb or flushDb needs to be called before stopping using an Annex state. So, removed that comment. In Remote.Git, change to using quiesce everywhere that it used to use stopCoProcesses. This means that uses on onLocal in there are just as slow as before. I considered only calling closeDb on the local git remotes when git-annex exits. But, the reason that Remote.Git calls stopCoProcesses in each onLocal is so as not to leave git processes running that have files open on the remote repo, when it's on removable media. So, it seemed to make sense to also closeDb after each one, since sqlite may also keep files open. Although that has not seemed to cause problems with removable media so far. It was also just easier to quiesce in each onLocal than once at the end. This does likely leave performance on the floor, so could be revisited. In Annex.Content.saveState, there was no reason to close the db, flushing it is enough. The rest of the changes are from auditing for Annex.new, and making sure that quiesce is called, after any action that might possibly need it. After that audit, I'm pretty sure that the change to Annex.run' is safe. The only concern might be that this does let more changes get queued for write to the db, and if git-annex is interrupted, those will be lost. But interrupting git-annex can obviously already prevent it from writing the most recent change to the db, so it must recover from such lost data... right? Sponsored-by: Dartmouth College's Datalad project
2022-10-12 17:50:46 +00:00
r <- callCommandAction $
start' False o
avoid flushing keys db queue after each Annex action The flush was only done Annex.run' to make sure that the queue was flushed before git-annex exits. But, doing it there means that as soon as one change gets queued, it gets flushed soon after, which contributes to excessive writes to the database, slowing git-annex down. (This does not yet speed git-annex up, but it is a stepping stone to doing so.) Database queues do not autoflush when garbage collected, so have to be flushed explicitly. I don't think it's possible to make them autoflush (except perhaps if git-annex sqitched to using ResourceT..). The comment in Database.Keys.closeDb used to be accurate, since the automatic flushing did mean that all writes reached the database even when closeDb was not called. But now, closeDb or flushDb needs to be called before stopping using an Annex state. So, removed that comment. In Remote.Git, change to using quiesce everywhere that it used to use stopCoProcesses. This means that uses on onLocal in there are just as slow as before. I considered only calling closeDb on the local git remotes when git-annex exits. But, the reason that Remote.Git calls stopCoProcesses in each onLocal is so as not to leave git processes running that have files open on the remote repo, when it's on removable media. So, it seemed to make sense to also closeDb after each one, since sqlite may also keep files open. Although that has not seemed to cause problems with removable media so far. It was also just easier to quiesce in each onLocal than once at the end. This does likely leave performance on the floor, so could be revisited. In Annex.Content.saveState, there was no reason to close the db, flushing it is enough. The rest of the changes are from auditing for Annex.new, and making sure that quiesce is called, after any action that might possibly need it. After that audit, I'm pretty sure that the change to Annex.run' is safe. The only concern might be that this does let more changes get queued for write to the db, and if git-annex is interrupted, those will be lost. But interrupting git-annex can obviously already prevent it from writing the most recent change to the db, so it must recover from such lost data... right? Sponsored-by: Dartmouth College's Datalad project
2022-10-12 17:50:46 +00:00
quiesce False
return r
cannotStartIn :: FilePath -> String -> IO ()
cannotStartIn d reason = warningIO $ "unable to start webapp in repository " ++ d ++ ": " ++ reason
{- Run the webapp without a repository, which prompts the user, makes one,
- changes to it, starts the regular assistant, and redirects the
- browser to its url.
-
- This is a very tricky dance -- The first webapp calls the signaler,
- which signals the main thread when it's ok to continue by writing to a
- MVar. The main thread starts the second webapp, and uses its callback
- to write its url back to the MVar, from where the signaler retrieves it,
- returning it to the first webapp, which does the redirect.
-
- Note that it's important that mainthread never terminates! Much
- of this complication is due to needing to keep the mainthread running.
-}
firstRun :: WebAppOptions -> IO ()
firstRun o = do
checkEnvironmentIO
{- Without a repository, we cannot have an Annex monad, so cannot
- get a ThreadState. This is only safe because the
- webapp checks its noAnnex field before accessing the
- threadstate. -}
let st = error "annex state not available"
{- Get a DaemonStatus without running in the Annex monad. -}
dstatus <- atomically . newTVar =<< newDaemonStatus
d <- newAssistantData st dstatus
urlrenderer <- newUrlRenderer
v <- newEmptyMVar
let callback a = Just $ a v
2013-01-26 03:14:32 +00:00
runAssistant d $ do
startNamedThread urlrenderer $
webAppThread d urlrenderer True Nothing
2013-01-26 03:14:32 +00:00
(callback signaler)
(listenAddress o)
2013-01-26 03:14:32 +00:00
(callback mainthread)
waitNamedThreads
2012-11-12 05:05:04 +00:00
where
signaler v = do
putMVar v ""
takeMVar v
mainthread v url htmlshim
| isJust (listenAddress o)= do
putStrLn url
hFlush stdout
go
| otherwise = do
browser <- maybe Nothing webBrowser
<$> catchDefaultIO Nothing Git.Config.global
openBrowser browser htmlshim url Nothing Nothing
go
where
go = do
_wait <- takeMVar v
state <- Annex.new =<< Git.CurrentRepo.get
Annex.eval state $
startDaemon True True Nothing Nothing (listenAddress o) $ Just $
sendurlback v
sendurlback v _origout _origerr url _htmlshim = putMVar v url
openBrowser :: Maybe FilePath -> FilePath -> String -> Maybe Handle -> Maybe Handle -> IO ()
openBrowser mcmd htmlshim realurl outh errh = do
htmlshim' <- fromRawFilePath <$> absPath (toRawFilePath htmlshim)
openBrowser' mcmd htmlshim' realurl outh errh
openBrowser' :: Maybe FilePath -> FilePath -> String -> Maybe Handle -> Maybe Handle -> IO ()
openBrowser' mcmd htmlshim realurl outh errh =
ifM osAndroid
{- Android does not support file:// urls well, but neither
- is the security of the url in the process table important
- there, so just use the real url. -}
( runbrowser realurl
, runbrowser (fileUrl htmlshim)
)
2012-11-12 05:05:04 +00:00
where
runbrowser url = do
let p = case mcmd of
Just c -> proc c [url]
Nothing ->
#ifndef mingw32_HOST_OS
browserProc url
#else
{- Windows hack to avoid using the full path,
- which might contain spaces that cause problems
- for browserProc. -}
(browserProc (takeFileName htmlshim))
{ cwd = Just (takeDirectory htmlshim) }
#endif
hPutStrLn (fromMaybe stdout outh) $ "Launching web browser on " ++ url
hFlush stdout
environ <- cleanStandaloneEnvironment
let p' = p
{ env = environ
, std_out = maybe Inherit UseHandle outh
, std_err = maybe Inherit UseHandle errh
}
withCreateProcess p' $ \_ _ _ pid -> do
exitcode <- waitForProcess pid
unless (exitcode == ExitSuccess) $
hPutStrLn (fromMaybe stderr errh) "failed to start web browser"
{- web.browser is a generic git config setting for a web browser program -}
webBrowser :: Git.Repo -> Maybe FilePath
webBrowser = fmap fromConfigValue <$> Git.Config.getMaybe "web.browser"
fileUrl :: FilePath -> String
fileUrl file = "file://" ++ file