• Stars
    star
    166
  • Rank 227,126 (Top 5 %)
  • Language
    Ruby
  • Created over 14 years ago
  • Updated over 13 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A minimalist queueing DSL for Beanstalk.

This repo is unmaintained

Please go to the new official fork

Stalker - a job queueing DSL for Beanstalk

Beanstalkd is a fast, lightweight queueing backend inspired by mmemcached. The Ruby Beanstalk client is a bit raw, however, so Stalker provides a thin wrapper to make job queueing from your Ruby app easy and fun.

Queueing jobs

From anywhere in your app:

require 'stalker'

Stalker.enqueue('email.send', :to => '[email protected]')
Stalker.enqueue('post.cleanup.all')
Stalker.enqueue('post.cleanup', :id => post.id)

Working jobs

In a standalone file, typically jobs.rb or worker.rb:

require 'stalker'
include Stalker

job 'email.send' do |args|
  Pony.send(:to => args['to'], :subject => "Hello there")
end

job 'post.cleanup.all' do |args|
  Post.all.each do |post|
    enqueue('post.cleanup', :id => post.all)
  end
end

job 'post.cleanup' do |args|
  Post.find(args['id']).cleanup
end

Running

First, make sure you have Beanstalkd installed and running:

$ sudo port install beanstalkd
$ beanstalkd

Stalker:

$ sudo gem install stalker

Now run a worker using the stalk binary:

$ stalk jobs.rb
Working 3 jobs: [ email.send post.cleanup.all post.cleanup ]
Working send.email ([email protected])
Finished send.email in 31ms

Stalker will log to stdout as it starts working each job, and then again when the job finishes including the ellapsed time in milliseconds.

Filter to a list of jobs you wish to run with an argument:

$ stalk jobs.rb post.cleanup.all,post.cleanup
Working 2 jobs: [ post.cleanup.all post.cleanup ]

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:

$ for i in 1 2; do stalk jobs.rb email.send > log/urgent-worker.log 2>&1; end
$ for i in 1 2 3 4; do stalk jobs.rb > log/worker.log 2>&1; end

Error Handling

If you include an error block in your jobs definition, that block will be invoked when a worker encounters an error. You might use this to report errors to an external monitoring service:

error do |e, job, args|
  Exceptional.handle(e)
end

Before filter

If you wish to run a block of code prior to any job:

before do |job|
  puts "About to work #{job}"
end

Tidbits

  • 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.
  • Because there are no class definitions associated with jobs, you can queue jobs from anywhere without needing to include your full app's environment.
  • 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/
  • The stalk binary is just for convenience, you can also run a worker with a straight Ruby command: $ ruby -r jobs -e Stalker.work

Running the tests

If you wish to hack on Stalker, install these extra gems:

$ gem install contest mocha

Make sure you have a beanstalkd running, then run the tests:

$ ruby test/stalker_test.rb

Meta

Created by Adam Wiggins

Patches from Jamie Cobbett, Scott Water, Keith Rarick, Mark McGranaghan, Sean Walberg, Adam Pohorecki

Heavily inspired by Minion by Orion Henry

Released under the MIT License: http://www.opensource.org/licenses/mit-license.php

http://github.com/adamwiggins/stalker

More Repositories

1

clockwork

A scheduler process to replace cron
Ruby
659
star
2

rush

Ruby replacement for bash+ssh
Ruby
517
star
3

rest-client

Simple REST client for Ruby, inspired by microframework syntax for specifying actions. Official fork is now at:
Ruby
480
star
4

scanty

The blog that's almost nothing
Ruby
430
star
5

12factor

Official fork now at: https://github.com/heroku/12factor
Ruby
403
star
6

pony

The official fork is now maintained by benprew in http://github.com/benprew/pony
Ruby
233
star
7

yaml_db

Rails plugin for a database-independent dump format, data.yml. This fork no longer maintained, please see:
Ruby
219
star
8

sumo

Launch ec2 instances, pronto
Ruby
163
star
9

rifgraf

Fire-and-forget data collection and graphing service
JavaScript
141
star
10

clockwork-sinatra-beanstalk

A sample app using Clockwork with a Sinatra/Beanstalk app to fetch market prices periodically
Ruby
46
star
11

lua-pong

Very simple example of a game engine written in C and game logic written in Lua.
C
38
star
12

qfeedreader

A web-based feed reader, comparing queueing with Delayed::Job, Minion, and Stalker
Ruby
38
star
13

gmail_smtp

rails plugin to send email via gmail smtp
Ruby
35
star
14

scanty-redis

The blog that's almost nothing - now on Redis
Ruby
31
star
15

bitswiki

A sweet wiki written in Rails.
Ruby
31
star
16

clockwork-rails-dj

A sample app using Clockwork with a Rails/DJ app to fetch market prices periodically
Ruby
28
star
17

heroku-client

This fork is not maintained, please see: http://github.com/heroku/heroku
Ruby
25
star
18

cachemanifest

simple example of an html5 cache manifest, deployable to heroku
Ruby
22
star
19

nginx-as-heroku-app

Runing Nginx as a Heroku app with the C buildpack
C
15
star
20

example-nested-resources

An example of nested resources in Rails 2
14
star
21

peruse

Ruby
11
star
22

cocos2d

Python 2D game library, unofficial import from svn
Python
11
star
23

wordpress-cleardb

Wordpress on Heroku, using the ClearDB MySQL add-on free plan.
PHP
10
star
24

postgres-app

C
9
star
25

chef-cookbooks

Ruby
8
star
26

sigterm

Demonstration of graceful shutdown on SIGTERM
Ruby
7
star
27

erowid-mobile

Mobile site prototype for erowid.org
Ruby
7
star
28

sampleapp-sms

JavaScript
6
star
29

s5

S3 slide show
Ruby
6
star
30

merb-on-heroku

Blank Merb app with tweaks for Heroku deployment
Ruby
5
star
31

rack-maintenance

Ruby
5
star
32

ramaze-on-heroku

Ruby
4
star
33

qfeedreader-minion

Ruby
3
star
34

whatswrong

Ruby
3
star
35

redx-dashboard

Example dashboard which consumes event stream from redx-backend
JavaScript
3
star
36

queuedist

Ruby
3
star
37

blendproof

Python
3
star
38

heroku_suspenders

thoughtbot's Suspenders modified for Heroku.
Ruby
3
star
39

redx-backend

Example of Redis backend which forwards events vis https
JavaScript
3
star
40

heroku-branch-app

Heroku client plugin that picks app to use based on branch == remote
Ruby
2
star
41

tinkerdb

Ruby
2
star
42

wedgeguard

Rack middleware for request timeouts
Ruby
2
star
43

hello-node

JavaScript
2
star
44

trydjango

Python
2
star
45

heroku-releases

Ruby
2
star
46

hoptoad_notifier

Reports exceptions to Hoptoad
Ruby
2
star
47

hello-world

JavaScript
1
star
48

cacheit

Ruby
1
star
49

app-with-bundler-0.8-bundled

Ruby
1
star
50

ey-cloud-recipes

A starter repo for custom chef recipes on EY's cloud platform
Ruby
1
star
51

rails31-asset-sync

Ruby
1
star
52

lang-test-case

1
star
53

lumberjack

Ruby
1
star
54

assetbench

Ruby
1
star
55

logarchive

Ruby
1
star
56

psmanage

Ruby
1
star
57

churner

Ruby
1
star
58

nav-ruby19

Ruby
1
star
59

memcache-auth-test

Ruby
1
star
60

sampletest

1
star
61

heroku-devclouds

Ruby
1
star