Commit 89005432 authored by John E. Vincent's avatar John E. Vincent

self-reconfiguring Sinatra example

parent 360dea62
......@@ -3,8 +3,8 @@
require File.join(File.dirname(__FILE__), '..','lib','noah','watcher')
class StdoutWatcher < Noah::Watcher
redis_host "redis://127.0.0.1:6379/6"
pattern "noah.Noah::Configuration*"
redis_host "redis://127.0.0.1:6379/0"
pattern "noah.Noah::Configuration.redis_server.update"
destination Proc.new {|x| puts x}
run!
end
#!/usr/bin/env ruby
require File.join(File.dirname(__FILE__), '..','lib','noah','watcher')
require 'excon'
class HttpPostWatch < Noah::Watcher
redis_host "redis://127.0.0.1:6379/0"
pattern "noah.Noah::Configuration.redis_server.update"
destination Proc.new {|x| puts x; ::Excon.put("http://localhost:4567/webhook", :headers => {"Content-Type" => "application/json"}, :body => x)}
run!
end
require 'sinatra'
require 'ohm'
require 'open-uri'
require 'json'
set :noah_server, 'http://localhost:5678'
set :noah_client_name, 'my_sinatra_app'
def get_config_from_noah(setting)
begin
c = open("#{settings.noah_server}/c/#{settings.noah_client_name}/#{setting}").read
set setting.to_sym, c
end
end
get_config_from_noah('redis_server')
def get_redis_version
Ohm.connect :url => settings.redis_server
Ohm.redis.info["redis_version"]
end
get "/" do
"Redis version: #{get_redis_version}"
end
put "/webhook" do
data = JSON.parse(request.body.read)
settings.redis_server = data["body"]
resp = {:message => "reconfigured", :setting => data["name"], :body => data["body"]}.to_json
"#{resp}"
end
......@@ -47,8 +47,10 @@ module Noah
define_method("notify_via_redis_#{meth}".to_sym) do
db = self.dbnum
self.name.nil? ? name=@deleted_name : name=self.name
pub_category = "#{db}:noah.#{self.class.to_s}[name].#{meth}"
Ohm.redis.publish(pub_category, self.to_json)
# Pulling out dbnum for now. Need to rethink it
#pub_category = "#{db}:noah.#{self.class.to_s}[#{name}].#{meth}"
pub_category = "noah.#{self.class.to_s}.#{name}.#{meth}"
Ohm.redis.publish(pub_category, self.to_hash.merge({"action" => meth, "pubcategory" => pub_category}).to_json)
end
end
end
......
......@@ -57,8 +57,10 @@ module Noah
trap("INT") { log.info "Interrupted"; EventMachine.stop }
channel = EventMachine::Channel.new
r = EventMachine::Hiredis::Client.connect(redis_url.host, redis_url.port)
log.info "Binding to pattern #{db}:#{@my_pattern}"
r.psubscribe("#{db}:#{@my_pattern}")
# Pulling out dbnum for now. Need to rethink it
#log.info "Binding to pattern #{db}:#{@my_pattern}"
log.info "Binding to pattern #{@my_pattern}"
r.psubscribe("#{@my_pattern}")
r.on(:pmessage) do |pattern, event, message|
log.debug "Got message"
channel.push "#{message}"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment