Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fnordmetric.options method to merge default, server_configuration and…

… given opts
  • Loading branch information...
commit abefad8b860513936d8f01e0cfeb506c18e242fd 1 parent 23970a7
Ross Kaffenberger rossta authored
Showing with 88 additions and 35 deletions.
  1. +21 −25 lib/fnordmetric.rb
  2. +11 −10 lib/fnordmetric/api.rb
  3. +56 −0 spec/fnordmetric_spec.rb
46 lib/fnordmetric.rb
View
@@ -20,37 +20,33 @@ def self.namespace(key=nil, &block)
def self.server_configuration=(configuration)
@@server_configuration = configuration
end
-
- def self.default_options(opts)
- opts[:redis_url] ||= "redis://localhost:6379"
- opts[:redis_prefix] ||= "fnordmetric"
-
- opts[:inbound_stream] ||= ["0.0.0.0", "1337"]
- opts[:web_interface] ||= ["0.0.0.0", "4242"]
- opts[:web_interface_server] ||= "thin"
-
- opts[:start_worker] ||= true
- opts[:print_stats] ||= 3
-
- # events that aren't processed after 2 min get dropped
- opts[:event_queue_ttl] ||= 120
-
- # event data is kept for one month
- opts[:event_data_ttl] ||= 3600*24*30
-
- # session data is kept for one month
- opts[:session_data_ttl] ||= 3600*24*30
+ def self.default_options(opts = {})
+ {
+ :redis_url => "redis://localhost:6379",
+ :redis_prefix => "fnordmetric",
+ :inbound_stream => ["0.0.0.0", "1337"],
+ :web_interface => ["0.0.0.0", "4242"],
+ :web_interface_server => "thin",
+ :start_worker => true,
+ :print_stats => 3,
+ :event_queue_ttl => 120,
+ :event_data_ttl => 3600*24*30,
+ :session_data_ttl => 3600*24*30
+ }.merge(opts)
+ end
- opts
+ def self.options(opts = {})
+ default_options(@@server_configuration || {}).merge(opts)
end
- def self.start_em(opts)
+ def self.start_em(opts = {})
EM.run do
trap("TERM", &method(:shutdown))
trap("INT", &method(:shutdown))
+ opts = options(opts)
app = embedded(opts)
if opts[:web_interface]
@@ -78,10 +74,10 @@ def self.error!(msg)
def self.run
opts = (defined?(@@server_configuration) && @@server_configuration) || {}
- start_em(opts)
+ start_em(opts)
rescue Exception => e
log "!!! eventmachine died, restarting... #{e.message}"
- sleep(1); run
+ sleep(1); run
end
def self.shutdown
@@ -113,7 +109,7 @@ def self.standalone
# `:inbound_stream` starts the TCP interface
# `:print_stats` periodicaly prints worker stats
def self.embedded(opts={})
- opts = default_options(opts)
+ opts = options(opts)
app = nil
if opts[:rack_app] or opts[:web_interface]
21 lib/fnordmetric/api.rb
View
@@ -1,19 +1,20 @@
require 'securerandom'
class FnordMetric::API
@@opts = nil
+
def initialize opts
@@opts = FnordMetric.default_options(opts)
connect
end
-
+
def connect
@redis = Redis.connect(:url => @@opts[:redis_url])
end
-
+
def event(event_data)
begin
if event_data.is_a?(Hash)
- event_data = event_data.to_json
+ event_data = event_data.to_json
else
JSON.parse(event_data) # void ;)
end
@@ -27,19 +28,19 @@ def event(event_data)
def disconnect
@redis.quit
end
-
- private
-
- def push_event(event_id, event_data)
- prefix = @@opts[:redis_prefix]
+
+ private
+
+ def push_event(event_id, event_data)
+ prefix = @@opts[:redis_prefix]
@redis.hincrby "#{prefix}-testdata", "events_received", 1
@redis.hincrby "#{prefix}-stats", "events_received", 1
@redis.set "#{prefix}-event-#{event_id}", event_data
- @redis.lpush "#{prefix}-queue", event_id
+ @redis.lpush "#{prefix}-queue", event_id
@redis.expire "#{prefix}-event-#{event_id}", @@opts[:event_queue_ttl]
event_id
end
-
+
def get_next_uuid
SecureRandom.uuid
end
56 spec/fnordmetric_spec.rb
View
@@ -0,0 +1,56 @@
+require 'spec_helper'
+
+describe FnordMetric do
+ after { FnordMetric.server_configuration = nil }
+
+ it "has default_options" do
+ opts = FnordMetric.default_options
+ opts[:redis_url].should eq("redis://localhost:6379")
+ opts[:redis_prefix].should eq("fnordmetric")
+ opts[:inbound_stream].should eq(["0.0.0.0", "1337"])
+ opts[:web_interface].should eq(["0.0.0.0", "4242"])
+ opts[:web_interface_server].should eq("thin")
+ opts[:start_worker].should be_true
+ opts[:print_stats].should == 3
+ opts[:event_queue_ttl].should == 120
+ opts[:event_data_ttl].should == 3600*24*30
+ opts[:session_data_ttl].should == 3600*24*30
+ end
+
+ it "default_options can be overriden" do
+ opts = FnordMetric.default_options({
+ :redis_url => "redis://example.com:6379",
+ :redis_prefix => "foobar",
+ :inbound_stream => ["0.0.0.0", "7331"],
+ :web_interface => ["0.0.0.0", "2424"],
+ :web_interface_server => "hatetepe",
+ :start_worker => false,
+ :print_stats => false,
+ :event_queue_ttl => 10,
+ :event_data_ttl => 20,
+ :session_data_ttl => 30,
+ })
+ opts[:redis_url].should eq("redis://example.com:6379")
+ opts[:redis_prefix].should eq("foobar")
+ opts[:inbound_stream].should eq(["0.0.0.0", "7331"])
+ opts[:web_interface].should eq(["0.0.0.0", "2424"])
+ opts[:web_interface_server].should eq("hatetepe")
+ opts[:start_worker].should be_false
+ opts[:print_stats].should be_false
+ opts[:event_queue_ttl].should == 10
+ opts[:event_data_ttl].should == 20
+ opts[:session_data_ttl].should == 30
+ end
+
+ it "merges default, server configuration, and given opts as options" do
+ FnordMetric.server_configuration = { :web_interface_server => "hatetepe", :print_stats => true }
+ opts = FnordMetric.options(:redis_url => "redis://example.com:6379", :print_stats => false)
+
+ opts[:redis_url].should eq("redis://example.com:6379")
+ opts[:redis_prefix].should eq("fnordmetric")
+ opts[:web_interface_server].should eq("hatetepe")
+ opts[:print_stats].should be_false
+ end
+
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.