forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemporary_redis.rb
111 lines (99 loc) · 2.42 KB
/
temporary_redis.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
# frozen_string_literal: true
class TemporaryRedis
REDIS_TEMP_DIR = "/tmp/discourse_temp_redis"
REDIS_LOG_PATH = "#{REDIS_TEMP_DIR}/redis.log"
REDIS_PID_PATH = "#{REDIS_TEMP_DIR}/redis.pid"
attr_reader :instance
def initialize
set_redis_server_bin
end
def port
@port ||= find_free_port(11_000..11_900)
end
def start
return if @started
FileUtils.rm_rf(REDIS_TEMP_DIR)
Dir.mkdir(REDIS_TEMP_DIR)
FileUtils.touch(REDIS_LOG_PATH)
puts "Starting redis on port: #{port}"
@thread =
Thread.new do
system(
@redis_server_bin,
"--port",
port.to_s,
"--pidfile",
REDIS_PID_PATH,
"--logfile",
REDIS_LOG_PATH,
"--databases",
"1",
"--save",
'""',
"--appendonly",
"no",
"--daemonize",
"no",
"--maxclients",
"100",
"--dir",
REDIS_TEMP_DIR,
)
end
puts "Waiting for redis server to start..."
success = false
instance = nil
config = { port: port, host: "127.0.0.1", db: 0 }
start = Time.now
while !success
begin
instance = DiscourseRedis.new(config, namespace: true)
success = instance.ping == "PONG"
rescue Redis::CannotConnectError
ensure
if !success && (Time.now - start) >= 5
STDERR.puts "ERROR: Could not connect to redis in 5 seconds."
self.remove
exit(1)
elsif !success
sleep 0.1
end
end
end
puts "Redis is ready"
@instance = instance
@started = true
end
def remove
if @instance
@instance.shutdown
@thread.join
puts "Redis has been shutdown."
end
FileUtils.rm_rf(REDIS_TEMP_DIR)
@started = false
puts "Redis files have been cleaned up."
end
private
def set_redis_server_bin
path = `which redis-server 2> /dev/null`.strip
if path.size < 1
STDERR.puts "ERROR: redis-server is not installed on this machine. Please install it"
exit(1)
end
@redis_server_bin = path
rescue => ex
STDERR.puts "ERROR: Failed to find redis-server binary:"
STDERR.puts ex.inspect
exit(1)
end
def find_free_port(range)
range.each { |port| return port if port_available?(port) }
end
def port_available?(port)
TCPServer.open(port).close
true
rescue Errno::EADDRINUSE
false
end
end