yesod-job-queue 0.1.0.3 → 0.2.0.0
raw patch · 3 files changed
+23/−6 lines, 3 files
Files
- Yesod/JobQueue.hs +20/−3
- example/Main.hs +2/−2
- yesod-job-queue.cabal +1/−1
Yesod/JobQueue.hs view
@@ -12,6 +12,7 @@ , enqueue , JobState , newJobState+ , jobQueueInfo , getJobQueue ) where @@ -25,6 +26,7 @@ import Control.Concurrent import Control.Lens ((^.)) import qualified Data.ByteString.Char8 as BSC (pack, unpack)+import qualified Data.Text as T (pack, append) import Control.Monad.Logger import qualified Database.Redis as R @@ -89,6 +91,10 @@ -- | queue key name for redis queueKey :: master -> ByteString queueKey _ = "yesod-job-queue"++ -- | The number of threads to run the job+ threadNumber :: master -> Int+ threadNumber _ = 1 -- | runDB for job runDBJob :: (MonadBaseControl IO m, MonadIO m)@@ -108,11 +114,16 @@ -- | get information of all type classes related job-queue getClassInformation :: master -> [JobQueueClassInfo]- getClassInformation _ = []+ getClassInformation m = [jobQueueInfo m] --- | start dequeue-ing job in new thread startDequeue :: (YesodJobQueue master, MonadBaseControl IO m, MonadIO m) => master -> m () startDequeue m = do+ let num = threadNumber m+ forM_ [1 .. num] $ startThread m++-- | start dequeue-ing job in new thread+startThread :: (YesodJobQueue master, MonadBaseControl IO m, MonadIO m) => master -> ThreadNum -> m ()+startThread m tNo = do liftIO $ forkIO $ do conn <- R.connect R.defaultConnectInfo R.runRedis conn $ do@@ -127,7 +138,7 @@ Just jt -> do jid <- U.nextRandom time <- getCurrentTime- let job = RunningJob (show jt) 1 jid time+ let job = RunningJob (show jt) tNo jid time STM.atomically $ STM.modifyTVar (getJobState m) (job:)@@ -169,6 +180,12 @@ -- | get all job type list allJobTypes :: (YesodJobQueue master) => master -> [JobType master] allJobTypes _ = [minBound..]++-- | Need by 'getClassInformation'+jobQueueInfo :: YesodJobQueue master => master -> JobQueueClassInfo+jobQueueInfo m = JobQueueClassInfo "JobQueue" [threadInfo]+ where threadInfo = "Number of threads: " `T.append` (T.pack . show $ threadNumber m)+ -- | Handler for job manager api routes type JobHandler master a =
example/Main.hs view
@@ -44,7 +44,7 @@ instance YesodJobQueue App where type JobType App = MyJobType getJobState = appJobState- -- jobDBConfig app = (appDBConf app, appConnPool app)+ threadNumber _ = 2 runDBJob action = do app <- ask runSqlPool action $ appConnPool app@@ -55,7 +55,7 @@ putStrLn "complate job!" runJob _ PushNotification = do putStrLn "send norification!"- getClassInformation app = [schedulerInfo app]+ getClassInformation app = [jobQueueInfo app, schedulerInfo app] -- jobManagerJSUrl _ = "http://localhost:3001/dist/app.bundle.js" -- use for development with "npm run bs" instance YesodJobQueueScheduler App where
yesod-job-queue.cabal view
@@ -1,5 +1,5 @@ name: yesod-job-queue-version: 0.1.0.3+version: 0.2.0.0 synopsis: Background jobs library for Yesod. description: Background jobs library for Yesod