Class: WatchmonkeyCli::Checker

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/watchmonkey_cli/checker.rb

Defined Under Namespace

Modules: AppHelper Classes: Result

Constant Summary

Constants included from Helper

Helper::BYTE_UNITS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

#human_filesize, #human_number, #human_seconds

Constructor Details

#initialize(app) ⇒ Checker

Returns a new instance of Checker.



128
129
130
131
# File 'lib/watchmonkey_cli/checker.rb', line 128

def initialize app
  @app = app
  send(:init) if respond_to?(:init)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



126
127
128
# File 'lib/watchmonkey_cli/checker.rb', line 126

def app
  @app
end

Class Method Details

.checker_nameObject



12
13
14
# File 'lib/watchmonkey_cli/checker.rb', line 12

def self.checker_name
  @checker_name || self.name
end

.checker_name=(name) ⇒ Object



16
17
18
# File 'lib/watchmonkey_cli/checker.rb', line 16

def self.checker_name= name
  @checker_name = name
end

.descendantsObject

Descendant tracking for inherited classes.



4
5
6
# File 'lib/watchmonkey_cli/checker.rb', line 4

def self.descendants
  @descendants ||= []
end

.inherited(descendant) ⇒ Object



8
9
10
# File 'lib/watchmonkey_cli/checker.rb', line 8

def self.inherited(descendant)
  descendants << descendant
end

.maxrtObject



20
21
22
# File 'lib/watchmonkey_cli/checker.rb', line 20

def self.maxrt
  @maxrt
end

.maxrt=(seconds) ⇒ Object



24
25
26
# File 'lib/watchmonkey_cli/checker.rb', line 24

def self.maxrt= seconds
  @maxrt = seconds
end

Instance Method Details

#_tolog(msg, meth = :log) ⇒ Object



155
156
157
158
# File 'lib/watchmonkey_cli/checker.rb', line 155

def _tolog msg, meth = :log
  return unless app.opts[:logfile]
  app.logger.public_send(meth, msg)
end

#blank_config(tags = []) ⇒ Object



166
167
168
# File 'lib/watchmonkey_cli/checker.rb', line 166

def blank_config tags = []
  Application::Configuration.new(app, nil, tags)
end

#check!(*a) ⇒ Object

Raises:

  • (NotImplementedError)


252
253
254
255
# File 'lib/watchmonkey_cli/checker.rb', line 252

def check! *a
  # required, see #enqueue
  raise NotImplementedError, "a checker (#{self.class.name}) must implement `#check!' method!"
end

#debug(msg, robj = nil) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/watchmonkey_cli/checker.rb', line 140

def debug msg, robj = nil
  app.fire(:on_debug, msg, robj)
  app.fire(:on_message, msg, robj)
  return if app.opts[:quiet] || app.opts[:silent]
  _tolog(msg, :debug)
  app.puts app.c(msg, :black)
end

#enqueue(*args) ⇒ Object

Raises:

  • (NotImplementedError)


245
246
247
248
249
250
# File 'lib/watchmonkey_cli/checker.rb', line 245

def enqueue *args
  # Called by configuration defining a check with all the arguments.
  #   e.g. www_availability :my_host, foo: "bar" => args = [:my_host, {foo: "bar"}]
  # Should invoke `app.enqueue` which will by default call `#check!` method with given arguments.
  raise NotImplementedError, "a checker (#{self.class.name}) must implement `#enqueue' method!"
end

#error(msg, robj = nil) ⇒ Object



148
149
150
151
152
153
# File 'lib/watchmonkey_cli/checker.rb', line 148

def error msg, robj = nil
  app.fire(:on_error, msg, robj)
  app.fire(:on_message, msg, robj)
  _tolog(msg, :error)
  app.sync { app.error(msg) }
end

#info(msg, robj = nil) ⇒ Object



133
134
135
136
137
138
# File 'lib/watchmonkey_cli/checker.rb', line 133

def info msg, robj = nil
  app.fire(:on_info, msg, robj)
  return if app.opts[:quiet]
  _tolog(msg, :info)
  app.puts app.c(msg, :blue)
end

#initObject

API =



227
228
229
# File 'lib/watchmonkey_cli/checker.rb', line 227

def init
  # hook method (called when checker is being initialized)
end

#localObject

def to_s

string = "#<#{self.class.name}:#{self.object_id} "
fields = self.class.inspector_fields.map{|field| "#{field}: #{self.send(field)}"}
string << fields.join(", ") << ">"

end



176
177
178
# File 'lib/watchmonkey_cli/checker.rb', line 176

def local
  @app.fetch_connection(:loopback, :local)
end

#rsafe(resultobj, &block) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/watchmonkey_cli/checker.rb', line 200

def rsafe resultobj, &block
  tries = 0
  begin
    tries += 1
    block.call
  rescue StandardError => e
    unless tries > 3
      resultobj.sync do
        resultobj.error! "retry #{tries} reason is `#{e.class}: #{e.message}'"
        e.backtrace.each{|l| resultobj.debug "\t\t#{l}" }
        resultobj.dump!
      end
      unless $wm_runtime_exiting
        sleep 1
        retry
      end
    end
    resultobj.error! "retries exceeded"
    resultobj.dump!
  end
end

#safe(descriptor = nil, &block) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/watchmonkey_cli/checker.rb', line 180

def safe descriptor = nil, &block
  tries = 0
  begin
    tries += 1
    block.call
  rescue StandardError => e
    unless tries > 3
      app.sync do
        error "#{descriptor}retry #{tries} reason is `#{e.class}: #{e.message}'"
        e.backtrace.each{|l| debug "\t\t#{l}" }
      end
      unless $wm_runtime_exiting
        sleep 1
        retry
      end
    end
    error "#{descriptor}retries exceeded"
  end
end

#spawn_sub(which, *args) ⇒ Object



160
161
162
163
164
# File 'lib/watchmonkey_cli/checker.rb', line 160

def spawn_sub which, *args
  if sec = app.checkers[which.to_s]
    sec.enqueue(*args)
  end
end

#startObject



231
232
233
234
# File 'lib/watchmonkey_cli/checker.rb', line 231

def start
  # hook method (called after all checkers were initialized and configs + hosts are loaded)
  # can/should be used for starting connections, etc.
end

#stopObject



236
237
238
239
240
241
242
243
# File 'lib/watchmonkey_cli/checker.rb', line 236

def stop
  # hook method (called on application shutdown)
  # connections should be closed here

  # DO NOT CLOSE CONNECTIONS HANDLED BY THE APP!
  # Keep in mind that the checkers run concurrently
  # and therefore shared resources might still be in use
end