Screencast: Beanstalkd und Stalker

Beanstalkd ist ein einfacher, schneller und verteilt arbeitender Dienst zum Aufbau von Message Queues. Stalker ist ein Wrapper Interface um entsprechende Jobs in die Message Queue zu senden.

 

Download:

Download(13.6 MB, 9:16)
Alternativer Download für iPod & Apple TV(13.6 MB, 9:16)

 

Resourcen:

 

Quellcode:

[bash]
beanstalkd -d
killall beanstalkd
beanstalkd -d -b …
stalk ./config/jobs.rb
[/bash]

[ruby]
# 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

# config/god.rb
# 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
[/ruby]

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.