Sha256: 78090e42e3763fd110a467c9597242556fc55cc7a33eff85a2579a88ce786bd5

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

require 'sinatra/base'
require 'json'

class RedisStat
class Server < Sinatra::Base
  configure do
    unless RUBY_PLATFORM == 'java'
      require 'thin'
      set :server, :thin
    end
    set :environment, :production
    set :root, File.join( File.dirname(__FILE__), 'server' )
    set :clients, []
  end

  get '/' do
    @hosts    = settings.redis_stat.hosts
    @info     = settings.redis_stat.info
    @measures = settings.redis_stat.measures
    erb :index
  end

  get '/pull' do
    content_type 'text/event-stream'

    stream(:keep_open) do |out|
      settings.clients << out
      out.callback { settings.clients.delete out }
    end
  end

  class << self
    def wait_until_running
      while !RedisStat::Server.running?
        sleep 0.5
      end
    end

    def push info, data
      static = Hash[RedisStat::MEASURES[:static].map { |stat|
        [stat, info[stat]]
      }]

      data = [
        "retry: 1000",
        "data: #{{:static => static, :dynamic => data}.to_json}",
        "\n"
      ].join("\n")

      settings.clients.each do |cl|
        cl << data
      end
    end
  end
end#Server
end#RedisStat

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redis-stat-0.3.0-java lib/redis-stat/server.rb
redis-stat-0.3.0 lib/redis-stat/server.rb