Commit 70f68b6f authored by Paul Asmuth's avatar Paul Asmuth
Browse files

merged topics/middleware from fd - implements fnordmetric#embedded #5

parents f62b4189 77cd3597
Loading
Loading
Loading
Loading
+49 −33
Original line number Diff line number Diff line
@@ -49,25 +49,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
@@ -75,13 +60,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

@@ -126,6 +104,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"
+12 −12

File changed.

Contains only whitespace changes.