antiope-sqs (empty) → 1.0.0
raw patch · 8 files changed
+205/−0 lines, 8 filesdep +aesondep +amazonkadep +amazonka-coresetup-changed
Dependencies added: aeson, amazonka, amazonka-core, amazonka-s3, amazonka-sqs, antiope-s3, antiope-sqs, base, generic-lens, lens, lens-aeson, monad-loops, network-uri, text
Files
- ChangeLog.md +3/−0
- LICENSE +30/−0
- README.md +1/−0
- Setup.hs +2/−0
- antiope-sqs.cabal +79/−0
- src/Antiope/SQS.hs +74/−0
- src/Antiope/SQS/Types.hs +14/−0
- test/Spec.hs +2/−0
+ ChangeLog.md view
@@ -0,0 +1,3 @@+# Changelog for antiope-sqs++## Unreleased changes
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Arbor Networks (c) 2018++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Alexey Raga nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,1 @@+# antiope-sqs
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ antiope-sqs.cabal view
@@ -0,0 +1,79 @@+-- This file has been generated from package.yaml by hpack version 0.20.0.+--+-- see: https://github.com/sol/hpack+--+-- hash: ac64958cc7efc69597ec92dfaf4373cb4d2525a8efafa7a89526a55e027a8434++name: antiope-sqs+version: 1.0.0+description: Please see the README on Github at <https://github.com/arbor/antiope#readme>+category: Services+homepage: https://github.com/arbor/antiope#readme+bug-reports: https://github.com/arbor/antiope/issues+author: Tyler Durden+maintainer: mayhem@arbor.net+copyright: Arbor Networks+license: MIT+license-file: LICENSE+build-type: Simple+cabal-version: >= 1.10++extra-source-files:+ ChangeLog.md+ README.md++source-repository head+ type: git+ location: https://github.com/arbor/antiope++library+ hs-source-dirs:+ src+ default-extensions: BangPatterns GeneralizedNewtypeDeriving OverloadedStrings TupleSections+ ghc-options: -Wall -Wcompat -Wincomplete-record-updates -Wincomplete-uni-patterns -Wredundant-constraints -O2 -msse4.2+ build-depends:+ aeson+ , amazonka+ , amazonka-core+ , amazonka-s3+ , amazonka-sqs+ , antiope-s3+ , base >=4.7 && <5+ , generic-lens+ , lens+ , lens-aeson+ , monad-loops+ , network-uri+ , text+ exposed-modules:+ Antiope.SQS+ Antiope.SQS.Types+ other-modules:+ Paths_antiope_sqs+ default-language: Haskell2010++test-suite antiope-sqs-test+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ hs-source-dirs:+ test+ default-extensions: BangPatterns GeneralizedNewtypeDeriving OverloadedStrings TupleSections+ ghc-options: -Wall -Wcompat -Wincomplete-record-updates -Wincomplete-uni-patterns -Wredundant-constraints -O2 -msse4.2 -threaded -rtsopts -with-rtsopts=-N+ build-depends:+ aeson+ , amazonka+ , amazonka-core+ , amazonka-s3+ , amazonka-sqs+ , antiope-s3+ , antiope-sqs+ , base >=4.7 && <5+ , generic-lens+ , lens+ , lens-aeson+ , monad-loops+ , network-uri+ , text+ other-modules:+ Paths_antiope_sqs+ default-language: Haskell2010
+ src/Antiope/SQS.hs view
@@ -0,0 +1,74 @@+module Antiope.SQS+( MonadAWS+, FromText(..), fromText+, ToText(..)+, QueueUrl(..)+, SQSError(..)+, readQueue+, drainQueue+, ackMessage+, ackMessages+, messageInBody+, s3Location+, s3Location'+) where++import Antiope.S3 (S3Uri (..))+import Antiope.SQS.Types (QueueUrl (QueueUrl), SQSError (DeleteMessageBatchError))+import Control.Lens+import Control.Monad (join)+import Control.Monad.Loops (unfoldWhileM)+import Data.Aeson.Lens+import Data.Maybe (catMaybes)+import Data.Text (Text, pack, unpack)+import Network.AWS (MonadAWS, send)+import Network.AWS.Data.Text (FromText (..), ToText (..), fromText, toText)+import Network.AWS.S3 hiding (s3Location)+import Network.AWS.SQS++import qualified Network.URI as URI++-- | Reads the specified SQS queue once returning a bath of messages+readQueue :: MonadAWS m => QueueUrl -> m [Message]+readQueue (QueueUrl queueUrl) = do+ -- max wait time allowed by aws is 20sec, max number messages to recieve is 10+ resp <- send $ receiveMessage queueUrl & rmWaitTimeSeconds ?~ 10 & rmMaxNumberOfMessages ?~ 10+ return $ resp ^. rmrsMessages++-- | Reads the specified SQS queue until it is empty and returns a list of messages+drainQueue :: MonadAWS m => QueueUrl -> m [Message]+drainQueue queueUrl =+ join <$> unfoldWhileM (not . null) (readQueue queueUrl)++ackMessage :: MonadAWS m => QueueUrl -> Message -> m (Either SQSError ())+ackMessage q msg = ackMessages q [msg]++ackMessages :: MonadAWS m => QueueUrl -> [Message] -> m (Either SQSError ())+ackMessages (QueueUrl queueUrl) msgs = do+ let receipts = catMaybes $ msgs ^.. each . mReceiptHandle+ -- each dmbr needs an ID. just use the list index.+ let dmbres = (\(r, i) -> deleteMessageBatchRequestEntry (pack (show i)) r) <$> zip receipts ([0..] :: [Int])+ resp <- send $ deleteMessageBatch queueUrl & dmbEntries .~ dmbres+ -- only acceptable if no errors.+ if resp ^. dmbrsResponseStatus == 200+ then case resp ^. dmbrsFailed of+ [] -> return $ Right ()+ _ -> return $ Left DeleteMessageBatchError+ else return $ Left DeleteMessageBatchError++-- Extract the "Message" content in the body if have+messageInBody :: Text -> Maybe Text+messageInBody body = body ^? key "Message" . _String++s3Location :: Message -> Maybe S3Uri+s3Location msg = join $ s3Location' <$> msg ^. mBody++s3Location' :: Text -> Maybe S3Uri+s3Location' msg = do+ s3m <- messageInBody msg ^? _Just . key "Records" . nth 0 . key "s3"+ b <- s3m ^? key "bucket" . key "name" . _String+ k <- s3m ^? key "object" . key "key" . _String+ pure $ S3Uri (BucketName b) (ObjectKey $ uriDecode k)++uriDecode :: Text -> Text+uriDecode = pack . URI.unEscapeString . unpack
+ src/Antiope/SQS/Types.hs view
@@ -0,0 +1,14 @@+{-# LANGUAGE DeriveGeneric #-}++module Antiope.SQS.Types where++import Data.String (IsString)+import Data.Text (Text)+import GHC.Generics+import Network.AWS.Data.Text (FromText (..), ToText (..))++data SQSError = DeleteMessageBatchError+ deriving (Eq, Show, Generic)++newtype QueueUrl = QueueUrl Text+ deriving (Show, Eq, IsString, FromText, ToText, Generic)
+ test/Spec.hs view
@@ -0,0 +1,2 @@+main :: IO ()+main = putStrLn "Test suite not yet implemented"