Class: Guard::Rpush

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

Constant Summary collapse

DEFAULT_SIGNAL =
:TERM

Instance Method Summary collapse

Instance Method Details

#capture_logging?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/guard/rpush.rb', line 128

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

#check_init_pidfile_directoryObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/guard/rpush.rb', line 68

def check_init_pidfile_directory
  pidfile_dir = File.dirname(pidfile_path)
  unless Dir.exist? pidfile_dir
    UI.info "Creating directory #{pidfile_dir} for pidfile"
    FileUtils.mkdir_p pidfile_dir
  end

  unless File.writable? pidfile_dir
    raise "No write access to pidfile directory #{pidfile_dir}"
  end
end

#cmdObject



95
96
97
98
99
# File 'lib/guard/rpush.rb', line 95

def cmd
  command = ['bundle exec rpush start'] 
  command << "-e #{@options[:environment]}"  if @options[:environment]  
  command.join(' ')
end

#logfileObject



101
102
103
104
105
# File 'lib/guard/rpush.rb', line 101

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

#pidObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/guard/rpush.rb', line 80

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



62
63
64
65
66
# File 'lib/guard/rpush.rb', line 62

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

#process_running?Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
126
# File 'lib/guard/rpush.rb', line 119

def process_running?
  begin
    Process.getpgid pid
    true
  rescue Errno::ESRCH
    false
  end
end

#reloadObject



32
33
34
35
36
37
# File 'lib/guard/rpush.rb', line 32

def reload
  UI.info "Reloading Rpush..."
  stop
  start
  UI.info "Rpush restarted successfully."
end

#reload_on_change?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/guard/rpush.rb', line 115

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

#run_allObject



39
40
41
# File 'lib/guard/rpush.rb', line 39

def run_all
  true
end

#run_on_change(paths) ⇒ Object



43
44
45
# File 'lib/guard/rpush.rb', line 43

def run_on_change(paths)
  reload if reload_on_change?
end

#shutdown_retriesObject



107
108
109
# File 'lib/guard/rpush.rb', line 107

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

#shutdown_rpushObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/guard/rpush.rb', line 47

def shutdown_rpush
  return UI.info "No instance of Rpush to stop." unless pid
  return UI.info "Rpush (#{pid}) was already stopped." unless process_running?
  UI.info "Sending TERM signal to Rpush (#{pid})..."
  Process.kill("TERM", pid)

  return if shutdown_retries == 0
  shutdown_retries.times do
    return UI.info "Rpush stopped." unless process_running?
    UI.info "Rpush is still shutting down. Retrying in #{ shutdown_wait } second(s)..."
    sleep shutdown_wait
  end
  UI.error "Rpush didn't shut down after #{ shutdown_retries * shutdown_wait } second(s)."
end

#shutdown_waitObject



111
112
113
# File 'lib/guard/rpush.rb', line 111

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

#startObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/guard/rpush.rb', line 7

def start
  @rpush_started = false
  begin
    check_init_pidfile_directory
    UI.info "Starting Rpush daemon.." 
    sid = spawn({},cmd)
    Process.wait(sid)
    UI.info "Rpush is running with PID #{pid}"
    @rpush_started = $?.success?
  rescue ::Exception => err
    UI.error "Unable to start Rpush. Errors: #{err}"
  end
  @rpush_started
end

#stopObject



22
23
24
25
26
27
28
29
30
# File 'lib/guard/rpush.rb', line 22

def stop
  if @rpush_started
    shutdown_rpush
    true
  else
    UI.info "Rpush was not started. Skipping stop process.."
    true
  end
end