class Firehose::Rack::Ping::PingCheck

Encapsulate this in a class so we aren’t passing a bunch of variables around

Constants

SECONDS_TO_EXPIRE
TEST_VALUE

Attributes

env[R]
key[R]
redis[R]
req[R]

Public Class Methods

new(env, redis=nil) click to toggle source
# File lib/firehose/rack/ping.rb, line 33
def initialize(env, redis=nil)
  @redis = redis || self.class.redis
  @env   = env
  @req   = env['parsed_request'] ||= ::Rack::Request.new(env)
  @key   = "/firehose/ping/#{Time.now.to_i}/#{rand}"
end
redis() click to toggle source
# File lib/firehose/rack/ping.rb, line 29
def self.redis
  @redis ||= Firehose::Server.redis.connection
end

Public Instance Methods

call() click to toggle source
# File lib/firehose/rack/ping.rb, line 40
def call
  log req, 'started'
  test_redis
end

Private Instance Methods

expire_key() click to toggle source
# File lib/firehose/rack/ping.rb, line 62
def expire_key
  redis.expire(key, SECONDS_TO_EXPIRE).
    errback do
      log req, "failed to expire key #{key.inspect}. If this key is not manually deleted, it may cause a memory leak."
    end
end
log(req, msg) click to toggle source
# File lib/firehose/rack/ping.rb, line 48
def log(req, msg)
  Firehose.logger.debug "HTTP PING request for path '#{req.path}': #{msg}"
end
read_and_respond() click to toggle source
# File lib/firehose/rack/ping.rb, line 69
def read_and_respond
  redis.get(key).
    callback do |val|
      if val == TEST_VALUE
        log req, 'succeeded'
        env['async.callback'].call response(200)
      else
        log req, "failed with unexpected value retrieved from redis: #{val.inspect}"
        env['async.callback'].call response(500)
      end
    end.
    errback do |e|
      log req, "failed with read value from redis: #{e.inspect}"
      env['async.callback'].call response(500)
    end
end
test_redis() click to toggle source
# File lib/firehose/rack/ping.rb, line 52
def test_redis
  redis.set(key, TEST_VALUE).
    callback { expire_key }.
    callback { read_and_respond }.
    errback do |e|
      log req, "failed with write value to redis: #{e.inspect}"
      env['async.callback'].call response(500)
    end
end