-
Notifications
You must be signed in to change notification settings - Fork 57
/
redis_server_helper.rb
118 lines (104 loc) · 2.5 KB
/
redis_server_helper.rb
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# frozen_string_literal: true
require "pathname"
module RedisServerHelper
module_function
ROOT = Pathname.new(File.expand_path("../../", __dir__))
CERTS_PATH = ROOT.join("test/docker/files/certs")
PID_FILE = ROOT.join("tmp/redis.pid")
SOCKET_FILE = if ENV["CI"]
Pathname.new("/var/redis/redis.sock")
else
ROOT.join("tmp/redis.sock")
end
HOST = "127.0.0.1"
TCP_PORT = 1_6379
TLS_PORT = 2_6379
REAL_TCP_PORT = 1_6380
REAL_TLS_PORT = 2_6380
PASSWORD = "hunter2"
def tcp_config
{
host: HOST,
port: TCP_PORT,
timeout: 0.1,
}
end
def ssl_config
{
host: HOST,
port: TLS_PORT,
timeout: 0.1,
ssl: true,
ssl_params: {
verify_hostname: false, # TODO: See if we could actually verify the hostname with our CI and dev setup
cert: OpenSSL::X509::Certificate.new(CERTS_PATH.join("client.crt").read),
key: OpenSSL::PKey.read(CERTS_PATH.join("client.key").read),
ca_file: CERTS_PATH.join("ca.crt").to_s,
},
}
end
def unix_config
{
path: SOCKET_FILE.to_s,
timeout: 0.1,
}
end
def spawn
if alive?
puts "redis-server already running with pid=#{pid}"
else
PID_FILE.parent.mkpath
print "starting redis-server... "
pid = Process.spawn(
"redis-server",
"--unixsocket", SOCKET_FILE.to_s,
"--unixsocketperm", "700",
"--port", REAL_TCP_PORT.to_s,
"--tls-port", REAL_TLS_PORT.to_s,
"--tls-cert-file", CERTS_PATH.join("redis.crt").to_s,
"--tls-key-file", CERTS_PATH.join("redis.key").to_s,
"--tls-ca-cert-file", CERTS_PATH.join("ca.crt").to_s,
"--save", "",
"--appendonly", "no",
out: ROOT.join("tmp/redis.log").to_s,
err: ROOT.join("tmp/redis.log").to_s,
)
PID_FILE.write(pid.to_s)
print "started with pid=#{pid}... "
wait_until_ready
puts "ready."
end
end
def wait_until_ready(timeout: 5)
(timeout * 100).times do
TCPSocket.new(HOST, TCP_PORT)
return true
rescue Errno::ECONNREFUSED
sleep 0.01
end
false
end
def shutdown
if alive?
pid = self.pid
Process.kill("INT", pid)
Process.wait(pid)
end
true
rescue Errno::ESRCH, Errno::ECHILD
true
end
def pid
Integer(PID_FILE.read)
rescue Errno::ENOENT
nil
end
def alive?
pid = self.pid
return false unless pid
pid && Process.kill(0, pid)
true
rescue Errno::ESRCH
false
end
end