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
|
2013-03-19 22:46:29 +00:00
|
|
|
import Assistant.TransferrerPool
|
|
|
|
import Assistant.Types.TransferrerPool
|
2012-08-28 21:17:09 +00:00
|
|
|
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
|
|
|
|
2013-03-19 22:46:29 +00:00
|
|
|
type TransferGenerator = Assistant (Maybe (Transfer, TransferInfo, Transferrer -> Assistant ()))
|
2012-10-30 19:39:15 +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
|
|
|
-}
|
2013-03-19 22:46:29 +00:00
|
|
|
inTransferSlot :: FilePath -> TransferGenerator -> Assistant ()
|
|
|
|
inTransferSlot program gen = do
|
2012-10-30 19:39:15 +00:00
|
|
|
flip MSemN.wait 1 <<~ transferSlots
|
2013-03-19 22:46:29 +00:00
|
|
|
runTransferThread program =<< 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. -}
|
2013-03-19 22:46:29 +00:00
|
|
|
inImmediateTransferSlot :: FilePath -> TransferGenerator -> Assistant ()
|
|
|
|
inImmediateTransferSlot program gen = do
|
2012-10-30 19:39:15 +00:00
|
|
|
flip MSemN.signal (-1) <<~ transferSlots
|
2013-03-19 22:46:29 +00:00
|
|
|
runTransferThread program =<< 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.
|
|
|
|
-}
|
2013-03-19 22:46:29 +00:00
|
|
|
runTransferThread :: FilePath -> Maybe (Transfer, TransferInfo, Transferrer -> Assistant ()) -> Assistant ()
|
|
|
|
runTransferThread _ Nothing = flip MSemN.signal 1 <<~ transferSlots
|
|
|
|
runTransferThread program (Just (t, info, a)) = do
|
2012-10-30 19:39:15 +00:00
|
|
|
d <- getAssistant id
|
2013-03-19 22:46:29 +00:00
|
|
|
aio <- asIO1 a
|
|
|
|
tid <- liftIO $ forkIO $ runTransferThread' program d aio
|
2012-10-30 19:39:15 +00:00
|
|
|
updateTransferInfo t $ info { transferTid = Just tid }
|
2012-10-30 21:14:26 +00:00
|
|
|
|
2013-03-19 22:46:29 +00:00
|
|
|
runTransferThread' :: FilePath -> AssistantData -> (Transferrer -> IO ()) -> IO ()
|
|
|
|
runTransferThread' program d run = go
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
2013-03-19 22:46:29 +00:00
|
|
|
go = catchPauseResume $
|
|
|
|
withTransferrer program (transferrerPool d)
|
|
|
|
run
|
|
|
|
pause = catchPauseResume $
|
|
|
|
runEvery (Seconds 86400) noop
|
2012-10-31 06:34:03 +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
|
|
|
|
r <- E.try a' :: IO (Either E.SomeException ())
|
|
|
|
case r of
|
|
|
|
Left e -> case E.fromException e of
|
|
|
|
Just PauseTransfer -> pause
|
|
|
|
Just ResumeTransfer -> go
|
2012-10-30 21:14:26 +00:00
|
|
|
_ -> done
|
2012-10-31 06:34:03 +00:00
|
|
|
_ -> done
|
2012-11-05 23:39:08 +00:00
|
|
|
done = runAssistant d $
|
2012-10-31 06:34:03 +00:00
|
|
|
flip MSemN.signal 1 <<~ transferSlots
|