redis-job-queue (empty) → 0.1.0
raw patch · 3 files changed
+120/−0 lines, 3 filesdep +aesondep +basedep +bytestring
Dependencies added: aeson, base, bytestring, hedis
Files
- LICENSE +22/−0
- RedisJobQueue.hs +70/−0
- redis-job-queue.cabal +28/−0
+ LICENSE view
@@ -0,0 +1,22 @@+The MIT License (MIT)++Copyright (c) 2015 Tero Laitinen++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.+
+ RedisJobQueue.hs view
@@ -0,0 +1,70 @@+{-# LANGUAGE OverloadedStrings #-}++module RedisJobQueue (+ withJobQueue,+ withJobQueue',+ JobQueue,+ push,+ pop,+ pushJson,+ popJson,+ QueueName,+ Job,+ Priority+) where++import Database.Redis+import Data.ByteString+import qualified Data.Aeson as A+import qualified Data.ByteString.Lazy as LBS+import Data.ByteString.Char8 as BS8+++type QueueName = ByteString+type Job = ByteString+type Priority = Double++data JobQueue = JobQueue QueueName Connection++withJobQueue :: QueueName -> (JobQueue -> IO ()) -> IO ()+withJobQueue = withJobQueue' defaultConnectInfo++withJobQueue' :: ConnectInfo -> QueueName -> (JobQueue -> IO ()) -> IO ()+withJobQueue' connInfo qn f = do+ conn <- connect connInfo+ f $ JobQueue qn conn++push :: JobQueue -> Priority -> Job -> IO (Either Reply Integer)+push (JobQueue qn conn) p j = runRedis conn $ zadd qn [(p,j)]+ +pop :: JobQueue -> IO (Either Reply (Maybe Job))+pop (JobQueue qn conn) = runRedis conn $ do+ _ <- watch [qn]+ js <- zrange qn 0 0 + case js of + Right (j:_) -> do+ r <- multiExec $ zrem qn [j]+ case r of+ TxSuccess _ -> return $ Right (Just j)+ TxAborted -> return $ Left $ Error "aborted"+ TxError err -> return $ Left $ Error $ BS8.pack err+ Right [] -> do+ _ <- unwatch+ return $ Right Nothing+ Left e -> return $ Left e+ +pushJson :: A.ToJSON a => JobQueue -> Priority -> a -> IO (Either Reply Integer)+pushJson jq p j = push jq p $ LBS.toStrict $ A.encode j++popJson :: A.FromJSON a => JobQueue -> IO (Either String (Maybe a))+popJson jq = do+ mj <- pop jq+ case mj of+ Right (Just j) -> return $ A.eitherDecode $ LBS.fromStrict j+ Right Nothing -> return $ Right Nothing+ Left l -> return $ Left (show l)+ + + + +
+ redis-job-queue.cabal view
@@ -0,0 +1,28 @@+name: redis-job-queue+version: 0.1.0+license: MIT+license-file: LICENSE+author: Tero Laitinen +maintainer: Tero Laitinen+synopsis: Simple priority job queue backed by Redis.+description: Priority queue for background jobs implemented using Redis' sorted sets.+category: Client+stability: Experimental+cabal-version: >= 1.8+build-type: Simple+++library+ build-depends: base >= 4 && < 5+ , bytestring -any+ , hedis -any+ , aeson -any+ ghc-options: -Wall + exposed: True+ buildable: True+ exposed-modules:+ RedisJobQueue++source-repository head+ type: git+ location: https://github.com/tlaitinen/redis-job-queue