2012-07-06 20:39:07 +00:00
|
|
|
{- git-annex assistant transfer slots
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2012-08-10 22:42:44 +00:00
|
|
|
{-# LANGUAGE DeriveDataTypeable #-}
|
|
|
|
|
2012-07-06 20:39:07 +00:00
|
|
|
module Assistant.TransferSlots where
|
|
|
|
|
2012-08-28 21:17:09 +00:00
|
|
|
import Common.Annex
|
|
|
|
import Utility.ThreadScheduler
|
|
|
|
import Assistant.DaemonStatus
|
|
|
|
import Logs.Transfer
|
|
|
|
|
2012-08-10 22:42:44 +00:00
|
|
|
import qualified Control.Exception as E
|
2012-07-06 20:39:07 +00:00
|
|
|
import Control.Concurrent
|
2012-08-10 22:42:44 +00:00
|
|
|
import Data.Typeable
|
2012-07-06 20:39:07 +00:00
|
|
|
|
|
|
|
type TransferSlots = QSemN
|
|
|
|
|
2012-08-10 22:42:44 +00:00
|
|
|
{- A special exception that can be thrown to pause or resume a transfer, while
|
|
|
|
- keeping its slot in use. -}
|
|
|
|
data TransferException = PauseTransfer | ResumeTransfer
|
|
|
|
deriving (Show, Eq, Typeable)
|
|
|
|
|
|
|
|
instance E.Exception TransferException
|
|
|
|
|
2012-08-28 21:17:09 +00:00
|
|
|
type TransferSlotRunner = DaemonStatusHandle -> TransferSlots -> TransferGenerator -> IO ()
|
|
|
|
type TransferGenerator = IO (Maybe (Transfer, TransferInfo, IO ()))
|
2012-08-12 16:36:08 +00:00
|
|
|
|
2012-07-06 20:39:07 +00:00
|
|
|
{- Number of concurrent transfers allowed to be run from the assistant.
|
|
|
|
-
|
|
|
|
- Transfers launched by other means, including by remote assistants,
|
|
|
|
- do not currently take up slots.
|
|
|
|
-}
|
|
|
|
numSlots :: Int
|
|
|
|
numSlots = 1
|
|
|
|
|
|
|
|
newTransferSlots :: IO TransferSlots
|
|
|
|
newTransferSlots = newQSemN numSlots
|
|
|
|
|
2012-08-28 21:17:09 +00:00
|
|
|
{- Waits until a transfer slot becomes available, then runs a
|
|
|
|
- TransferGenerator, and then runs the transfer action in its own thread.
|
2012-08-12 16:36:08 +00:00
|
|
|
-}
|
|
|
|
inTransferSlot :: TransferSlotRunner
|
2012-08-28 21:17:09 +00:00
|
|
|
inTransferSlot dstatus s gen = do
|
|
|
|
waitQSemN s 1
|
|
|
|
runTransferThread dstatus s =<< gen
|
2012-08-12 16:36:08 +00:00
|
|
|
|
2012-08-28 21:17:09 +00:00
|
|
|
{- Runs a TransferGenerator, and its transfer action,
|
|
|
|
- without waiting for a slot to become available. -}
|
2012-08-12 16:36:08 +00:00
|
|
|
inImmediateTransferSlot :: TransferSlotRunner
|
2012-08-28 21:17:09 +00:00
|
|
|
inImmediateTransferSlot dstatus s gen = do
|
|
|
|
signalQSemN s (-1)
|
|
|
|
runTransferThread dstatus s =<< gen
|
2012-08-12 16:36:08 +00:00
|
|
|
|
2012-08-28 21:17:09 +00:00
|
|
|
{- Runs a transfer action, in an already allocated transfer slot.
|
|
|
|
- Once it finishes, frees the transfer slot.
|
|
|
|
-
|
|
|
|
- Note that the action is subject to being killed when the transfer
|
2012-08-10 22:42:44 +00:00
|
|
|
- is canceled or paused.
|
|
|
|
-
|
|
|
|
- A PauseTransfer exception is handled by letting the action be killed,
|
|
|
|
- then pausing the thread until a ResumeTransfer exception is raised,
|
|
|
|
- then rerunning the action.
|
|
|
|
-}
|
2012-08-28 21:17:09 +00:00
|
|
|
runTransferThread :: DaemonStatusHandle -> TransferSlots -> Maybe (Transfer, TransferInfo, IO ()) -> IO ()
|
|
|
|
runTransferThread _ s Nothing = signalQSemN s 1
|
|
|
|
runTransferThread dstatus s (Just (t, info, a)) = do
|
2012-09-13 04:57:52 +00:00
|
|
|
tid <- forkIO go
|
2012-08-28 21:17:09 +00:00
|
|
|
updateTransferInfo dstatus t $ info { transferTid = Just tid }
|
2012-07-07 03:45:08 +00:00
|
|
|
where
|
2012-08-28 21:17:09 +00:00
|
|
|
go = catchPauseResume a
|
2012-09-13 04:57:52 +00:00
|
|
|
pause = catchPauseResume $ runEvery (Seconds 86400) noop
|
2012-08-29 22:02:52 +00:00
|
|
|
{- Note: This must use E.try, rather than E.catch.
|
|
|
|
- When E.catch is used, and has called go in its exception
|
|
|
|
- handler, Control.Concurrent.throwTo will block sometimes
|
|
|
|
- when signaling. Using E.try avoids the problem. -}
|
|
|
|
catchPauseResume a' = do
|
2012-08-30 01:28:59 +00:00
|
|
|
r <- E.try a' :: IO (Either E.SomeException ())
|
2012-08-29 22:02:52 +00:00
|
|
|
case r of
|
2012-08-30 01:28:59 +00:00
|
|
|
Left e -> case E.fromException e of
|
|
|
|
Just PauseTransfer -> pause
|
|
|
|
Just ResumeTransfer -> go
|
|
|
|
_ -> done
|
|
|
|
_ -> done
|
|
|
|
done = signalQSemN s 1
|