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.DaemonStatus
|
|
|
|
import Assistant.TransferQueue
|
2012-07-06 20:39:07 +00:00
|
|
|
import Assistant.TransferSlots
|
2012-08-06 21:09:23 +00:00
|
|
|
import Assistant.Alert
|
ensure that git-annex branch is pushed after a successful transfer
I now have this topology working:
assistant ---> {bare repo, special remote} <--- assistant
And, I think, also this one:
+----------- bare repo --------+
v v
assistant ---> special remote <--- assistant
While before with assistant <---> assistant connections, both sides got
location info updated after a transfer, in this topology, the bare repo
*might* get its location info updated, but the other assistant has no way to
know that it did. And a special remote doesn't record location info,
so transfers to it won't propigate out location log changes at all.
So, for these to work, after a transfer succeeds, the git-annex branch
needs to be pushed. This is done by recording a synthetic commit has
occurred, which lets the pusher handle pushing out the change (which will
include actually committing any still journalled changes to the git-annex
branch).
Of course, this means rather a lot more syncing action than happened
before. At least the pusher bundles together very close together pushes,
somewhat. Currently it just waits 2 seconds between each push.
2012-10-28 20:05:34 +00:00
|
|
|
import Assistant.Commits
|
2012-07-05 20:34:20 +00:00
|
|
|
import Logs.Transfer
|
2012-07-07 16:50:20 +00:00
|
|
|
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-08-24 21:23:58 +00:00
|
|
|
import Types.Key
|
2012-08-27 17:43:03 +00:00
|
|
|
import Locations.UserConfig
|
2012-07-05 20:34:20 +00:00
|
|
|
|
2012-08-10 19:45:00 +00:00
|
|
|
import System.Process (create_group)
|
2012-07-05 20:34:20 +00:00
|
|
|
|
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-10-29 18:07:12 +00:00
|
|
|
transfererThread :: NamedThread
|
|
|
|
transfererThread = NamedThread "Transferr" $ do
|
|
|
|
program <- liftIO readProgramFile
|
|
|
|
transferqueue <- getAssistant transferQueue
|
|
|
|
dstatus <- getAssistant daemonStatusHandle
|
|
|
|
slots <- getAssistant transferSlots
|
|
|
|
starter <- asIO2 $ startTransfer program
|
|
|
|
liftIO $ forever $ inTransferSlot dstatus slots $
|
|
|
|
maybe (return Nothing) (uncurry starter)
|
|
|
|
=<< getNextTransfer transferqueue dstatus notrunning
|
|
|
|
where
|
|
|
|
{- Skip transfers that are already running. -}
|
|
|
|
notrunning = isNothing . startedTime
|
2012-07-05 20:34:20 +00:00
|
|
|
|
2012-08-29 21:32:41 +00:00
|
|
|
{- By the time this is called, the daemonstatus's transfer map should
|
2012-08-29 20:30:40 +00:00
|
|
|
- already have been updated to include the transfer. -}
|
2012-10-29 18:07:12 +00:00
|
|
|
startTransfer :: FilePath -> Transfer -> TransferInfo -> Assistant (Maybe (Transfer, TransferInfo, IO ()))
|
|
|
|
startTransfer program t info = case (transferRemote info, associatedFile info) of
|
|
|
|
(Just remote, Just file) -> ifM (liftAnnex $ shouldTransfer t info)
|
2012-08-29 20:30:40 +00:00
|
|
|
( do
|
2012-10-29 18:07:12 +00:00
|
|
|
debug [ "Transferring:" , show t ]
|
|
|
|
notifyTransfer <<~ daemonStatusHandle
|
|
|
|
tp <- asIO2 transferprocess
|
|
|
|
return $ Just (t, info, tp remote file)
|
2012-08-29 20:30:40 +00:00
|
|
|
, do
|
2012-10-29 18:07:12 +00:00
|
|
|
debug [ "Skipping unnecessary transfer:" , show t ]
|
|
|
|
void $ flip removeTransfer t <<~ daemonStatusHandle
|
2012-08-29 20:30:40 +00:00
|
|
|
return Nothing
|
|
|
|
)
|
|
|
|
_ -> return Nothing
|
2012-10-29 18:07:12 +00:00
|
|
|
where
|
|
|
|
direction = transferDirection t
|
|
|
|
isdownload = direction == Download
|
2012-07-07 16:50:20 +00:00
|
|
|
|
2012-10-29 18:07:12 +00:00
|
|
|
transferprocess remote file = void $ do
|
|
|
|
(_, _, _, pid)
|
|
|
|
<- liftIO $ createProcess (proc program $ toCommand params)
|
|
|
|
{ create_group = True }
|
|
|
|
{- Alerts are only shown for successful transfers.
|
|
|
|
- Transfers can temporarily fail for many reasons,
|
|
|
|
- so there's no point in bothering the user about
|
|
|
|
- those. The assistant should recover.
|
|
|
|
-
|
|
|
|
- Also, after a successful transfer, the location
|
|
|
|
- log has changed. Indicate that a commit has been
|
|
|
|
- made, in order to queue a push of the git-annex
|
|
|
|
- branch out to remotes that did not participate
|
|
|
|
- in the transfer.
|
|
|
|
-}
|
|
|
|
whenM (liftIO $ (==) ExitSuccess <$> waitForProcess pid) $ do
|
|
|
|
dstatus <- getAssistant daemonStatusHandle
|
|
|
|
liftIO $ void $ addAlert dstatus $
|
|
|
|
makeAlertFiller True $
|
|
|
|
transferFileAlert direction True file
|
2012-10-29 23:35:18 +00:00
|
|
|
recordCommit
|
2012-10-29 18:07:12 +00:00
|
|
|
where
|
|
|
|
params =
|
|
|
|
[ Param "transferkey"
|
|
|
|
, Param "--quiet"
|
|
|
|
, Param $ key2file $ transferKey t
|
|
|
|
, Param $ if isdownload
|
|
|
|
then "--from"
|
|
|
|
else "--to"
|
|
|
|
, Param $ Remote.name remote
|
|
|
|
, Param "--file"
|
|
|
|
, File file
|
|
|
|
]
|
2012-08-29 20:30:40 +00:00
|
|
|
|
|
|
|
{- Checks if the file to download is already present, or the remote
|
|
|
|
- being uploaded to isn't known to have the file. -}
|
|
|
|
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
|
|
|
|
where
|
|
|
|
key = transferKey t
|