Class: Guard::Redis
- Inherits:
-
Plugin
- Object
- Plugin
- Guard::Redis
- Defined in:
- lib/guard/redis.rb
Instance Method Summary collapse
- #capture_logging? ⇒ Boolean
- #config ⇒ Object
- #ensure_pidfile_directory ⇒ Object
- #executable ⇒ Object
- #last_operation_succeeded? ⇒ Boolean
- #logfile ⇒ Object
- #pid ⇒ Object
- #pidfile_path ⇒ Object
- #port ⇒ Object
- #process_running? ⇒ Boolean
- #redis_started? ⇒ Boolean
- #reload ⇒ Object
- #reload_on_change? ⇒ Boolean
- #run_all ⇒ Object
- #run_on_change(paths) ⇒ Object
- #shutdown_redis ⇒ Object
- #shutdown_retries ⇒ Object
- #shutdown_wait ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Instance Method Details
#capture_logging? ⇒ Boolean
148 149 150 |
# File 'lib/guard/redis.rb', line 148 def capture_logging? .fetch(:capture_logging) { false } end |
#config ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/guard/redis.rb', line 84 def config result = <<END daemonize yes pidfile #{pidfile_path} port #{port} END result << "logfile #{logfile}" if capture_logging? result end |
#ensure_pidfile_directory ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/guard/redis.rb', line 72 def ensure_pidfile_directory pidfile_dir = File.dirname(pidfile_path) unless Dir.exist? pidfile_dir UI.info "Creating pidfie directory #{pidfile_dir}" FileUtils.mkdir_p pidfile_dir end unless File.writable? pidfile_dir raise "no write access to pidfile directory #{pidfile_dir}" end end |
#executable ⇒ Object
109 110 111 |
# File 'lib/guard/redis.rb', line 109 def executable .fetch(:executable) { 'redis-server' } end |
#last_operation_succeeded? ⇒ Boolean
131 132 133 |
# File 'lib/guard/redis.rb', line 131 def last_operation_succeeded? $?.success? end |
#logfile ⇒ Object
117 118 119 120 121 |
# File 'lib/guard/redis.rb', line 117 def logfile .fetch(:logfile) { if capture_logging? then "log/redis_#{port}.log" else 'stdout' end } end |
#pid ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/guard/redis.rb', line 94 def pid count = 0 loop do if count > 5 raise "pidfile was never written to #{pidfile_path}" end break if File.exists? pidfile_path UI.info "Waiting for pidfile to appear at #{pidfile_path}..." count += 1 sleep 1 end File.read(pidfile_path).to_i end |
#pidfile_path ⇒ Object
66 67 68 69 70 |
# File 'lib/guard/redis.rb', line 66 def pidfile_path .fetch(:pidfile) { File.('/tmp/redis.pid', File.dirname(__FILE__)) } end |
#port ⇒ Object
113 114 115 |
# File 'lib/guard/redis.rb', line 113 def port .fetch(:port) { 6379 } end |
#process_running? ⇒ Boolean
139 140 141 142 143 144 145 146 |
# File 'lib/guard/redis.rb', line 139 def process_running? begin Process.getpgid pid true rescue Errno::ESRCH false end end |
#redis_started? ⇒ Boolean
47 48 49 |
# File 'lib/guard/redis.rb', line 47 def redis_started? @started end |
#reload ⇒ Object
32 33 34 35 36 37 |
# File 'lib/guard/redis.rb', line 32 def reload UI.info "Reloading Redis..." stop start UI.info "Redis successfully restarted." end |
#reload_on_change? ⇒ Boolean
135 136 137 |
# File 'lib/guard/redis.rb', line 135 def reload_on_change? .fetch(:reload_on_change) { false } end |
#run_all ⇒ Object
39 40 41 |
# File 'lib/guard/redis.rb', line 39 def run_all true end |
#run_on_change(paths) ⇒ Object
43 44 45 |
# File 'lib/guard/redis.rb', line 43 def run_on_change(paths) reload if reload_on_change? end |
#shutdown_redis ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/guard/redis.rb', line 51 def shutdown_redis return UI.info "No instance of Redis to stop." unless pid return UI.info "Redis (#{pid}) was already stopped." unless process_running? UI.info "Sending TERM signal to Redis (#{pid})..." Process.kill("TERM", pid) return if shutdown_retries == 0 shutdown_retries.times do return UI.info "Redis stopped." unless process_running? UI.info "Redis is still shutting down. Retrying in #{ shutdown_wait } second(s)..." sleep shutdown_wait end UI.error "Redis didn't shut down after #{ shutdown_retries * shutdown_wait } second(s)." end |
#shutdown_retries ⇒ Object
123 124 125 |
# File 'lib/guard/redis.rb', line 123 def shutdown_retries .fetch(:shutdown_retries) { 0 } end |
#shutdown_wait ⇒ Object
127 128 129 |
# File 'lib/guard/redis.rb', line 127 def shutdown_wait .fetch(:shutdown_wait) { 0 } end |
#start ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/guard/redis.rb', line 5 def start @started = false begin ensure_pidfile_directory UI.info "Starting Redis on port #{port}..." IO.popen("#{executable} -", 'w+') do |server| server.write(config) server.close_write end UI.info "Redis is running with PID #{pid}" @started = last_operation_succeeded? rescue ::Exception => err UI.error "Redis not started due to errors: #{err}" end @started end |
#stop ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/guard/redis.rb', line 22 def stop if redis_started? shutdown_redis true else UI.info "Redis never started. No need to shutdown." true end end |