Class: DynportTools::EmbeddedRedis

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/dynport_tools/embedded_redis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ EmbeddedRedis

Returns a new instance of EmbeddedRedis.



12
13
14
15
# 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

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



9
10
11
# File 'lib/dynport_tools/embedded_redis.rb', line 9

def base_path
  @base_path
end

#custom_configObject

Returns the value of attribute custom_config.



9
10
11
# File 'lib/dynport_tools/embedded_redis.rb', line 9

def custom_config
  @custom_config
end

#killedObject

Returns the value of attribute killed.



9
10
11
# File 'lib/dynport_tools/embedded_redis.rb', line 9

def killed
  @killed
end

#loggerObject



79
80
81
# File 'lib/dynport_tools/embedded_redis.rb', line 79

def logger
  @logger ||= Logger.new($stdout)
end

#startedObject

Returns the value of attribute started.



9
10
11
# File 'lib/dynport_tools/embedded_redis.rb', line 9

def started
  @started
end

Instance Method Details

#configObject



105
106
107
# File 'lib/dynport_tools/embedded_redis.rb', line 105

def config
  default_config.merge(custom_config || {}).map { |key, value| ["#{key} #{value}"] }.join("\n")
end

#connectionObject



68
69
70
71
72
73
# File 'lib/dynport_tools/embedded_redis.rb', line 68

def connection
  if !started?
    start 
  end
  @connection ||= Redis.new(:path => socket_path)
end

#dbfile_pathObject



29
30
31
# File 'lib/dynport_tools/embedded_redis.rb', line 29

def dbfile_path
  "#{base_path}/#{dbfilename}"
end

#dbfilenameObject



25
26
27
# File 'lib/dynport_tools/embedded_redis.rb', line 25

def dbfilename
  "redis.#{Process.pid}.rdb"
end

#default_configObject



98
99
100
101
102
103
# File 'lib/dynport_tools/embedded_redis.rb', line 98

def default_config
  { 
    :daemonize => "yes", :pidfile => pid_path, :port => 0, :unixsocket => socket_path, :dir => base_path, 
    :dbfilename  => dbfilename
  }
end

#do_start!Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/dynport_tools/embedded_redis.rb', line 53

def do_start!
  FileUtils.mkdir_p(base_path)
  system(%(echo "#{config}" | redis-server -))
  sleep 0.1
  self.started = true
  log "started redis with pid #{pid}"
  at_exit do
    kill
  end
end

#killObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/dynport_tools/embedded_redis.rb', line 87

def kill
  log "killing redis"
  if !killed? && pid
    log "killing #{pid}"
    system(%(kill #{pid})) 
    FileUtils.rm_f(socket_path)
    FileUtils.rm_f(dbfile_path)
    self.killed = true
  end
end

#killed?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/dynport_tools/embedded_redis.rb', line 83

def killed?
  !!killed
end

#log(message) ⇒ Object



75
76
77
# File 'lib/dynport_tools/embedded_redis.rb', line 75

def log(message)
  logger.info("EMBEDDED_REDIS: #{message}")
end

#pidObject



33
34
35
36
37
38
# 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_pathObject



17
18
19
# File 'lib/dynport_tools/embedded_redis.rb', line 17

def pid_path
  "#{base_path}/redis.#{Process.pid}.pid"
end

#running?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/dynport_tools/embedded_redis.rb', line 40

def running?
  !!(pid && IO.popen("ps -p #{pid} | grep redis-server").count > 0)
end

#socket_pathObject



21
22
23
# File 'lib/dynport_tools/embedded_redis.rb', line 21

def socket_path
  "#{base_path}/redis.#{Process.pid}.socket"
end

#startObject



44
45
46
47
48
49
50
51
# 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?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/dynport_tools/embedded_redis.rb', line 64

def started?
  !!self.started
end