Commit 7a379f2f authored by Simon Menke's avatar Simon Menke
Browse files

Added FnordMetric#embedded

There are still some problems with the CSS with need to be fixed.
See #5
parent 263f429f
Loading
Loading
Loading
Loading
+52 −36
Original line number Diff line number Diff line
@@ -45,25 +45,10 @@ module FnordMetric
      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)
          Thin::Server.start(*opts[:web_interface], app)
          log "listening on http##{opts[:web_interface].join(":")}"
        rescue Exception => e
@@ -71,13 +56,6 @@ module FnordMetric
        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

@@ -121,6 +99,44 @@ module FnordMetric
    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"