yesod-worker: Drop-in(ish) background worker system for Yesod apps

[ bsd3, library, web ] [ Propose Tags ]

Modules

[Index]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.0.1, 0.1.0.0
Dependencies base (>=4.7 && <4.8), containers (>=0.5), fast-logger (>=2.2), monad-control (>=0.3), monad-logger (>=0.3), persistent (>=2.1), resourcet (>=1.1.2.2), stm (>=2.4.2), template-haskell, transformers (>=0.4), transformers-base (>=0.4.2), yesod (>=1.4), yesod-core (>=1.4) [details]
License GPL-3.0-only
Author James Dabbs
Maintainer jamesdabbs@gmail.com
Home page https://github.com/jamesdabbs/yesod-worker
Uploaded by jdabbs at 2014-12-09T02:18:22Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 1411 total (2 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for yesod-worker-0.0.1

[back to package description]

See the demo for an example site with workers set up.

Usage

Define your Job type

-- Up to you where this goes, so long as it's importable
data AppJob = CountJob Int | UserJob

Include a queue in your foundation data type

-- Foundation.hs
import Yesod.Worker (JobQueue, YesodWorker(..), defaultRunW)

data App = App
  { settings :: AppConfig DefaultEnv Extra
  ...
  , appQueue :: JobQueue AppJob
  }

Specify how to run your jobs

-- Foundation.hs (provided you don't want to orphan the instance)
instance YesodWorker App where
  type Job = AppJob
  queue = appQueue
  runW = defaultRunW persistConfig connPool

  -- Dummy implementations, obviously these would depend on your app
  perform (CountJob n) = void . forM [1..n] $ \k -> do
    lift . putStrLn . show $ k
    lift $ threadDelay 1000000
  perform UserJob = do
    -- Note that you can run SQL using the `runW` helper
    n <- runW $ count ([] :: [Filter User])
    lift . putStrLn $ "There are " ++ (show n) ++ " users"

Start the queue on app boot

-- Application.hs
import Yesod.Worker (emptyQueue, spawnWorkers)

makeFoundation conf = do
  ...
  q <- emptyQueue
  let foundation = App conf ... q
  spawnWorkers foundation
  ...

Queue up a job

import Yesod.Worker (enqueue)

getWhateverR = do
  enqueue $ CountJob 10
  ...

TODO

  • Simplify installation?
  • Include job subsite for viewing queue and worker status
  • Swappable queue backends (for e.g. Redis)
  • Improve error handling / job failures / worker restarts
  • Allow multiple queues with customizable priority