2012-07-05 20:34:20 +00:00
|
|
|
{- git-annex assistant data transferrer thread
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Assistant.Threads.Transferrer where
|
|
|
|
|
2012-07-20 23:29:59 +00:00
|
|
|
import Assistant.Common
|
2012-07-05 20:34:20 +00:00
|
|
|
import Assistant.ThreadedMonad
|
|
|
|
import Assistant.DaemonStatus
|
|
|
|
import Assistant.TransferQueue
|
2012-07-06 20:39:07 +00:00
|
|
|
import Assistant.TransferSlots
|
2012-07-05 20:34:20 +00:00
|
|
|
import Logs.Transfer
|
2012-07-07 16:50:20 +00:00
|
|
|
import Logs.Presence
|
|
|
|
import Logs.Location
|
2012-07-05 20:34:20 +00:00
|
|
|
import Annex.Content
|
2012-07-07 16:50:20 +00:00
|
|
|
import qualified Remote
|
2012-07-05 20:34:20 +00:00
|
|
|
|
2012-07-18 22:42:41 +00:00
|
|
|
import Data.Time.Clock.POSIX
|
2012-07-05 20:34:20 +00:00
|
|
|
import Data.Time.Clock
|
|
|
|
import qualified Data.Map as M
|
|
|
|
|
2012-07-20 23:29:59 +00:00
|
|
|
thisThread :: ThreadName
|
|
|
|
thisThread = "Transferrer"
|
|
|
|
|
2012-07-06 00:57:06 +00:00
|
|
|
{- For now only one transfer is run at a time. -}
|
|
|
|
maxTransfers :: Int
|
|
|
|
maxTransfers = 1
|
|
|
|
|
|
|
|
{- Dispatches transfers from the queue. -}
|
2012-07-06 20:39:07 +00:00
|
|
|
transfererThread :: ThreadState -> DaemonStatusHandle -> TransferQueue -> TransferSlots -> IO ()
|
2012-07-07 00:48:51 +00:00
|
|
|
transfererThread st dstatus transferqueue slots = go
|
|
|
|
where
|
2012-07-29 17:37:26 +00:00
|
|
|
go = getNextTransfer transferqueue dstatus notrunning >>= handle
|
|
|
|
handle Nothing = go
|
|
|
|
handle (Just (t, info)) = do
|
|
|
|
ifM (runThreadState st $ shouldTransfer t info)
|
2012-07-20 23:29:59 +00:00
|
|
|
( do
|
|
|
|
debug thisThread [ "Transferring:" , show t ]
|
2012-07-29 12:52:57 +00:00
|
|
|
notifyTransfer dstatus
|
2012-07-27 15:47:34 +00:00
|
|
|
transferThread st dstatus slots t info
|
2012-07-28 22:47:24 +00:00
|
|
|
, do
|
|
|
|
debug thisThread [ "Skipping unnecessary transfer:" , show t ]
|
|
|
|
-- getNextTransfer added t to the
|
|
|
|
-- daemonstatus's transfer map.
|
|
|
|
void $ removeTransfer dstatus t
|
2012-07-20 23:29:59 +00:00
|
|
|
)
|
2012-07-07 00:48:51 +00:00
|
|
|
go
|
2012-07-29 17:37:26 +00:00
|
|
|
{- Skip transfers that are already running. -}
|
|
|
|
notrunning i = startedTime i == Nothing
|
2012-07-05 20:34:20 +00:00
|
|
|
|
2012-07-29 17:37:26 +00:00
|
|
|
{- Checks if the file to download is already present, or the remote
|
2012-07-17 16:06:35 +00:00
|
|
|
- being uploaded to isn't known to have the file. -}
|
2012-07-29 17:37:26 +00:00
|
|
|
shouldTransfer :: Transfer -> TransferInfo -> Annex Bool
|
|
|
|
shouldTransfer t info
|
|
|
|
| transferDirection t == Download =
|
|
|
|
not <$> inAnnex key
|
|
|
|
| transferDirection t == Upload =
|
|
|
|
{- Trust the location log to check if the
|
|
|
|
- remote already has the key. This avoids
|
|
|
|
- a roundtrip to the remote. -}
|
|
|
|
case transferRemote info of
|
|
|
|
Nothing -> return False
|
|
|
|
Just remote ->
|
|
|
|
notElem (Remote.uuid remote)
|
|
|
|
<$> loggedLocations key
|
|
|
|
| otherwise = return False
|
2012-07-06 00:57:06 +00:00
|
|
|
where
|
2012-07-17 16:06:35 +00:00
|
|
|
key = transferKey t
|
2012-07-05 20:34:20 +00:00
|
|
|
|
2012-07-18 22:42:41 +00:00
|
|
|
{- A transfer is run in a separate thread, with a *copy* of the Annex
|
2012-07-05 20:34:20 +00:00
|
|
|
- state. This is necessary to avoid blocking the rest of the assistant
|
|
|
|
- on the transfer completing, and also to allow multiple transfers to run
|
2012-07-18 22:42:41 +00:00
|
|
|
- at once. This requires GHC's threaded runtime to work!
|
2012-07-05 20:34:20 +00:00
|
|
|
-
|
2012-07-18 22:42:41 +00:00
|
|
|
- The copy of state means that the transfer processes are responsible
|
2012-07-05 20:34:20 +00:00
|
|
|
- for doing any necessary shutdown cleanups, and that the parent
|
2012-07-06 00:57:06 +00:00
|
|
|
- thread's cache must be invalidated once a transfer completes, as
|
2012-07-07 16:50:20 +00:00
|
|
|
- changes may have been made to the git-annex branch.
|
2012-07-05 20:34:20 +00:00
|
|
|
-}
|
2012-07-27 15:47:34 +00:00
|
|
|
transferThread :: ThreadState -> DaemonStatusHandle -> TransferSlots -> Transfer -> TransferInfo -> IO ()
|
|
|
|
transferThread st dstatus slots t info = case (transferRemote info, associatedFile info) of
|
2012-07-07 16:50:20 +00:00
|
|
|
(Nothing, _) -> noop
|
|
|
|
(_, Nothing) -> noop
|
2012-07-17 16:06:35 +00:00
|
|
|
(Just remote, Just file) -> do
|
2012-07-18 23:13:56 +00:00
|
|
|
tid <- inTransferSlot slots st $
|
|
|
|
transferprocess remote file
|
2012-07-07 16:50:20 +00:00
|
|
|
now <- getCurrentTime
|
2012-07-28 22:02:11 +00:00
|
|
|
adjustTransfers dstatus $
|
2012-07-07 16:50:20 +00:00
|
|
|
M.insertWith' const t info
|
2012-07-18 22:42:41 +00:00
|
|
|
{ startedTime = Just $ utcTimeToPOSIXSeconds now
|
|
|
|
, transferTid = Just tid
|
2012-07-07 16:50:20 +00:00
|
|
|
}
|
2012-07-05 20:34:20 +00:00
|
|
|
where
|
2012-07-07 16:50:20 +00:00
|
|
|
isdownload = transferDirection t == Download
|
|
|
|
tofrom
|
|
|
|
| isdownload = "from"
|
|
|
|
| otherwise = "to"
|
|
|
|
key = transferKey t
|
|
|
|
|
|
|
|
transferprocess remote file = do
|
|
|
|
showStart "copy" file
|
|
|
|
showAction $ tofrom ++ " " ++ Remote.name remote
|
2012-07-27 15:47:34 +00:00
|
|
|
ok <- runTransfer t (Just file) $
|
2012-07-07 16:50:20 +00:00
|
|
|
if isdownload
|
|
|
|
then getViaTmp key $
|
|
|
|
Remote.retrieveKeyFile remote key (Just file)
|
|
|
|
else do
|
|
|
|
ok <- Remote.storeKey remote key $ Just file
|
|
|
|
when ok $
|
|
|
|
Remote.logStatus remote key InfoPresent
|
|
|
|
return ok
|
|
|
|
showEndResult ok
|