2013-01-03 19:16:40 +00:00
|
|
|
{- git-annex assistant webapp control
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP, TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings, RankNTypes #-}
|
|
|
|
|
|
|
|
module Assistant.WebApp.Control where
|
|
|
|
|
|
|
|
import Assistant.WebApp.Common
|
2013-04-23 15:38:52 +00:00
|
|
|
import Config.Files
|
2013-01-15 17:52:35 +00:00
|
|
|
import Utility.LogFile
|
2013-01-26 06:09:33 +00:00
|
|
|
import Assistant.DaemonStatus
|
2013-04-10 21:52:04 +00:00
|
|
|
import Assistant.Alert
|
2013-10-26 20:54:49 +00:00
|
|
|
import Assistant.TransferSlots
|
2013-01-03 19:16:40 +00:00
|
|
|
|
|
|
|
import Control.Concurrent
|
|
|
|
import System.Posix (getProcessID, signalProcess, sigTERM)
|
2013-01-26 06:09:33 +00:00
|
|
|
import qualified Data.Map as M
|
2013-01-03 19:16:40 +00:00
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
getShutdownR :: Handler Html
|
2013-01-03 19:16:40 +00:00
|
|
|
getShutdownR = page "Shutdown" Nothing $
|
|
|
|
$(widgetFile "control/shutdown")
|
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
getShutdownConfirmedR :: Handler Html
|
2013-04-10 21:38:53 +00:00
|
|
|
getShutdownConfirmedR = do
|
2013-04-10 21:52:04 +00:00
|
|
|
liftAssistant $ do
|
2013-10-26 20:54:49 +00:00
|
|
|
{- Remove all alerts for currently running activities. -}
|
2013-04-10 21:52:04 +00:00
|
|
|
updateAlertMap $ M.filter $ \a -> alertClass a /= Activity
|
|
|
|
void $ addAlert shutdownAlert
|
2013-10-26 20:54:49 +00:00
|
|
|
{- Stop transfers the assistant is running,
|
|
|
|
- otherwise they would continue past shutdown.
|
|
|
|
- Pausing transfers prevents more being started up (and stops
|
|
|
|
- the transfer processes). -}
|
|
|
|
ts <- M.keys . currentTransfers <$> getDaemonStatus
|
|
|
|
mapM_ pauseTransfer ts
|
2013-04-10 21:38:53 +00:00
|
|
|
page "Shutdown" Nothing $ do
|
|
|
|
{- Wait 2 seconds before shutting down, to give the web
|
|
|
|
- page time to load in the browser. -}
|
|
|
|
void $ liftIO $ forkIO $ do
|
|
|
|
threadDelay 2000000
|
|
|
|
signalProcess sigTERM =<< getProcessID
|
|
|
|
$(widgetFile "control/shutdownconfirmed")
|
2013-01-03 22:50:30 +00:00
|
|
|
|
|
|
|
{- Quite a hack, and doesn't redirect the browser window. -}
|
2013-06-27 05:15:28 +00:00
|
|
|
getRestartR :: Handler Html
|
2013-01-03 22:50:30 +00:00
|
|
|
getRestartR = page "Restarting" Nothing $ do
|
|
|
|
void $ liftIO $ forkIO $ do
|
|
|
|
threadDelay 2000000
|
|
|
|
program <- readProgramFile
|
|
|
|
unlessM (boolSystem "sh" [Param "-c", Param $ restartcommand program]) $
|
|
|
|
error "restart failed"
|
|
|
|
$(widgetFile "control/restarting")
|
|
|
|
where
|
2013-07-23 21:31:51 +00:00
|
|
|
restartcommand program = program ++ " assistant --stop; exec " ++
|
2013-01-03 22:50:30 +00:00
|
|
|
program ++ " webapp"
|
2013-01-15 17:52:35 +00:00
|
|
|
|
2013-01-26 06:09:33 +00:00
|
|
|
getRestartThreadR :: ThreadName -> Handler ()
|
|
|
|
getRestartThreadR name = do
|
|
|
|
m <- liftAssistant $ startedThreads <$> getDaemonStatus
|
|
|
|
liftIO $ maybe noop snd $ M.lookup name m
|
|
|
|
redirectBack
|
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
getLogR :: Handler Html
|
2013-01-15 17:52:35 +00:00
|
|
|
getLogR = page "Logs" Nothing $ do
|
2013-03-16 04:12:28 +00:00
|
|
|
logfile <- liftAnnex $ fromRepo gitAnnexLogFile
|
2013-01-15 17:52:35 +00:00
|
|
|
logs <- liftIO $ listLogs logfile
|
|
|
|
logcontent <- liftIO $ concat <$> mapM readFile logs
|
|
|
|
$(widgetFile "control/log")
|