Class: DynportTools::EmbeddedRedis
- Inherits:
-
Object
- Object
- DynportTools::EmbeddedRedis
- Includes:
- Singleton
- Defined in:
- lib/dynport_tools/embedded_redis.rb
Instance Attribute Summary collapse
-
#base_path ⇒ Object
Returns the value of attribute base_path.
-
#custom_config ⇒ Object
Returns the value of attribute custom_config.
-
#killed ⇒ Object
Returns the value of attribute killed.
- #logger ⇒ Object
-
#started ⇒ Object
Returns the value of attribute started.
Instance Method Summary collapse
- #config ⇒ Object
- #connection ⇒ Object
- #dbfile_path ⇒ Object
- #dbfilename ⇒ Object
- #default_config ⇒ Object
- #do_start! ⇒ Object
-
#initialize(options = {}) ⇒ EmbeddedRedis
constructor
A new instance of EmbeddedRedis.
- #kill ⇒ Object
- #killed? ⇒ Boolean
- #log(message) ⇒ Object
- #pid ⇒ Object
- #pid_path ⇒ Object
- #running? ⇒ Boolean
- #socket_path ⇒ Object
- #start ⇒ Object
- #started? ⇒ Boolean
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( = {}) self.base_path = [:base_path] || "/tmp/embedded_redis" self.logger = [:logger] || Logger.new($stderr) end |
Instance Attribute Details
#base_path ⇒ Object
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_config ⇒ Object
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 |
#killed ⇒ Object
Returns the value of attribute killed.
9 10 11 |
# File 'lib/dynport_tools/embedded_redis.rb', line 9 def killed @killed end |
#logger ⇒ Object
79 80 81 |
# File 'lib/dynport_tools/embedded_redis.rb', line 79 def logger @logger ||= Logger.new($stdout) end |
#started ⇒ Object
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
#config ⇒ Object
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 |
#connection ⇒ Object
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_path ⇒ Object
29 30 31 |
# File 'lib/dynport_tools/embedded_redis.rb', line 29 def dbfile_path "#{base_path}/#{dbfilename}" end |
#dbfilename ⇒ Object
25 26 27 |
# File 'lib/dynport_tools/embedded_redis.rb', line 25 def dbfilename "redis.#{Process.pid}.rdb" end |
#default_config ⇒ Object
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 |
#kill ⇒ Object
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
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() logger.info("EMBEDDED_REDIS: #{}") end |
#pid ⇒ Object
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_path ⇒ Object
17 18 19 |
# File 'lib/dynport_tools/embedded_redis.rb', line 17 def pid_path "#{base_path}/redis.#{Process.pid}.pid" end |
#running? ⇒ 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_path ⇒ Object
21 22 23 |
# File 'lib/dynport_tools/embedded_redis.rb', line 21 def socket_path "#{base_path}/redis.#{Process.pid}.socket" end |
#start ⇒ Object
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
64 65 66 |
# File 'lib/dynport_tools/embedded_redis.rb', line 64 def started? !!self.started end |