Class: TemporaryRedis
- Inherits:
-
Object
- Object
- TemporaryRedis
- Defined in:
- lib/temporary_redis.rb
Constant Summary collapse
- REDIS_TEMP_DIR =
"/tmp/discourse_temp_redis"
- REDIS_LOG_PATH =
"#{REDIS_TEMP_DIR}/redis.log"
- REDIS_PID_PATH =
"#{REDIS_TEMP_DIR}/redis.pid"
Instance Attribute Summary collapse
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
Instance Method Summary collapse
-
#initialize ⇒ TemporaryRedis
constructor
A new instance of TemporaryRedis.
- #port ⇒ Object
- #remove ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ TemporaryRedis
Returns a new instance of TemporaryRedis.
10 11 12 |
# File 'lib/temporary_redis.rb', line 10 def initialize set_redis_server_bin end |
Instance Attribute Details
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
8 9 10 |
# File 'lib/temporary_redis.rb', line 8 def instance @instance end |
Instance Method Details
#port ⇒ Object
14 15 16 |
# File 'lib/temporary_redis.rb', line 14 def port @port ||= find_free_port(11_000..11_900) end |
#remove ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/temporary_redis.rb', line 75 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 |
#start ⇒ Object
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 |
# File 'lib/temporary_redis.rb', line 18 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 |