summaryrefslogtreecommitdiff
path: root/Assistant/Pusher.hs
blob: 119575b92a74732d2cc04cfe6ea95199d28d1eba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{- git-annex assistant git pushing thread
 -
 - Copyright 2012 Joey Hess <joey@kitenet.net>
 -}

module Assistant.Pusher where

import Common.Annex
import Assistant.Commits
import Assistant.ThreadedMonad
import qualified Command.Sync
import Utility.ThreadScheduler
import Utility.Parallel

import Data.Time.Clock

data FailedPush = FailedPush 
	{ failedRemote :: Remote
	, failedTimeStamp :: UTCTime
	}

{- This thread pushes git commits out to remotes. -}
pushThread :: ThreadState -> CommitChan -> IO ()
pushThread st commitchan = do
	remotes <- runThreadState st $ Command.Sync.syncRemotes []
	runEveryWith (Seconds 2) [] $ \failedpushes -> do
		-- We already waited two seconds as a simple rate limiter.
		-- Next, wait until at least one commit has been made
		commits <- getCommits commitchan
		-- Now see if now's a good time to push.
		time <- getCurrentTime
		if shouldPush time commits failedpushes
			then pushToRemotes time st remotes
			else do
				refillCommits commitchan commits
				return failedpushes

{- Decide if now is a good time to push to remotes.
 -
 - Current strategy: Immediately push all commits. The commit machinery
 - already determines batches of changes, so we can't easily determine
 - batches better.
 -
 - TODO: FailedPushs are only retried the next time there's a commit.
 - Should retry them periodically, or when a remote that was not available
 - becomes available.
 -}
shouldPush :: UTCTime -> [Commit] -> [FailedPush] -> Bool
shouldPush _now commits _failedremotes
	| not (null commits) = True
	| otherwise = False

{- Updates the local sync branch, then pushes it to all remotes, in
 - parallel.
 -
 - Avoids running possibly long-duration commands in the Annex monad, so
 - as not to block other threads. -}
pushToRemotes :: UTCTime -> ThreadState -> [Remote] -> IO [FailedPush]
pushToRemotes now st remotes = do
	(g, branch) <- runThreadState st $
		(,) <$> fromRepo id <*> Command.Sync.currentBranch
	Command.Sync.updateBranch (Command.Sync.syncBranch branch) g
	map (`FailedPush` now) <$> inParallel (push g branch) remotes
	where
		push g branch remote =
			ifM (Command.Sync.pushBranch remote branch g)
				( exitSuccess, exitFailure)