2013-11-23 19:50:17 +00:00
|
|
|
{- git-annex assistant restarting
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2013 Joey Hess <id@joeyh.name>
|
2013-11-23 19:50:17 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2013-11-23 19:50:17 +00:00
|
|
|
-}
|
|
|
|
|
2013-12-04 19:37:53 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2013-11-23 19:50:17 +00:00
|
|
|
module Assistant.Restart where
|
|
|
|
|
|
|
|
import Assistant.Common
|
|
|
|
import Assistant.Threads.Watcher
|
|
|
|
import Assistant.DaemonStatus
|
|
|
|
import Assistant.NamedThread
|
|
|
|
import Utility.ThreadScheduler
|
|
|
|
import Utility.NotificationBroadcaster
|
|
|
|
import Utility.Url
|
2014-02-11 19:29:56 +00:00
|
|
|
import Utility.PID
|
2020-11-04 18:20:37 +00:00
|
|
|
import qualified Utility.RawFilePath as R
|
2013-11-23 19:50:17 +00:00
|
|
|
import qualified Git.Construct
|
|
|
|
import qualified Git.Config
|
|
|
|
import qualified Annex
|
|
|
|
import qualified Git
|
2015-02-28 21:23:13 +00:00
|
|
|
import Annex.Path
|
2013-11-23 19:50:17 +00:00
|
|
|
|
|
|
|
import Control.Concurrent
|
2013-12-04 19:37:53 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2014-02-11 19:29:56 +00:00
|
|
|
import System.Posix (signalProcess, sigTERM)
|
2013-12-11 04:15:10 +00:00
|
|
|
#else
|
2017-10-25 23:46:28 +00:00
|
|
|
import System.Win32.Process (terminateProcessById)
|
2013-12-04 19:37:53 +00:00
|
|
|
#endif
|
2014-03-01 02:20:54 +00:00
|
|
|
import Network.URI
|
2013-11-23 19:50:17 +00:00
|
|
|
|
|
|
|
{- Before the assistant can be restarted, have to remove our
|
|
|
|
- gitAnnexUrlFile and our gitAnnexPidFile. Pausing the watcher is also
|
|
|
|
- a good idea, to avoid fighting when two assistants are running in the
|
|
|
|
- same repo.
|
|
|
|
-}
|
|
|
|
prepRestart :: Assistant ()
|
|
|
|
prepRestart = do
|
|
|
|
liftIO . maybe noop (`throwTo` PauseWatcher) =<< namedThreadId watchThread
|
2020-11-04 18:20:37 +00:00
|
|
|
liftIO . removeWhenExistsWith R.removeLink =<< liftAnnex (fromRepo gitAnnexUrlFile)
|
|
|
|
liftIO . removeWhenExistsWith R.removeLink =<< liftAnnex (fromRepo gitAnnexPidFile)
|
2013-11-23 19:50:17 +00:00
|
|
|
|
|
|
|
{- To finish a restart, send a global redirect to the new url
|
|
|
|
- to any web browsers that are displaying the webapp.
|
|
|
|
-
|
|
|
|
- Wait for browser to update before terminating this process. -}
|
|
|
|
postRestart :: URLString -> Assistant ()
|
|
|
|
postRestart url = do
|
|
|
|
modifyDaemonStatus_ $ \status -> status { globalRedirUrl = Just url }
|
|
|
|
liftIO . sendNotification . globalRedirNotifier =<< getDaemonStatus
|
|
|
|
void $ liftIO $ forkIO $ do
|
|
|
|
threadDelaySeconds (Seconds 120)
|
2014-09-15 18:45:37 +00:00
|
|
|
terminateSelf
|
|
|
|
|
|
|
|
terminateSelf :: IO ()
|
|
|
|
terminateSelf =
|
2013-12-11 04:15:10 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2014-02-11 19:29:56 +00:00
|
|
|
signalProcess sigTERM =<< getPID
|
2013-12-04 19:37:53 +00:00
|
|
|
#else
|
2017-10-25 23:46:28 +00:00
|
|
|
terminateProcessById =<< getPID
|
2013-12-04 19:37:53 +00:00
|
|
|
#endif
|
2013-11-23 19:50:17 +00:00
|
|
|
|
|
|
|
runRestart :: Assistant URLString
|
|
|
|
runRestart = liftIO . newAssistantUrl
|
|
|
|
=<< liftAnnex (Git.repoLocation <$> Annex.gitRepo)
|
|
|
|
|
|
|
|
{- Starts up the assistant in the repository, and waits for it to create
|
|
|
|
- a gitAnnexUrlFile. Waits for the assistant to be up and listening for
|
|
|
|
- connections by testing the url. -}
|
|
|
|
newAssistantUrl :: FilePath -> IO URLString
|
|
|
|
newAssistantUrl repo = do
|
|
|
|
startAssistant repo
|
|
|
|
geturl
|
|
|
|
where
|
|
|
|
geturl = do
|
2020-11-04 18:20:37 +00:00
|
|
|
r <- Git.Config.read =<< Git.Construct.fromPath (toRawFilePath repo)
|
|
|
|
waiturl $ fromRawFilePath $ gitAnnexUrlFile r
|
2013-11-23 19:50:17 +00:00
|
|
|
waiturl urlfile = do
|
|
|
|
v <- tryIO $ readFile urlfile
|
|
|
|
case v of
|
|
|
|
Left _ -> delayed $ waiturl urlfile
|
2014-03-01 02:20:54 +00:00
|
|
|
Right url -> ifM (assistantListening url)
|
2013-11-23 19:50:17 +00:00
|
|
|
( return url
|
|
|
|
, delayed $ waiturl urlfile
|
|
|
|
)
|
|
|
|
delayed a = do
|
|
|
|
threadDelay 100000 -- 1/10th of a second
|
|
|
|
a
|
|
|
|
|
2014-03-01 02:20:54 +00:00
|
|
|
{- Checks if the assistant is listening on an url.
|
|
|
|
-
|
2016-06-02 01:46:58 +00:00
|
|
|
- Always checks http, because https with self-signed cert is problematic.
|
2014-03-01 02:20:54 +00:00
|
|
|
- warp-tls listens to http, in order to show an error page, so this works.
|
|
|
|
-}
|
|
|
|
assistantListening :: URLString -> IO Bool
|
2019-11-12 17:33:41 +00:00
|
|
|
assistantListening url = catchBoolIO $ do
|
|
|
|
uo <- defUrlOptions
|
|
|
|
(== Right True) <$> exists url' uo
|
2014-03-01 02:20:54 +00:00
|
|
|
where
|
2023-03-27 17:38:02 +00:00
|
|
|
url' = case parseURIPortable url of
|
2014-03-01 02:20:54 +00:00
|
|
|
Nothing -> url
|
|
|
|
Just uri -> show $ uri
|
|
|
|
{ uriScheme = "http:"
|
|
|
|
}
|
|
|
|
|
2014-02-13 16:55:27 +00:00
|
|
|
{- Does not wait for assistant to be listening for web connections.
|
|
|
|
-
|
|
|
|
- On windows, the assistant does not daemonize, which is why the forkIO is
|
|
|
|
- done.
|
|
|
|
-}
|
2013-11-23 19:50:17 +00:00
|
|
|
startAssistant :: FilePath -> IO ()
|
2014-02-13 16:55:27 +00:00
|
|
|
startAssistant repo = void $ forkIO $ do
|
2015-02-28 21:23:13 +00:00
|
|
|
program <- programPath
|
2020-06-04 16:13:26 +00:00
|
|
|
let p = (proc program ["assistant"]) { cwd = Just repo }
|
|
|
|
withCreateProcess p $ \_ _ _ pid -> void $ checkSuccessProcess pid
|