Class: Redispot::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/redispot/server.rb

Defined Under Namespace

Classes: Killer

Instance Method Summary collapse

Constructor Details

#initialize(config: { }, timeout: 3, tmpdir: nil) {|connect_info| ... } ⇒ Server

Create a new instance, and start redis-server if block given.

Examples:

Redispot::Server.new do |connect_info|
  redis = Redis.new(connect_info)
  assert_equal('PONG', redis.ping)
end

Parameters:

  • config (Hash) (defaults to: { })

    This is a ‘redis.conf` key value pair. You can use any key-value pair(s) that redis-server supports.

  • timeout (Fixnum) (defaults to: 3)

    Timeout seconds for detecting if redis-server is awake or not.

  • tmpdir (String) (defaults to: nil)

    Temporal directory, where redis config will be stored.

Yields:

  • (connect_info)

    Connection info for client library to connect this redis-server instance.

Yield Parameters:

  • connect_info (String)

    This parameter is designed to pass directly to Redis module.



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_infoString

Return connection info for client library to connect this redis-server instance.

Examples:

redispot = Redispot::Server.new
redis    = Redis.new(redispot.connect_info)

Returns:

  • (String)

    This parameter is designed to pass directly to Redis module.



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 #startObject

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.

Examples:

redispot.start do |connect_info|
  redis = Redis.new(connect_info)
  assert_equal('PONG', redis.ping)
end

# or

connect_info = redispot.start

Overloads:

  • #start({ ... }) {|connect_info| ... } ⇒ Object

    Yields:

    • (connect_info)

      Connection info for client library to connect this redis-server instance.

    Yield Parameters:

    • connect_info (Hash)

      This parameter is designed to pass directly to Redis module.



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

#stopObject

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