packages feed

yesod-job-queue 0.1.0.0 → 0.1.0.1

raw patch · 4 files changed

+46/−20 lines, 4 filesdep ~yesod

Dependency ranges changed: yesod

Files

Yesod/JobQueue.hs view
@@ -19,7 +19,6 @@ import Control.Lens ((^.)) import qualified Data.ByteString.Char8 as BSC (pack, unpack) import Control.Monad.Logger-import System.Cron.Schedule import qualified Database.Redis as R  import qualified Data.UUID as U@@ -32,7 +31,7 @@ import Data.FileEmbed (embedFile)  -- persist config for job env-type JobDBConf c = (PersistConfig c) => (c, PersistConfigPool c)+-- type JobDBConf c = (PersistConfig c) => (c, PersistConfigPool c)  class JobInfo j where     describe :: j -> String@@ -70,12 +69,11 @@  class (Yesod master, Read (JobType master), Show (JobType master)       , Enum (JobType master), Bounded (JobType master)-      , JobInfo (JobType master)-      , PersistConfig config)-      => YesodJobQueue master config | master -> config where+      , JobInfo (JobType master))+      => YesodJobQueue master where          -- persistent config for job-    jobDBConfig :: master -> JobDBConf config+    -- jobDBConfig :: master -> JobDBConf config          -- Custom Job Type     type JobType master@@ -99,13 +97,11 @@     -- get all job type list     allJobTypes :: master -> [JobType master]     allJobTypes _ = [minBound..]+         -- runDB with config for job-    runDBJob :: (MonadBaseControl IO m, MonadIO m) =>-                PersistConfigBackend config m a -> ReaderT master m a-    runDBJob action = do-        m <- ask-        let (config, pool) = jobDBConfig m-        lift $ runPool config action pool+    runDBJob :: (MonadBaseControl IO m, MonadIO m)+                => ReaderT (YesodPersistBackend master) (ReaderT master m) a+                -> ReaderT master m a      -- start thread of dequeue-ing job     startDequeue :: (MonadBaseControl IO m, MonadIO m) => master -> m ()@@ -137,6 +133,7 @@                     return ()         return () +    -- | get job state     getJobState :: master -> JobState      -- | get API and Manager base url@@ -171,7 +168,7 @@  -- | Handler for job manager api routes type JobHandler master a =-    YesodJobQueue master c => HandlerT JobQueue (HandlerT master IO) a+    YesodJobQueue master => HandlerT JobQueue (HandlerT master IO) a  -- | get job definitions getJobR :: JobHandler master Value@@ -244,7 +241,7 @@     | otherwise = notFound  -- | JobQueue manager subsite-instance YesodJobQueue master c => YesodSubDispatch JobQueue (HandlerT master IO) where+instance YesodJobQueue master => YesodSubDispatch JobQueue (HandlerT master IO) where     yesodSubDispatch = $(mkYesodSubDispatch resourcesJobQueue)  getJobQueue :: a -> JobQueue
+ Yesod/JobQueue/Scheduler.hs view
@@ -0,0 +1,18 @@+module Yesod.JobQueue.Scheduler where++import Yesod.JobQueue+import System.Cron.Schedule++import qualified Prelude as P+import ClassyPrelude.Yesod++class (YesodJobQueue master) => YesodJobQueueScheduler master where+    -- | job schedules+    getJobSchedules :: master -> [(String, JobType master)]++    -- | start schedule+    startJobSchedule :: (MonadBaseControl IO m, MonadIO m) => master -> m ()+    startJobSchedule master = do+        let add (s, jt) = addJob (enqueue master jt) s+        tids <- liftIO $ execSchedule $ mapM_ add $ getJobSchedules master+        print tids
example/Main.hs view
@@ -2,6 +2,7 @@ import ClassyPrelude.Yesod import Yesod import Yesod.JobQueue+import Yesod.JobQueue.Scheduler import Control.Concurrent import Database.Persist.Sqlite import Control.Monad.Trans.Resource (runResourceT)@@ -48,18 +49,26 @@         describe AggregationUser = "aggregate user's activities"         describe _ = "No information" -instance YesodJobQueue App SqliteConf where+instance YesodJobQueue App where     type JobType App = MyJobType     getJobState = appJobState-    jobDBConfig app = (appDBConf app, appConnPool app)+    -- jobDBConfig app = (appDBConf app, appConnPool app)+    runDBJob action = do+        app <- ask+        runSqlPool action $ appConnPool app     runJob app AggregationUser = do         us <- runDBJob $ selectList ([] :: [Filter Person]) []         liftIO $ threadDelay $ 10 * 1000 * 1000+        print us         putStrLn "complate job!"     runJob app PushNotification = do         putStrLn "send norification!"     -- jobManagerJSUrl _ = "http://localhost:3001/dist/app.bundle.js" -- use for development with "npm run bs" +instance YesodJobQueueScheduler App  where+    getJobSchedules _ = [("* * * * *", AggregationUser)+                         , ("* * * * *", PushNotification)]+ -- Main main :: IO () main = runStderrLoggingT $@@ -70,5 +79,6 @@            jobState <- newJobState -- ^ create JobState            let app = App pool dbConf jobState            startDequeue app+           startJobSchedule app            warp 3000 app   where dbConf = SqliteConf "test.db3" 4
yesod-job-queue.cabal view
@@ -1,6 +1,6 @@ name:                yesod-job-queue-version:             0.1.0.0-synopsis:            Background jobs library for Yesod. contains management API and web interface. Queue backend is Redis.+version:             0.1.0.1+synopsis:            Background jobs library for Yesod. description:         Please see README.md homepage:            https://github.com/nakaji-dayo/yesod-job-queue#readme license:             BSD3@@ -20,6 +20,7 @@ library   -- hs-source-dirs:      src   exposed-modules:     Yesod.JobQueue+                     , Yesod.JobQueue.Scheduler                      , Yesod.JobQueue.APIData                      , Yesod.JobQueue.Routes   default-extensions:  TemplateHaskell@@ -43,7 +44,7 @@                      , lens                      , classy-prelude-yesod                      , api-field-json-th-                     , yesod+                     , yesod >= 1.4 && < 1.5                      , file-embed                      , text                      , time@@ -68,7 +69,7 @@ --  other-modules:    ghc-options:      -Wall -fwarn-tabs -O2 -  build-depends:  base+  build-depends:  base >= 4.7 && < 5                 , yesod                 , yesod-core                 , yesod-job-queue