Permalink
Browse files

Added FnordMetric#embedded

There are still some problems with the CSS with need to be fixed.
See #5
  • Loading branch information...
1 parent f62b418 commit 554920f03d2af54c7d85c994e4cb85939acdbc68 @fd fd committed with ianlevesque Dec 29, 2011
Showing with 57 additions and 33 deletions.
  1. +57 −33 lib/fnordmetric.rb
View
@@ -12,8 +12,8 @@ module FnordMetric
@@namespaces = {}
- def self.namespace(key=nil, &block)
- @@namespaces[key] = block
+ def self.namespace(key=nil, &block)
+ @@namespaces[key] = block
end
def self.server_configuration=(configuration)
@@ -23,7 +23,7 @@ def self.server_configuration=(configuration)
def self.default_options(opts)
opts[:redis_url] ||= "redis://localhost:6379"
- opts[:redis_prefix] ||= "fnordmetric"
+ opts[:redis_prefix] ||= "fnordmetric"
opts[:inbound_stream] ||= ["0.0.0.0", "1337"]
opts[:web_interface] ||= ["0.0.0.0", "4242"]
@@ -38,8 +38,8 @@ def self.default_options(opts)
opts[:event_data_ttl] ||= 3600*24*30
# session data is kept for one month
- opts[:session_data_ttl] ||= 3600*24*30
-
+ opts[:session_data_ttl] ||= 3600*24*30
+
opts
end
@@ -49,40 +49,18 @@ def self.start_em(opts)
trap("TERM", &method(:shutdown))
trap("INT", &method(:shutdown))
- opts = default_options(opts)
-
- if opts[:start_worker]
- worker = Worker.new(@@namespaces.clone, opts)
- worker.ready!
- end
-
- if opts[:inbound_stream]
- begin
- inbound_stream = InboundStream.start(opts)
- log "listening on tcp##{opts[:inbound_stream].join(":")}"
- rescue
- log "cant start FnordMetric::InboundStream. port in use?"
- end
- end
+ app = embedded(opts)
if opts[:web_interface]
- begin
- app = FnordMetric::App.new(@@namespaces.clone, opts)
+ begin
Thin::Server.start(*opts[:web_interface], app)
log "listening on http##{opts[:web_interface].join(":")}"
rescue Exception => e
log "cant start FnordMetric::App. port in use?"
end
end
- if opts[:print_stats]
- redis = connect_redis(opts[:redis_url])
- EM::PeriodicTimer.new(opts[:print_stats]) do
- print_stats(opts, redis)
- end
- end
-
- end
+ end
end
def self.log(msg)
@@ -94,12 +72,20 @@ def self.error!(msg)
puts(msg); exit!
end
+<<<<<<< HEAD
def self.run
opts = (defined?(@@server_configuration) && @@server_configuration) || {}
start_em(opts)
rescue Exception => e
log "!!! eventmachine died, restarting... #{e.message}"
sleep(1); run
+=======
+ def self.run(opts={})
+ start_em(opts)
+ rescue Exception => e
+ log "!!! eventmachine died, restarting... #{e.message}"
+ sleep(1); run(opts)
+>>>>>>> Added FnordMetric#embedded
end
def self.shutdown
@@ -113,19 +99,57 @@ def self.connect_redis(redis_url)
def self.print_stats(opts, redis) # FIXME: refactor this mess
keys = [:events_received, :events_processed]
- redis.llen("#{opts[:redis_prefix]}-queue") do |queue_length|
+ redis.llen("#{opts[:redis_prefix]}-queue") do |queue_length|
redis.hmget("#{opts[:redis_prefix]}-stats", *keys) do |data|
data_human = keys.size.times.map{|n|"#{keys[n]}: #{data[n]}"}
log "#{data_human.join(", ")}, queue_length: #{queue_length}"
- end
+ end
end
end
- def self.standalone
+ def self.standalone
require "fnordmetric/logger"
require "fnordmetric/standalone"
end
+ # returns a Rack app which can be mounted under any path.
+ # `:start_worker` starts a worker
+ # `:inbound_stream` starts the TCP interface
+ # `:print_stats` periodicaly prints worker stats
+ def self.embedded(opts={})
+ opts = default_options(opts)
+ app = nil
+
+ if opts[:rack_app] or opts[:web_interface]
+ app = FnordMetric::App.new(@@namespaces.clone, opts)
+ end
+
+ EM.next_tick do
+ if opts[:start_worker]
+ worker = Worker.new(@@namespaces.clone, opts)
+ worker.ready!
+ end
+
+ if opts[:inbound_stream]
+ begin
+ inbound_stream = InboundStream.start(opts)
+ log "listening on tcp##{opts[:inbound_stream].join(":")}"
+ rescue
+ log "cant start FnordMetric::InboundStream. port in use?"
+ end
+ end
+
+ if opts[:print_stats]
+ redis = connect_redis(opts[:redis_url])
+ EM::PeriodicTimer.new(opts[:print_stats]) do
+ print_stats(opts, redis)
+ end
+ end
+ end
+
+ app
+ end
+
end
require "fnordmetric/inbound_stream"

0 comments on commit 554920f

Please sign in to comment.