|
| 1 | +Stalker - a job queueing DSL for Beanstalk |
| 2 | +========================================== |
| 3 | + |
| 4 | +[Beanstalkd](http://kr.github.com/beanstalkd/) is a fast, lightweight queueing backend inspired by mmemcached. The [Ruby Beanstalk client](http://beanstalk.rubyforge.org/) is a bit raw, however, so Stalker provides a thin wrapper to make job queueing from your Ruby app easy and fun. |
| 5 | + |
| 6 | +Queueing jobs |
| 7 | +------------- |
| 8 | + |
| 9 | +From anywhere in your app: |
| 10 | + |
| 11 | + require 'stalker' |
| 12 | + |
| 13 | + Stalker.enqueue('email.send', :to => '[email protected]') |
| 14 | + Stalker.enqueue('post.cleanup.all') |
| 15 | + Stalker.enqueue('post.cleanup', :id => post.id) |
| 16 | + |
| 17 | +Working jobs |
| 18 | +------------ |
| 19 | + |
| 20 | +In a standalone file, typically jobs.rb or worker.rb: |
| 21 | + |
| 22 | + require 'stalker' |
| 23 | + include Stalker |
| 24 | + |
| 25 | + job 'email.send' do |args| |
| 26 | + Pony.send(:to => args['to'], :subject => "Hello there") |
| 27 | + end |
| 28 | + |
| 29 | + job 'post.cleanup.all' do |args| |
| 30 | + Post.all.each do |post| |
| 31 | + enqueue('post.cleanup', :id => post.all) |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + job 'post.cleanup' do |args| |
| 36 | + Post.find(args['id']).cleanup |
| 37 | + end |
| 38 | + |
| 39 | +Running |
| 40 | +------- |
| 41 | + |
| 42 | +First, make sure you have Beanstalkd installed and running: |
| 43 | + |
| 44 | + $ sudo port install beanstalkd |
| 45 | + $ beanstalkd |
| 46 | + |
| 47 | +Stalker: |
| 48 | + |
| 49 | + $ sudo gem install stalker |
| 50 | + |
| 51 | +Now run a worker using the stalk binary: |
| 52 | + |
| 53 | + $ stalk jobs.rb |
| 54 | + [Sat Apr 17 14:13:40 -0700 2010] Working 3 jobs :: [ email.send post.cleanup.all post.cleanup ] |
| 55 | + |
| 56 | +Stalker will log to stdout as it starts working each job. |
| 57 | + |
| 58 | +Filter to a list of jobs you wish to run with an argument: |
| 59 | + |
| 60 | + $ stalk jobs.rb post.cleanup.all,post.cleanup |
| 61 | + [Sat Apr 17 14:13:40 -0700 2010] Working 2 jobs :: [ post.cleanup.all post.cleanup ] |
| 62 | + |
| 63 | +In a production environment you may run one or more high-priority workers (limited to short/urgent jobs) and any number of regular workers (working all jobs). For example, two workers working just the email.send job, and four running all jobs: |
| 64 | + |
| 65 | + $ for i in 1 2; do stalk jobs.rb email.send > log/urgent-worker.log 2>&1; end |
| 66 | + $ for i in 1 2 3 4; do stalk jobs.rb > log/worker.log 2>&1; end |
| 67 | + |
| 68 | +Tidbits |
| 69 | +------- |
| 70 | + |
| 71 | +* Jobs are serialized as JSON, so you should stick to strings, integers, arrays, and hashes as arguments to jobs. e.g. don't pass full Ruby objects - use something like an ActiveRecord/MongoMapper/CouchRest id instead. |
| 72 | +* Because there are no class definitions associated with jobs, you can queue jobs from anywhere without needing to include your full app's environment. |
| 73 | +* If you need to change the location of your Beanstalk from the default (localhost:11300), set BEANSTALK_URL in your environment, e.g. export BEANSTALK_URL=beanstalk://example.com:11300/ |
| 74 | +* The stalk binary is just for convenience, you can also run a worker with a straight Ruby command: |
| 75 | + $ ruby -r jobs -e Stalker.work |
| 76 | + |
| 77 | +Meta |
| 78 | +---- |
| 79 | + |
| 80 | +Created by Adam Wiggins |
| 81 | + |
| 82 | +Heavily inspired by [Minion](http://github.com/orionz/minion) by Orion Henry |
| 83 | + |
| 84 | +Released under the MIT License: http://www.opensource.org/licenses/mit-license.php |
| 85 | + |
| 86 | +http://github.com/adamwiggins/stalker |
| 87 | + |
0 commit comments