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-10-30 21:14:26 +00:00
|
|
|
type TransferGenerator = Assistant (Maybe (Transfer, TransferInfo, 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
|
|
|
-}
|
2012-10-30 19:39:15 +00:00
|
|
|
inTransferSlot :: TransferGenerator -> Assistant ()
|
|
|
|
inTransferSlot gen = do
|
|
|
|
flip MSemN.wait 1 <<~ transferSlots
|
|
|
|
runTransferThread =<< 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-10-30 19:39:15 +00:00
|
|
|
inImmediateTransferSlot :: TransferGenerator -> Assistant ()
|
|
|
|
inImmediateTransferSlot gen = do
|
|
|
|
flip MSemN.signal (-1) <<~ transferSlots
|
|
|
|
runTransferThread =<< 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-10-30 21:14:26 +00:00
|
|
|
runTransferThread :: Maybe (Transfer, TransferInfo, Assistant ()) -> Assistant ()
|
2012-10-30 19:39:15 +00:00
|
|
|
runTransferThread Nothing = flip MSemN.signal 1 <<~ transferSlots
|
|
|
|
runTransferThread (Just (t, info, a)) = do
|
|
|
|
d <- getAssistant id
|
2012-10-30 21:14:26 +00:00
|
|
|
aio <- asIO a
|
|
|
|
tid <- liftIO $ forkIO $ runTransferThread' d aio
|
2012-10-30 19:39:15 +00:00
|
|
|
updateTransferInfo t $ info { transferTid = Just tid }
|
2012-10-30 21:14:26 +00:00
|
|
|
|
|
|
|
runTransferThread' :: AssistantData -> IO () -> IO ()
|
|
|
|
runTransferThread' d a = go
|
2012-07-07 03:45:08 +00:00
|
|
|
where
|
2012-10-30 21:14:26 +00:00
|
|
|
go = catchPauseResume a
|
|
|
|
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. -}
|
2012-10-30 21:14:26 +00:00
|
|
|
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
|
2012-10-30 21:14:26 +00:00
|
|
|
Just PauseTransfer -> pause
|
|
|
|
Just ResumeTransfer -> go
|
|
|
|
_ -> done
|
|
|
|
_ -> done
|
|
|
|
done = flip runAssistant d $
|
2012-10-30 19:39:15 +00:00
|
|
|
flip MSemN.signal 1 <<~ transferSlots
|