Class: Guard::Redis

Inherits:
Plugin
  • Object
show all
Defined in:
lib/guard/redis.rb

Instance Method Summary collapse

Instance Method Details

#capture_logging?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/guard/redis.rb', line 148

def capture_logging?
  options.fetch(:capture_logging) { false }
end

#configObject



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_directoryObject



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

#executableObject



109
110
111
# File 'lib/guard/redis.rb', line 109

def executable
  options.fetch(:executable) { 'redis-server' }
end

#last_operation_succeeded?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/guard/redis.rb', line 131

def last_operation_succeeded?
  $?.success?
end

#logfileObject



117
118
119
120
121
# File 'lib/guard/redis.rb', line 117

def logfile
  options.fetch(:logfile) {
    if capture_logging? then "log/redis_#{port}.log" else 'stdout' end
  }
end

#pidObject



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_pathObject



66
67
68
69
70
# File 'lib/guard/redis.rb', line 66

def pidfile_path
  options.fetch(:pidfile) {
    File.expand_path('/tmp/redis.pid', File.dirname(__FILE__))
  }
end

#portObject



113
114
115
# File 'lib/guard/redis.rb', line 113

def port
  options.fetch(:port) { 6379 }
end

#process_running?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


47
48
49
# File 'lib/guard/redis.rb', line 47

def redis_started?
  @started
end

#reloadObject



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

Returns:

  • (Boolean)


135
136
137
# File 'lib/guard/redis.rb', line 135

def reload_on_change?
  options.fetch(:reload_on_change) { false }
end

#run_allObject



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_redisObject



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_retriesObject



123
124
125
# File 'lib/guard/redis.rb', line 123

def shutdown_retries
  options.fetch(:shutdown_retries) { 0 }
end

#shutdown_waitObject



127
128
129
# File 'lib/guard/redis.rb', line 127

def shutdown_wait
  options.fetch(:shutdown_wait) { 0 }
end

#startObject



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

#stopObject



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