Browse the Ruby on Rails Community.

You are here: Browse Railsplugins Simplified Starling

Simplified Starling

Simplified Starling

Works with Starling 0.9.6 or edge. You can follow these steps to build it.

sudo gem install newgem eventmachine rails_analyzer_tools memcache-client
git clone git://github.com/anotherbritt/starling.git
cd starling && rake gem && cd pkg && sudo gem install starling-0.9.6.gem

From Blaine Cook repository ...

git clone git://github.com/blaine/starling.git
cd starling
gem build starling.spec
gem install -l starling-0.9.7.5.gem

Note: I don't know which is the master copy of starling and I'm waiting an email reply from the developers.

= Installing

Install this Rails plugin as usual.

script/plugin install git://github.com/fesplugas/simplified_starling.git

After installing the plugin you’ll find configuration files for each Rails environment under config/starling.

#

  1. RAILS_ROOT/config/starling/development.yml # starling: host: 127.0.0.1 port: 22122 pid_file: /tmp/starling.pid queue_path: /tmp timeout: 0 syslog_channel: starling-tampopo log_level: DEBUG daemonize: true queue: app_development

You’ll also find a initializer called starling.rb under config/initializers that will connect your application to the starling server.

=== How it works?

With starling installed in your machine you can start/stop/restart the daemon by running.

$ rake simplified:starling:start
$ rake simplified:starling:stop
$ rake simplified:starling:restart

You can put anything into Starling, but after using Starling on a couple of projects I’ve seen I use always the same hash for storing the jobs. This will push into the queue the task test_rendering which belongs to a model.

#

  1. From a model/controller # newsletter = Newsletter.find(params[:id]) newsletter.push(‘test_rendering’) => STARLING.set(‘app’, { :type => ‘Newsletter’, :id => 1, :task => ‘test_rendering’ })

You can even push a class task into the queue. This will push into the queue the task recalculate which belongs to a model.

#

  1. From a model/controller # Stock.push(‘recalculate’) => STARLING.set(‘app’, { :type => ‘Stock’, :task => ‘recalculate’ })

To view queue stats …

$ rake simplified:starling:stats

Processed jobs are logged.

$ tail -f log/development_starling.log
$ tail -f log/production_starling.log

=== Log

Each time a job is pushed and popped to the queue is logged.

[Pushed job @ 2008-06-30 11:06:03] Dispatch order
[Popped job @ 2008-06-30 11:06:03] Dispatch order

If database connection goes down or dies after a few hours of inactivity you’ll see a WARNING message, if everything went as expected, database connection will be restored and job will be sent again to the queue.

[Pushed job @ 2008-06-30 11:06:42] Rebuild page 3
[WARNING] Database connection gone, reconnecting & retrying.
          {:type=>"Order", :task=>"dispatch", :id=>nil}
[Pushed job @ 2008-06-30 11:06:44] Rebuild page 3 (R)
[Popped job @ 2008-06-30 11:06:44] Rebuild page 3

If the record you’re trying to process is removed from the database before the queue is processed you’ll see a warning on the logs.

[Pushed job @ 2008-06-30 11:06:50] Rebuild page 3
[WARNING] Page#3 gone from database.

=== Example 1

Push a newsletter job into starling.

#

  1. app/controllers/typus/newsletters_controller.rb # class Typus::NewslettersController < TypusController

    def test_deliver Newsletter.find(params[:id]).push(‘test_rendering’) flash[:notice] = “Newsletter added to queue.” redirect_to :back end

    def deliver Newsletter.find(params[:id]).push(‘deliver’) flash[:notice] = “Newsletter added to queue.” redirect_to :back end

end

#

  1. app/models/newsletter.rb # class Newsletter < ActiveRecord::Base

    def test_rendering ## # Your long-running task to deliver newsletter to test rendering # in diferent email readers. end

    def deliver ## # Long running task to deliver newsletter … # end

end

=== Example 2

Confirm an order payment and push into starling an stock recalculation job.

#

  1. app/controllers/payments_controller.rb class PaymentsController < ApplicationController

    def confirm @order = Order.find_by_token(params[:token]) @order.confirm_payment flash[:notice] = “Thanks for your purchase!” redirect_to :action => ‘thanks’ end

end

#

  1. app/models/order.rb class Order < ActiveRecord::Base

    def confirm_payment OrderMailer.deliver_payment_confirmation(order) Stock.push(‘recalculate’) end

end

#

  1. app/models/stock.rb class Stock < ActiveRecord::Base

    def self.recalculate ## # A long running operation … # end

end

== Acknowledgments

- Blaine Cook, Twitter Inc. for this nice queue system.

Copyright© 2008 Francesc Esplugas Marti, released under the MIT license

NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly

Users


See all details


Membership

+ Join this railsplugin

Record Maintainer

'None'