2012-07-23 03:16:56 +00:00
|
|
|
{- git-annex assistant thread to scan remotes to find needed transfers
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Assistant.Threads.TransferScanner where
|
|
|
|
|
|
|
|
import Assistant.Common
|
|
|
|
import Assistant.ScanRemotes
|
|
|
|
import Assistant.TransferQueue
|
|
|
|
import Assistant.ThreadedMonad
|
2012-07-28 22:47:24 +00:00
|
|
|
import Assistant.DaemonStatus
|
2012-07-29 17:22:08 +00:00
|
|
|
import Assistant.Alert
|
2012-07-23 03:16:56 +00:00
|
|
|
import Logs.Transfer
|
2012-07-25 18:15:09 +00:00
|
|
|
import Logs.Location
|
|
|
|
import qualified Remote
|
2012-07-23 03:16:56 +00:00
|
|
|
import Utility.ThreadScheduler
|
2012-07-25 18:15:09 +00:00
|
|
|
import qualified Git.LsFiles as LsFiles
|
|
|
|
import Command
|
|
|
|
import Annex.Content
|
2012-07-23 03:16:56 +00:00
|
|
|
|
|
|
|
thisThread :: ThreadName
|
|
|
|
thisThread = "TransferScanner"
|
|
|
|
|
2012-07-25 17:12:34 +00:00
|
|
|
{- This thread waits until a remote needs to be scanned, to find transfers
|
|
|
|
- that need to be made, to keep data in sync.
|
|
|
|
-}
|
2012-07-28 22:47:24 +00:00
|
|
|
transferScannerThread :: ThreadState -> DaemonStatusHandle -> ScanRemoteMap -> TransferQueue -> IO ()
|
|
|
|
transferScannerThread st dstatus scanremotes transferqueue = do
|
2012-07-23 03:16:56 +00:00
|
|
|
runEvery (Seconds 2) $ do
|
|
|
|
r <- getScanRemote scanremotes
|
2012-07-25 18:15:09 +00:00
|
|
|
liftIO $ debug thisThread ["starting scan of", show r]
|
2012-07-30 06:07:02 +00:00
|
|
|
void $ alertWhile dstatus (scanAlert r) $
|
2012-07-29 17:22:08 +00:00
|
|
|
scan st dstatus transferqueue r
|
2012-07-25 18:15:09 +00:00
|
|
|
liftIO $ debug thisThread ["finished scan of", show r]
|
2012-07-23 03:16:56 +00:00
|
|
|
|
2012-07-25 18:54:09 +00:00
|
|
|
{- This is a naive scan through the git work tree.
|
|
|
|
-
|
|
|
|
- The scan is blocked when the transfer queue gets too large. -}
|
2012-07-30 06:07:02 +00:00
|
|
|
scan :: ThreadState -> DaemonStatusHandle -> TransferQueue -> Remote -> IO Bool
|
2012-07-28 22:47:24 +00:00
|
|
|
scan st dstatus transferqueue r = do
|
2012-07-25 18:54:09 +00:00
|
|
|
g <- runThreadState st $ fromRepo id
|
|
|
|
files <- LsFiles.inRepo [] g
|
|
|
|
go files
|
2012-07-30 06:07:02 +00:00
|
|
|
return True
|
2012-07-25 18:15:09 +00:00
|
|
|
where
|
2012-07-25 18:54:09 +00:00
|
|
|
go [] = return ()
|
|
|
|
go (f:fs) = do
|
|
|
|
v <- runThreadState st $ whenAnnexed check f
|
|
|
|
case v of
|
|
|
|
Nothing -> noop
|
|
|
|
Just t -> do
|
|
|
|
debug thisThread ["queuing", show t]
|
|
|
|
enqueue f t
|
|
|
|
go fs
|
2012-07-25 18:15:09 +00:00
|
|
|
where
|
2012-07-25 18:54:09 +00:00
|
|
|
check _ (key, _) = ifM (inAnnex key)
|
|
|
|
( helper key Upload False =<< remotehas key
|
|
|
|
, helper key Download True =<< remotehas key
|
|
|
|
)
|
|
|
|
helper key direction x y
|
2012-07-25 18:15:09 +00:00
|
|
|
| x == y = return $
|
2012-07-25 18:54:09 +00:00
|
|
|
Just $ Transfer direction u key
|
2012-07-25 18:15:09 +00:00
|
|
|
| otherwise = return Nothing
|
2012-07-25 18:54:09 +00:00
|
|
|
|
|
|
|
u = Remote.uuid r
|
2012-07-28 22:47:24 +00:00
|
|
|
enqueue f t = queueTransferAt smallsize Later transferqueue dstatus (Just f) t r
|
2012-07-25 18:54:09 +00:00
|
|
|
smallsize = 10
|
2012-07-25 18:15:09 +00:00
|
|
|
|
|
|
|
{- Look directly in remote for the key when it's cheap;
|
|
|
|
- otherwise rely on the location log. -}
|
|
|
|
remotehas key
|
|
|
|
| Remote.hasKeyCheap r = (==)
|
|
|
|
<$> pure (Right True)
|
|
|
|
<*> Remote.hasKey r key
|
|
|
|
| otherwise = elem
|
|
|
|
<$> pure u
|
|
|
|
<*> loggedLocations key
|