2012-10-12 05:09:28 +00:00
|
|
|
{- git-annex assistant webapp utilities
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Assistant.WebApp.Utility where
|
|
|
|
|
2013-03-16 04:12:28 +00:00
|
|
|
import Assistant.Common
|
2012-10-12 05:09:28 +00:00
|
|
|
import Assistant.WebApp.Types
|
|
|
|
import Assistant.DaemonStatus
|
|
|
|
import Assistant.TransferQueue
|
2012-10-30 18:34:48 +00:00
|
|
|
import Assistant.Types.TransferSlots
|
2012-10-12 05:09:28 +00:00
|
|
|
import Assistant.TransferSlots
|
|
|
|
import Assistant.Sync
|
|
|
|
import qualified Remote
|
2012-10-12 16:45:16 +00:00
|
|
|
import qualified Types.Remote as Remote
|
|
|
|
import qualified Remote.List as Remote
|
2012-10-12 05:09:28 +00:00
|
|
|
import qualified Assistant.Threads.Transferrer as Transferrer
|
|
|
|
import Logs.Transfer
|
2012-10-12 16:45:16 +00:00
|
|
|
import qualified Config
|
2013-04-23 15:38:52 +00:00
|
|
|
import Config.Files
|
2013-01-27 11:43:05 +00:00
|
|
|
import Git.Config
|
|
|
|
import Assistant.Threads.Watcher
|
|
|
|
import Assistant.NamedThread
|
2012-10-12 05:09:28 +00:00
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import Control.Concurrent
|
|
|
|
import System.Posix.Signals (signalProcessGroup, sigTERM, sigKILL)
|
|
|
|
import System.Posix.Process (getProcessGroupIDOf)
|
|
|
|
|
2013-01-27 11:43:05 +00:00
|
|
|
{- Use Nothing to change autocommit setting; or a remote to change
|
|
|
|
- its sync setting. -}
|
2012-10-12 16:45:16 +00:00
|
|
|
changeSyncable :: (Maybe Remote) -> Bool -> Handler ()
|
2013-03-04 20:36:38 +00:00
|
|
|
changeSyncable Nothing enable = do
|
2013-01-27 11:43:05 +00:00
|
|
|
liftAnnex $ Config.setConfig key (boolConfig enable)
|
|
|
|
liftIO . maybe noop (`throwTo` signal)
|
2013-03-04 20:36:38 +00:00
|
|
|
=<< liftAssistant (namedThreadId watchThread)
|
2013-01-27 11:43:05 +00:00
|
|
|
where
|
|
|
|
key = Config.annexConfig "autocommit"
|
|
|
|
signal
|
|
|
|
| enable = ResumeWatcher
|
|
|
|
| otherwise = PauseWatcher
|
2012-10-12 16:45:16 +00:00
|
|
|
changeSyncable (Just r) True = do
|
|
|
|
changeSyncFlag r True
|
2013-04-08 19:36:09 +00:00
|
|
|
liftAssistant $ syncRemote r
|
2012-10-12 16:45:16 +00:00
|
|
|
changeSyncable (Just r) False = do
|
|
|
|
changeSyncFlag r False
|
2012-10-30 21:14:26 +00:00
|
|
|
liftAssistant $ updateSyncRemotes
|
2012-10-12 05:09:28 +00:00
|
|
|
{- Stop all transfers to or from this remote.
|
|
|
|
- XXX Can't stop any ongoing scan, or git syncs. -}
|
2012-10-30 21:14:26 +00:00
|
|
|
void $ liftAssistant $ dequeueTransfers tofrom
|
2012-10-12 05:09:28 +00:00
|
|
|
mapM_ (cancelTransfer False) =<<
|
|
|
|
filter tofrom . M.keys <$>
|
2012-10-30 21:14:26 +00:00
|
|
|
liftAssistant (currentTransfers <$> getDaemonStatus)
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
tofrom t = transferUUID t == Remote.uuid r
|
2012-10-12 05:09:28 +00:00
|
|
|
|
2012-10-12 16:45:16 +00:00
|
|
|
changeSyncFlag :: Remote -> Bool -> Handler ()
|
2013-03-04 20:36:38 +00:00
|
|
|
changeSyncFlag r enabled = liftAnnex $ do
|
2013-01-27 11:43:05 +00:00
|
|
|
Config.setConfig key (boolConfig enabled)
|
2012-10-12 16:45:16 +00:00
|
|
|
void $ Remote.remoteListRefresh
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
key = Config.remoteConfig (Remote.repo r) "sync"
|
2012-10-12 16:45:16 +00:00
|
|
|
|
2012-10-12 05:09:28 +00:00
|
|
|
pauseTransfer :: Transfer -> Handler ()
|
|
|
|
pauseTransfer = cancelTransfer True
|
|
|
|
|
|
|
|
cancelTransfer :: Bool -> Transfer -> Handler ()
|
|
|
|
cancelTransfer pause t = do
|
|
|
|
m <- getCurrentTransfers
|
2012-10-30 21:14:26 +00:00
|
|
|
unless pause $
|
2012-10-30 19:39:15 +00:00
|
|
|
{- remove queued transfer -}
|
2012-10-30 21:14:26 +00:00
|
|
|
void $ liftAssistant $ dequeueTransfers $ equivilantTransfer t
|
2012-10-30 19:39:15 +00:00
|
|
|
{- stop running transfer -}
|
|
|
|
maybe noop stop (M.lookup t m)
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
stop info = liftAssistant $ do
|
|
|
|
{- When there's a thread associated with the
|
|
|
|
- transfer, it's signaled first, to avoid it
|
|
|
|
- displaying any alert about the transfer having
|
|
|
|
- failed when the transfer process is killed. -}
|
|
|
|
liftIO $ maybe noop signalthread $ transferTid info
|
|
|
|
liftIO $ maybe noop killproc $ transferPid info
|
|
|
|
if pause
|
|
|
|
then void $ alterTransferInfo t $
|
|
|
|
\i -> i { transferPaused = True }
|
|
|
|
else void $ removeTransfer t
|
|
|
|
signalthread tid
|
|
|
|
| pause = throwTo tid PauseTransfer
|
|
|
|
| otherwise = killThread tid
|
|
|
|
{- In order to stop helper processes like rsync,
|
|
|
|
- kill the whole process group of the process running the transfer. -}
|
2013-02-14 18:10:36 +00:00
|
|
|
killproc pid = void $ tryIO $ do
|
2012-10-31 06:34:03 +00:00
|
|
|
g <- getProcessGroupIDOf pid
|
|
|
|
void $ tryIO $ signalProcessGroup sigTERM g
|
|
|
|
threadDelay 50000 -- 0.05 second grace period
|
|
|
|
void $ tryIO $ signalProcessGroup sigKILL g
|
2012-10-12 05:09:28 +00:00
|
|
|
|
|
|
|
startTransfer :: Transfer -> Handler ()
|
|
|
|
startTransfer t = do
|
|
|
|
m <- getCurrentTransfers
|
|
|
|
maybe startqueued go (M.lookup t m)
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
go info = maybe (start info) resume $ transferTid info
|
|
|
|
startqueued = do
|
|
|
|
is <- liftAssistant $ map snd <$> getMatchingTransfers (== t)
|
|
|
|
maybe noop start $ headMaybe is
|
|
|
|
resume tid = do
|
|
|
|
liftAssistant $ alterTransferInfo t $
|
|
|
|
\i -> i { transferPaused = False }
|
|
|
|
liftIO $ throwTo tid ResumeTransfer
|
|
|
|
start info = liftAssistant $ do
|
|
|
|
program <- liftIO readProgramFile
|
2013-03-19 22:46:29 +00:00
|
|
|
inImmediateTransferSlot program $
|
|
|
|
Transferrer.genTransfer t info
|
2012-10-12 05:09:28 +00:00
|
|
|
|
|
|
|
getCurrentTransfers :: Handler TransferMap
|
2012-10-30 21:14:26 +00:00
|
|
|
getCurrentTransfers = currentTransfers <$> liftAssistant getDaemonStatus
|