module Network.AWS.Wolf.Ctx
( runConfCtx
, preConfCtx
, runAmazonCtx
, runAmazonStoreCtx
, runAmazonWorkCtx
, runAmazonDecisionCtx
) where
import Control.Concurrent
import Control.Exception.Lifted
import Control.Monad.Trans.AWS
import Data.Aeson
import Network.AWS.SWF
import Network.AWS.Wolf.Prelude
import Network.AWS.Wolf.Types
import Network.HTTP.Types
botSomeExceptionCatch :: MonadCtx c m => SomeException -> m a
botSomeExceptionCatch ex = do
traceError "exception" [ "error" .= displayException ex ]
throwIO ex
botErrorCatch :: MonadCtx c m => Error -> m a
botErrorCatch ex = do
case ex of
TransportError _ ->
return ()
_ ->
traceError "exception" [ "error" .= displayException ex ]
throwIO ex
topSomeExceptionCatch :: MonadStatsCtx c m => SomeException -> m a
topSomeExceptionCatch ex = do
statsIncrement "exception" [ "reason" =. textFromString (displayException ex) ]
throwIO ex
runBotTransT :: (MonadControl m, HasCtx c) => c -> TransT c m a -> m a
runBotTransT c action = runTransT c $ catches action [ Handler botErrorCatch, Handler botSomeExceptionCatch ]
runTopTransT :: (MonadControl m, HasStatsCtx c) => c -> TransT c m a -> m a
runTopTransT c action = runBotTransT c $ catch action topSomeExceptionCatch
runConfCtx :: MonadStatsCtx c m => Conf -> TransT ConfCtx m a -> m a
runConfCtx conf action = do
let preamble =
[ "domain" .= (conf ^. cDomain)
, "bucket" .= (conf ^. cBucket)
, "prefix" .= (conf ^. cPrefix)
]
c <- view statsCtx <&> cPreamble <>~ preamble
runTopTransT (ConfCtx c conf) action
preConfCtx :: MonadConf c m => Pairs -> TransT ConfCtx m a -> m a
preConfCtx preamble action = do
c <- view confCtx <&> cPreamble <>~ preamble
runBotTransT c action
runAmazonCtx :: MonadConf c m => TransT AmazonCtx m a -> m a
runAmazonCtx action = do
c <- view confCtx
#if MIN_VERSION_amazonka(1,4,5)
e <- newEnv $ FromEnv "AWS_ACCESS_KEY_ID" "AWS_SECRET_ACCESS_KEY" mempty mempty
#else
e <- newEnv Oregon $ FromEnv "AWS_ACCESS_KEY_ID" "AWS_SECRET_ACCESS_KEY" mempty
#endif
runBotTransT (AmazonCtx c e) action
runAmazonStoreCtx :: MonadConf c m => Text -> TransT AmazonStoreCtx m a -> m a
runAmazonStoreCtx uid action = do
let preamble = [ "uid" .= uid ]
c <- view confCtx <&> cPreamble <>~ preamble
p <- (-/- uid) . view cPrefix <$> view ccConf
runBotTransT (AmazonStoreCtx c uid p) action
throttled :: MonadStatsCtx c m => m a -> m a
throttled action = do
traceError "throttled" mempty
statsIncrement "wolf.throttled" mempty
liftIO $ threadDelay $ 5 * 1000000
catch action $ throttler action
throttler :: MonadStatsCtx c m => m a -> Error -> m a
throttler action e =
case e of
ServiceError se ->
bool (throwIO e) (throttled action) $
se ^. serviceStatus == badRequest400 &&
se ^. serviceCode == "Throttling"
_ ->
throwIO e
runAmazonWorkCtx :: MonadConf c m => Text -> TransT AmazonWorkCtx m a -> m a
runAmazonWorkCtx queue action = do
let preamble = [ "queue" .= queue ]
c <- view confCtx <&> cPreamble <>~ preamble
runBotTransT (AmazonWorkCtx c queue) (catch action $ throttler action)
runAmazonDecisionCtx :: MonadConf c m => Plan -> [HistoryEvent] -> TransT AmazonDecisionCtx m a -> m a
runAmazonDecisionCtx p hes action = do
let preamble = [ "name" .= (p ^. pStart ^. tName) ]
c <- view confCtx <&> cPreamble <>~ preamble
runBotTransT (AmazonDecisionCtx c p hes) action