class DynportTools::EmbeddedRedis

Attributes

base_path[RW]
custom_config[RW]
killed[RW]
logger[W]
started[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 12
def initialize(options = {})
  self.base_path = options[:base_path] || "/tmp/embedded_redis"
  self.logger = options[:logger] || Logger.new($stderr)
end
system(cmd) click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 53
def self.system(cmd)
  Kernel.send(:system, cmd)
end

Public Instance Methods

config() click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 109
def config
  default_config.merge(custom_config || {}).map { |key, value| ["#{key} #{value}"] }.join("\n")
end
connection() click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 72
def connection
  if !started?
    start 
  end
  @connection ||= Redis.new(:path => socket_path)
end
dbfile_path() click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 29
def dbfile_path
  "#{base_path}/#{dbfilename}"
end
dbfilename() click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 25
def dbfilename
  "redis.#{Process.pid}.rdb"
end
default_config() click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 102
def default_config
  { 
    :daemonize => "yes", :pidfile => pid_path, :port => 0, :unixsocket => socket_path, :dir => base_path, 
    :dbfilename  => dbfilename
  }
end
do_start!() click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 57
def do_start!
  FileUtils.mkdir_p(base_path)
  self.class.system(%(echo "#{config}" | redis-server -))
  sleep 0.1
  self.started = true
  log "started redis with pid #{pid}"
  at_exit do
    kill
  end
end
kill() click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 91
def kill
  log "killing redis"
  if !killed? && pid
    log "killing #{pid}"
    self.class.system(%(kill #{pid})) 
    FileUtils.rm_f(socket_path)
    FileUtils.rm_f(dbfile_path)
    self.killed = true
  end
end
killed?() click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 87
def killed?
  !!killed
end
log(message) click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 79
def log(message)
  logger.info("EMBEDDED_REDIS: #{message}")
end
logger() click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 83
def logger
  @logger ||= Logger.new($stdout)
end
pid() click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 33
def pid
  if File.exists?(pid_path)
    pid = File.read(pid_path).strip
    pid.length > 0 ? pid : nil
  end
end
pid_path() click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 17
def pid_path
  "#{base_path}/redis.#{Process.pid}.pid"
end
running?() click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 40
def running?
  !!(pid && IO.popen("ps -p #{pid} | grep redis-server").count > 0)
end
socket_path() click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 21
def socket_path
  "#{base_path}/redis.#{Process.pid}.socket"
end
start() click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 44
def start
  if !running?
    do_start!
  else
    log "already running with pid #{pid}"
  end
  connection
end
started?() click to toggle source
# File lib/dynport_tools/embedded_redis.rb, line 68
def started?
  !!self.started
end