Class: Redispot::Server
- Inherits:
-
Object
- Object
- Redispot::Server
- Defined in:
- lib/redispot/server.rb
Defined Under Namespace
Classes: Killer
Instance Method Summary collapse
-
#connect_info ⇒ String
Return connection info for client library to connect this redis-server instance.
-
#initialize(config: { }, timeout: 3, tmpdir: nil) {|connect_info| ... } ⇒ Server
constructor
Create a new instance, and start redis-server if block given.
-
#start ⇒ Object
Start redis-server instance manually.
-
#stop ⇒ Object
Stop redis-server.
Constructor Details
#initialize(config: { }, timeout: 3, tmpdir: nil) {|connect_info| ... } ⇒ Server
Create a new instance, and start redis-server if block given.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/redispot/server.rb', line 20 def initialize (config: { }, timeout: 3, tmpdir: nil, &block) @executable = 'redis-server' @owner_pid = Process.pid @pid = nil @config = config.symbolize_keys @timeout = timeout @workdir = WorkingDirectory.new(tmpdir) if @config[:port].nil? && @config[:unixsocket].nil? @config[:unixsocket] = "#{@workdir}/redis.sock" @config[:port] = 0 end if @config[:dir].nil? @config[:dir] = @workdir end if @config[:loglevel].to_s == 'warning' $stderr.puts 'Redispot::Server does not support "loglevel warning", using "notice" instead.' @config[:loglevel] = 'notice' end start(&block) if block end |
Instance Method Details
#connect_info ⇒ String
Return connection info for client library to connect this redis-server instance.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/redispot/server.rb', line 107 def connect_info host = @config[:bind].presence || '0.0.0.0' port = @config[:port] if port.is_a?(Fixnum) && port > 0 { url: "redis://#{host}:#{port}/" } else { path: @config[:unixsocket] } end end |
#start({ ... }) {|connect_info| ... } ⇒ Object #start ⇒ Object
Start redis-server instance manually. If block given, the redis instance is available only within a block. Users must call Redispot::Server#stop they have called Redispot::Server#start without block.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/redispot/server.rb', line 64 def start return if @pid start_process if block_given? begin yield connect_info ensure stop end else connect_info end end |
#stop ⇒ Object
Stop redis-server. This method is automatically called from object destructor.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/redispot/server.rb', line 82 def stop return unless @pid signals = [:TERM, :INT, :KILL] begin Process.kill(signals.shift, @pid) Timeout.timeout(@timeout) { Process.waitpid(@pid) } rescue Timeout::Error => error retry unless signals.empty? raise error end @pid = nil ObjectSpace.undefine_finalizer(self) end |