GithubHelp home page GithubHelp logo

spine's Introduction

Spine

A high-level, Beanstalkd-backed job queueing library for Clojure.

Usage

Initialize the Spine client:

(require '[spine.client :as spine])

(def sp (spine/init))

Enqueue jobs:

(spine/enqueue sp "call-later")

(spine/enqueue sp "call-later-with-arg" "arg")

Work jobs:

(defn call-later []
  (println "called!"))

(defn call-later-with-arg [arg]
  (println "called with" arg "!"))

(spine/work sp [#'call-later #'call-later-with-arg])

Example

In this example, we'll show how to perform some background work as the result of a web request.

Our web namespace in src/web.clj looks like:

(ns web
  (:use compojure.core)
  (:use ring.adapter.jetty)
  (:require [spine.client :as client]))

(def sp (spine/init))

(defroutes app
  (POST "/upcase" [word]
    (println "upcase of" word "requested")
    (spine/enqueue sp "upcase" word)
    "upcasing asynchronously\n"))

(defn -main []
  (run-jetty app {:port 8080}))

Then our worker namespace in src/worker.clj is:

(ns worker
  (:require [spine.client :as spine]))

(def sp (spine/init))

(defn upcase [word]
  (println "the upcase of" word "is" (.toUpperCase word)))

(defn -main []
  (spine/work sp [#'upcase]))

Here is the project.clj that has the dependencies we need to run this:

(defproject spine-demo "0.0.3"
  :dependencies
    [[org.clojure/clojure "1.3.0-alpha4"]
     [ring/ring-jetty-adapter "0.3.5"]
     [compojure "0.5.3"]
     [spine "0.0.3"]])

After writing these files, run lein deps to install your dependencies. Then start three processes as follows:

$ beanstalkd
$ lein run -m web
$ lein run -m worker

When everything is running, test out your app with:

$ curl -X POST http://localhost:8080/upcase?word=beanstalkd

The curl command should return:

upcasing asynchronously

You should see in your web logs:

spine event=init url='beanstalkd://127.0.0.1:11300'
upcase of word beanstalkd requested
spine event=enqueue queue='upcase'

And in your worker logs:

spine event=init url='beanstalkd://127.0.0.1:11300'
spine event=work queues='upcase'
spine event=dequeue queue=upcase
the upcase of beanstalkd is BEANSTALKD
spine event=complete queue=upcase elapsed=7

Options

When initializing the client, you can specify the Beanstalkd URL:

(def sp (spine/init {:url "beanstalkd://queues.myapp.com:9000"}))

It defaults to "beanstalkd://127.0.0.1:11300".

When working jobs, you can specify an error handler that will be invoked with the Exception instance

(spine/work sp [#'job]
  {:error (fn [e] (send-email (pr-str "it broke:" e)))})

See also

The companion project Twine provides a protocol-level interface to Beanstalkd that may be more appropriate for some applications.

Installation

Depend on [spine "0.0.3"] in your project.clj.

spine's People

Contributors

mmcgrana avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.