#243 Beanstalkd and Stalker
Dec 06, 2010 | 9 minutes | Plugins, Background Jobs
Beanstalk is a fast and easy way to queue background tasks. Stalker provides a nice wrapper interface for creating these jobs.
- Download:
- source codeProject Files in Zip (96.3 KB)
- mp4Full Size H.264 Video (15.9 MB)
- m4vSmaller H.264 Video (9.97 MB)
- webmFull Size VP8 Video (24.3 MB)
- ogvFull Size Theora Video (19.8 MB)
Resources
bash
beanstalkd -d killall beanstalkd beanstalkd -d -b ... stalk ./config/jobs.rb
Gemfile
gem 'stalker' # config/jobs.rb with Rails require File.expand_path("../environment", __FILE__) job "city.fetch_name" do |args| City.find(args["id"]).fetch_name end
cities_controller.rb
Stalker.enqueue("city.fetch_name", :id => @city.id) # config/jobs.rb without Rails require "sqlite3" require "json" require "net/http" RAILS_ENV = ENV["RAILS_ENV"] || "development" db = SQLite3::Database.new(File.expand_path("../../db/#{RAILS_ENV}.sqlite3", __FILE__)) job "city.fetch_name" do |args| zip = db.get_first_value("select zip_code from cities where id=?", args["id"]) url = "http://ws.geonames.org/postalCodeLookupJSON?postalcode=#{zip}&country=US" json = Net::HTTP.get_response(URI.parse(url)).body name = JSON.parse(json)["postalcodes"].first["placeName"] db.execute("update cities set name=? where id=?", name, args["id"]) end error do |exception| # ... end
run with: god -c config/god.rb
RAILS_ROOT = File.expand_path("../..", __FILE__) God.watch do |w| w.name = "anycity-worker" w.interval = 30.seconds w.env = {"RAILS_ENV" => "production"} w.start = "/usr/bin/stalk #{RAILS_ROOT}/config/jobs.rb" w.log = "#{RAILS_ROOT}/log/stalker.log" w.start_if do |start| start.condition(:process_running) do |c| c.running = false end end w.restart_if do |restart| restart.condition(:memory_usage) do |c| c.above = 50.megabytes c.times = [3, 5] # 3 out of 5 intervals end restart.condition(:cpu_usage) do |c| c.above = 50.percent c.times = 5 end end w.lifecycle do |on| on.condition(:flapping) do |c| c.to_state = [:start, :restart] c.times = 5 c.within = 5.minute c.transition = :unmonitored c.retry_in = 10.minutes c.retry_times = 5 c.retry_within = 2.hours end end end