public
Description:
Homepage:
Clone URL: git://github.com/lukeredpath/beanstalk-messaging.git
Luke Redpath (author)
Fri Nov 14 08:27:30 -0800 2008
commit  a7737ea40f11f4f50ae303f27cc5a4bee629663c
tree    7d4ea27a1bb0bb12a125eff831ca4ede1b923fd0
parent  f0d394345185064564e34a0921b322944f19d750 parent  d2b88cae44349fdabfc98917253f44869175870a
name age message
file README.textile Mon Oct 13 05:40:14 -0700 2008 Just a test commit git-svn-id: http://svn/rep... [lukeredpath]
file Rakefile Fri Jul 25 03:08:56 -0700 2008 Merge branch 'master' into revieworld git-svn... [lukeredpath]
directory config/ Fri Jan 25 04:10:19 -0800 2008 Removed production config templates and made th... [lukeredpath]
directory examples/ Fri Jul 25 03:08:56 -0700 2008 Merge branch 'master' into revieworld git-svn... [lukeredpath]
file init.rb Fri Jul 25 03:08:56 -0700 2008 Merge branch 'master' into revieworld git-svn... [lukeredpath]
directory lib/ Fri Nov 14 07:34:14 -0800 2008 If we want multiple apps to control the same se... [lukeredpath]
directory script/ Fri Nov 14 08:24:02 -0800 2008 Missing require git-svn-id: http://svn/repos/... [lukeredpath]
directory tasks/ Mon Jan 14 07:13:08 -0800 2008 A basic statistics printer for the configured b... [lukeredpath]
directory test/ Fri Nov 14 08:23:08 -0800 2008 Consumer threads in polling acceptance tests *m... [lukeredpath]
file uninstall.rb Wed Jan 09 07:30:37 -0800 2008 Initial import of skeleton beanstalk_messaging ... [lukeredpath]
directory vendor/ Fri Jul 25 03:37:18 -0700 2008 Vendorised mocha 0.9 as we dont have it install... [lukeredpath]

Beanstalk Messaging Plugin for Rails

The Beanstalk Messaging Plugin provides a simple interface to the beanstalkd queuing process.

Configuration

Copy beanstalk.example.yml from the plugin config folder to your Rails config folder and configure the queues that you need for your application. Use script/beanstalk to start/stop/restart your beanstalkd processes.

Using the Beanstalk::QueueManager

Use the Beanstalk::QueueManager class to access your queues by name, using the Beanstalk::QueueManager#queue method. For example:

queue_manager = BeanStalk::QueueManager.new("beanstalk-config.yml")
my_queue = queue_manager.queue(:my_queue)
my_queue << "Here's a message"

In your environment.rb file, you should create a single instance of a Beanstalk::QueueManager using your beanstalk.yml config as a global constant that you can use throughout your app. This will ensure that each request to QueueManager.queue returns the same Beanstalk::Queue instance. For example, you may wish to add the following to the bottom of environment.rb:

QUEUE_MANAGER = Beanstalk::QueueManager.new(File.join(RAILS_ROOT, 'config', 'beanstalk.yml'))

You can disable queues globally by using the Beanstalk::QueueManager#disable method to temporarily disable the use of that particular queue. Beanstalk::QueueManager#disable_all disables all queues. When a queue is disabled, the Beanstalk::QueueManager will return a Beanstalk::NullQueue instead of an actual Beanstalk::Queue object which will handle any Beanstalk::Queue API calls silently:


# In environment.rb
QUEUE_MANAGER.disable(:my_queue)

## Elsewhere in your application...
queue = QUEUE_MANAGER.queue(:my_queue)
100.times { queue << "In /dev/null, noone can hear you scream..." }
## Nothing actually gets sent to the beanstalkd process

Using Beanstalk::Queue

Use the Beanstalk::Queue API to push messages on to the queue and consume messages. This amounts to using Beanstalk::Queue#push (or Beanstalk::Queue#<<) to add messages to the queue, and Beanstalk::Queue#next_message to retrieve messages. Note that you do not need to transform the data into YAML – this is performed automatically by the queue.


queue << [1,2,3]
# => "---\n1\n2\n3" # YAML version ends up on the queue

queue.next_message
# => [1,2,3] # YAML deserialization is performed automatically