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.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Assistant.TransferSlots where
|
|
|
|
|
2012-10-30 18:34:48 +00:00
|
|
|
import Assistant.Common
|
2012-08-28 21:17:09 +00:00
|
|
|
import Utility.ThreadScheduler
|
2012-10-30 18:34:48 +00:00
|
|
|
import Assistant.Types.TransferSlots
|
2012-08-28 21:17:09 +00:00
|
|
|
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-10-05 21:02:51 +00:00
|
|
|
import qualified Control.Concurrent.MSemN as MSemN
|
2012-07-06 20:39:07 +00:00
|
|
|
|
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
|
2012-10-05 21:02:51 +00:00
|
|
|
MSemN.wait s 1
|
2012-08-28 21:17:09 +00:00
|
|
|
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
|
2012-10-05 21:02:51 +00:00
|
|
|
MSemN.signal s (-1)
|
2012-08-28 21:17:09 +00:00
|
|
|
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 ()
|
2012-10-05 21:02:51 +00:00
|
|
|
runTransferThread _ s Nothing = MSemN.signal s 1
|
2012-08-28 21:17:09 +00:00
|
|
|
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
|
2012-10-05 21:02:51 +00:00
|
|
|
done = MSemN.signal s 1
|