File: sentinel.rb

package info (click to toggle)
ruby-redis 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,160 kB
  • sloc: ruby: 11,445; makefile: 117; sh: 24
file content (43 lines) | stat: -rw-r--r-- 1,029 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frozen_string_literal: true

require 'redis'

# This example creates a master-slave setup with a sentinel, then connects to
# it and sends write commands in a loop.
#
# After 30 seconds, the master dies. You will be able to see how a new master
# is elected and things continue to work as if nothing happened.
#
# To run this example:
#
#   $ ruby -I./lib examples/sentinel.rb
#

at_exit do
  begin
    Process.kill(:INT, @redises)
  rescue Errno::ESRCH
  end

  Process.waitall
end

@redises = spawn("examples/sentinel/start")

SENTINELS = [{ host: "127.0.0.1", port: 26_379 },
             { host: "127.0.0.1", port: 26_380 }].freeze
r = Redis.new(url: "redis://master1", sentinels: SENTINELS, role: :master)

# Set keys into a loop.
#
# The example traps errors so that you can actually try to failover while
# running the script to see redis-rb reconfiguring.
(0..1_000_000).each do |i|
  begin
    r.set(i, i)
    $stdout.write("SET (#{i} times)\n") if i % 100 == 0
  rescue
    $stdout.write("E")
  end
  sleep(0.01)
end