2012-06-22 21:01:08 +00:00
|
|
|
{- git-annex assistant git merge thread
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
2012-06-23 05:20:40 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
2012-06-22 21:01:08 +00:00
|
|
|
-}
|
|
|
|
|
2012-08-26 18:14:12 +00:00
|
|
|
module Assistant.Threads.Merger where
|
2012-06-22 21:01:08 +00:00
|
|
|
|
2012-07-20 23:29:59 +00:00
|
|
|
import Assistant.Common
|
2012-09-18 01:05:50 +00:00
|
|
|
import Assistant.TransferQueue
|
add ConfigMonitor thread
Monitors git-annex branch for changes, which are noticed by the Merger
thread whenever the branch ref is changed (either due to an incoming push,
or a local change), and refreshes cached config values for modified config
files.
Rate limited to run no more often than once per minute. This is important
because frequent git-annex branch changes happen when files are being
added, or transferred, etc.
A primary use case is that, when preferred content changes are made,
and get pushed to remotes, the remotes start honoring those settings.
Other use cases include propigating repository description and trust
changes to remotes, and learning when a remote has added a new special
remote, so the webapp can present the GUI to enable that special remote
locally.
Also added a uuid.log cache. All other config files already had caches.
2012-10-20 20:37:06 +00:00
|
|
|
import Assistant.BranchChange
|
assistant: Get back in sync with XMPP remotes after network reconnection, and on startup.
Make manualPull send push requests over XMPP.
When reconnecting with remotes, those that are XMPP remotes cannot
immediately be pulled from and scanned, so instead maintain a set of
(probably) desynced remotes, and put XMPP remotes on it. (This set could be
used in other ways later, if we can detect we're out of sync with other
types of remotes.)
The merger handles detecting when a XMPP push is received from a desynced
remote, and triggers a scan then, if they have in fact diverged.
This has one known bug: A single XMPP remote can have multiple clients
behind it. When this happens, only the UUID of one client is recorded
as the UUID of the XMPP remote. Pushes from the other XMPP clients will not
trigger a scan. If the client whose UUID is expected responds to the push
request, it'll work, but when that client is offline, we're SOL.
2013-03-06 19:09:31 +00:00
|
|
|
import Assistant.DaemonStatus
|
|
|
|
import Assistant.ScanRemotes
|
2012-06-22 21:01:08 +00:00
|
|
|
import Utility.DirWatcher
|
2013-03-11 02:24:13 +00:00
|
|
|
import Utility.DirWatcher.Types
|
2012-09-16 22:53:13 +00:00
|
|
|
import qualified Annex.Branch
|
2012-06-22 21:01:08 +00:00
|
|
|
import qualified Git
|
|
|
|
import qualified Git.Branch
|
2012-12-25 18:10:07 +00:00
|
|
|
import qualified Command.Sync
|
assistant: Get back in sync with XMPP remotes after network reconnection, and on startup.
Make manualPull send push requests over XMPP.
When reconnecting with remotes, those that are XMPP remotes cannot
immediately be pulled from and scanned, so instead maintain a set of
(probably) desynced remotes, and put XMPP remotes on it. (This set could be
used in other ways later, if we can detect we're out of sync with other
types of remotes.)
The merger handles detecting when a XMPP push is received from a desynced
remote, and triggers a scan then, if they have in fact diverged.
This has one known bug: A single XMPP remote can have multiple clients
behind it. When this happens, only the UUID of one client is recorded
as the UUID of the XMPP remote. Pushes from the other XMPP clients will not
trigger a scan. If the client whose UUID is expected responds to the push
request, it'll work, but when that client is offline, we're SOL.
2013-03-06 19:09:31 +00:00
|
|
|
import Annex.TaggedPush
|
|
|
|
import Remote (remoteFromUUID)
|
|
|
|
|
|
|
|
import qualified Data.Set as S
|
2013-03-06 20:29:19 +00:00
|
|
|
import qualified Data.Text as T
|
2012-06-22 21:01:08 +00:00
|
|
|
|
2012-09-18 01:05:50 +00:00
|
|
|
{- This thread watches for changes to .git/refs/, and handles incoming
|
|
|
|
- pushes. -}
|
2012-10-29 15:40:22 +00:00
|
|
|
mergeThread :: NamedThread
|
2013-01-26 06:09:33 +00:00
|
|
|
mergeThread = namedThread "Merger" $ do
|
2012-10-29 15:40:22 +00:00
|
|
|
g <- liftAnnex gitRepo
|
2012-09-17 03:09:08 +00:00
|
|
|
let dir = Git.localGitDir g </> "refs"
|
2012-10-29 15:40:22 +00:00
|
|
|
liftIO $ createDirectoryIfMissing True dir
|
|
|
|
let hook a = Just <$> asIO2 (runHandler a)
|
2013-04-30 20:35:55 +00:00
|
|
|
changehook <- hook onChange
|
2012-10-29 15:40:22 +00:00
|
|
|
errhook <- hook onErr
|
2012-06-22 21:01:08 +00:00
|
|
|
let hooks = mkWatchHooks
|
2013-04-30 20:35:55 +00:00
|
|
|
{ addHook = changehook
|
|
|
|
, modifyHook = changehook
|
2012-10-29 15:40:22 +00:00
|
|
|
, errHook = errhook
|
2012-06-22 21:01:08 +00:00
|
|
|
}
|
2012-10-29 15:40:22 +00:00
|
|
|
void $ liftIO $ watchDir dir (const False) hooks id
|
|
|
|
debug ["watching", dir]
|
2012-06-22 21:01:08 +00:00
|
|
|
|
2012-10-29 15:40:22 +00:00
|
|
|
type Handler = FilePath -> Assistant ()
|
2012-06-22 21:01:08 +00:00
|
|
|
|
|
|
|
{- Runs an action handler.
|
|
|
|
-
|
|
|
|
- Exceptions are ignored, otherwise a whole thread could be crashed.
|
|
|
|
-}
|
2012-10-29 15:40:22 +00:00
|
|
|
runHandler :: Handler -> FilePath -> Maybe FileStatus -> Assistant ()
|
|
|
|
runHandler handler file _filestatus =
|
|
|
|
either (liftIO . print) (const noop) =<< tryIO <~> handler file
|
2012-06-22 21:01:08 +00:00
|
|
|
|
|
|
|
{- Called when there's an error with inotify. -}
|
|
|
|
onErr :: Handler
|
2012-10-29 15:40:22 +00:00
|
|
|
onErr msg = error msg
|
2012-06-22 21:01:08 +00:00
|
|
|
|
2013-04-30 20:35:55 +00:00
|
|
|
{- Called when a new branch ref is written, or a branch ref is modified.
|
2012-06-22 21:01:08 +00:00
|
|
|
-
|
|
|
|
- At startup, synthetic add events fire, causing this to run, but that's
|
|
|
|
- ok; it ensures that any changes pushed since the last time the assistant
|
|
|
|
- ran are merged in.
|
|
|
|
-}
|
2013-04-30 20:35:55 +00:00
|
|
|
onChange :: Handler
|
|
|
|
onChange file
|
2012-06-22 21:01:08 +00:00
|
|
|
| ".lock" `isSuffixOf` file = noop
|
add ConfigMonitor thread
Monitors git-annex branch for changes, which are noticed by the Merger
thread whenever the branch ref is changed (either due to an incoming push,
or a local change), and refreshes cached config values for modified config
files.
Rate limited to run no more often than once per minute. This is important
because frequent git-annex branch changes happen when files are being
added, or transferred, etc.
A primary use case is that, when preferred content changes are made,
and get pushed to remotes, the remotes start honoring those settings.
Other use cases include propigating repository description and trust
changes to remotes, and learning when a remote has added a new special
remote, so the webapp can present the GUI to enable that special remote
locally.
Also added a uuid.log cache. All other config files already had caches.
2012-10-20 20:37:06 +00:00
|
|
|
| isAnnexBranch file = do
|
2012-10-29 23:20:54 +00:00
|
|
|
branchChanged
|
assistant: Get back in sync with XMPP remotes after network reconnection, and on startup.
Make manualPull send push requests over XMPP.
When reconnecting with remotes, those that are XMPP remotes cannot
immediately be pulled from and scanned, so instead maintain a set of
(probably) desynced remotes, and put XMPP remotes on it. (This set could be
used in other ways later, if we can detect we're out of sync with other
types of remotes.)
The merger handles detecting when a XMPP push is received from a desynced
remote, and triggers a scan then, if they have in fact diverged.
This has one known bug: A single XMPP remote can have multiple clients
behind it. When this happens, only the UUID of one client is recorded
as the UUID of the XMPP remote. Pushes from the other XMPP clients will not
trigger a scan. If the client whose UUID is expected responds to the push
request, it'll work, but when that client is offline, we're SOL.
2013-03-06 19:09:31 +00:00
|
|
|
diverged <- liftAnnex Annex.Branch.forceUpdate
|
|
|
|
when diverged $
|
|
|
|
unlessM handleDesynced $
|
|
|
|
queueDeferredDownloads "retrying deferred download" Later
|
2013-04-30 20:35:55 +00:00
|
|
|
| "/synced/" `isInfixOf` file =
|
2012-10-29 15:40:22 +00:00
|
|
|
mergecurrent =<< liftAnnex (inRepo Git.Branch.current)
|
2012-09-16 22:53:13 +00:00
|
|
|
| otherwise = noop
|
2012-10-29 15:40:22 +00:00
|
|
|
where
|
|
|
|
changedbranch = fileToBranch file
|
assistant: Get back in sync with XMPP remotes after network reconnection, and on startup.
Make manualPull send push requests over XMPP.
When reconnecting with remotes, those that are XMPP remotes cannot
immediately be pulled from and scanned, so instead maintain a set of
(probably) desynced remotes, and put XMPP remotes on it. (This set could be
used in other ways later, if we can detect we're out of sync with other
types of remotes.)
The merger handles detecting when a XMPP push is received from a desynced
remote, and triggers a scan then, if they have in fact diverged.
This has one known bug: A single XMPP remote can have multiple clients
behind it. When this happens, only the UUID of one client is recorded
as the UUID of the XMPP remote. Pushes from the other XMPP clients will not
trigger a scan. If the client whose UUID is expected responds to the push
request, it'll work, but when that client is offline, we're SOL.
2013-03-06 19:09:31 +00:00
|
|
|
|
2012-10-29 15:40:22 +00:00
|
|
|
mergecurrent (Just current)
|
|
|
|
| equivBranches changedbranch current = do
|
|
|
|
debug
|
|
|
|
[ "merging", show changedbranch
|
|
|
|
, "into", show current
|
|
|
|
]
|
2012-12-25 18:10:07 +00:00
|
|
|
void $ liftAnnex $ Command.Sync.mergeFrom changedbranch
|
2012-10-29 15:40:22 +00:00
|
|
|
mergecurrent _ = noop
|
2012-09-16 22:53:13 +00:00
|
|
|
|
2013-03-06 20:29:19 +00:00
|
|
|
handleDesynced = case fromTaggedBranch changedbranch of
|
assistant: Get back in sync with XMPP remotes after network reconnection, and on startup.
Make manualPull send push requests over XMPP.
When reconnecting with remotes, those that are XMPP remotes cannot
immediately be pulled from and scanned, so instead maintain a set of
(probably) desynced remotes, and put XMPP remotes on it. (This set could be
used in other ways later, if we can detect we're out of sync with other
types of remotes.)
The merger handles detecting when a XMPP push is received from a desynced
remote, and triggers a scan then, if they have in fact diverged.
This has one known bug: A single XMPP remote can have multiple clients
behind it. When this happens, only the UUID of one client is recorded
as the UUID of the XMPP remote. Pushes from the other XMPP clients will not
trigger a scan. If the client whose UUID is expected responds to the push
request, it'll work, but when that client is offline, we're SOL.
2013-03-06 19:09:31 +00:00
|
|
|
Nothing -> return False
|
2013-03-06 20:29:19 +00:00
|
|
|
Just (u, info) -> do
|
|
|
|
mr <- liftAnnex $ remoteFromUUID u
|
|
|
|
case mr of
|
|
|
|
Nothing -> return False
|
|
|
|
Just r -> do
|
|
|
|
s <- desynced <$> getDaemonStatus
|
|
|
|
if S.member u s || Just (T.unpack $ getXMPPClientID r) == info
|
|
|
|
then do
|
|
|
|
modifyDaemonStatus_ $ \st -> st
|
|
|
|
{ desynced = S.delete u s }
|
assistant: Get back in sync with XMPP remotes after network reconnection, and on startup.
Make manualPull send push requests over XMPP.
When reconnecting with remotes, those that are XMPP remotes cannot
immediately be pulled from and scanned, so instead maintain a set of
(probably) desynced remotes, and put XMPP remotes on it. (This set could be
used in other ways later, if we can detect we're out of sync with other
types of remotes.)
The merger handles detecting when a XMPP push is received from a desynced
remote, and triggers a scan then, if they have in fact diverged.
This has one known bug: A single XMPP remote can have multiple clients
behind it. When this happens, only the UUID of one client is recorded
as the UUID of the XMPP remote. Pushes from the other XMPP clients will not
trigger a scan. If the client whose UUID is expected responds to the push
request, it'll work, but when that client is offline, we're SOL.
2013-03-06 19:09:31 +00:00
|
|
|
addScanRemotes True [r]
|
|
|
|
return True
|
2013-03-06 20:29:19 +00:00
|
|
|
else return False
|
assistant: Get back in sync with XMPP remotes after network reconnection, and on startup.
Make manualPull send push requests over XMPP.
When reconnecting with remotes, those that are XMPP remotes cannot
immediately be pulled from and scanned, so instead maintain a set of
(probably) desynced remotes, and put XMPP remotes on it. (This set could be
used in other ways later, if we can detect we're out of sync with other
types of remotes.)
The merger handles detecting when a XMPP push is received from a desynced
remote, and triggers a scan then, if they have in fact diverged.
This has one known bug: A single XMPP remote can have multiple clients
behind it. When this happens, only the UUID of one client is recorded
as the UUID of the XMPP remote. Pushes from the other XMPP clients will not
trigger a scan. If the client whose UUID is expected responds to the push
request, it'll work, but when that client is offline, we're SOL.
2013-03-06 19:09:31 +00:00
|
|
|
|
2012-09-16 22:53:13 +00:00
|
|
|
equivBranches :: Git.Ref -> Git.Ref -> Bool
|
|
|
|
equivBranches x y = base x == base y
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
base = takeFileName . show
|
2012-06-22 21:01:08 +00:00
|
|
|
|
2012-09-16 22:53:13 +00:00
|
|
|
isAnnexBranch :: FilePath -> Bool
|
|
|
|
isAnnexBranch f = n `isSuffixOf` f
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
n = "/" ++ show Annex.Branch.name
|
2012-09-16 22:53:13 +00:00
|
|
|
|
|
|
|
fileToBranch :: FilePath -> Git.Ref
|
2012-09-17 03:09:08 +00:00
|
|
|
fileToBranch f = Git.Ref $ "refs" </> base
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
base = Prelude.last $ split "/refs/" f
|