Class: WatchmonkeyCli::Application

Inherits:
Object
  • Object
show all
Includes:
Colorize, Configuration::AppHelper, Core, Dispatch, OutputHelper, Checker::AppHelper, Helper
Defined in:
lib/watchmonkey_cli/application.rb,
lib/watchmonkey_cli/application/core.rb,
lib/watchmonkey_cli/application/colorize.rb,
lib/watchmonkey_cli/application/dispatch.rb,
lib/watchmonkey_cli/application/configuration.rb,
lib/watchmonkey_cli/application/output_helper.rb

Defined Under Namespace

Modules: Colorize, Core, Dispatch, OutputHelper Classes: Configuration

Constant Summary

Constants included from Colorize

Colorize::COLORMAP

Constants included from Helper

Helper::BYTE_UNITS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Checker::AppHelper

#init_checkers!, #start_checkers!, #stop_checkers!

Methods included from Configuration::AppHelper

#checker_directory, #checker_files, #config_directory, #config_filename, #config_files, #generate_config, #load_appconfig, #load_checkers!, #load_configs!, #wm_cfg_configfile, #wm_cfg_path

Methods included from Dispatch

#dispatch, #dispatch_generate_config, #dispatch_help, #dispatch_index, #dispatch_info

Methods included from Core

#_fire_around, #_queueoff, #close_connections!, #enqueue, #enqueue_sub, #fetch_connection, #filtered_threads, #fire, #haltpoint, #hook, #logger, #logger_filename, #release_signals, #spawn_threads_and_run!, #trap_signals

Methods included from Colorize

#colorize

Methods included from OutputHelper

#abort, #debug, #error, #info, #print, #puts, #warn

Methods included from Helper

#human_filesize, #human_number, #human_seconds

Constructor Details

#initialize(env, argv) {|_self| ... } ⇒ Application

Returns a new instance of Application.

Yields:

  • (_self)

Yield Parameters:



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
74
75
# File 'lib/watchmonkey_cli/application.rb', line 44

def initialize env, argv
  @boot = Time.current
  @env, @argv = env, argv
  @connections = {}
  @hooks = {}
  @monitor = Monitor.new
  @threads = []
  @queue = Queue.new
  @tag_list = Set.new
  @processed = 0
  @running = false
  @opts = {
    dump: false,             # (internal) if true app will dump itself and exit before running any checks
    dispatch: :index,        # (internal) action to dispatch
    check_for_updates: true, # -z flag
    colorize: true,          # -m flag
    debug: false,            # -d flag
    threads: 10,             # -t flag
    maxrt: 120.seconds,      # max runtime of a single task after which it will be terminated (may break SSH connection), 0/false to not limit runtime
    conclosewait: 10,        # max seconds to wait for connections to be closed (may never if they got killed by maxrt)
    loop_forever: false,     # (internal) loop forever (app mode)
    loop_wait_empty: 1,      # (internal) time to wait in thread if queue is empty
    autotag: true,           # (internal) if true checkers will get auto tags for checker name and hostname/connection
    silent: false,           # -s flag
    quiet: false,            # -q flag
    stdout: STDOUT,          # (internal) STDOUT redirect
    tag_only: [],            # -o flag
    tag_except: [],          # -e flag
  }
  init_params
  yield(self)
end

Instance Attribute Details

#checkersObject (readonly)

Returns the value of attribute checkers.



3
4
5
# File 'lib/watchmonkey_cli/application.rb', line 3

def checkers
  @checkers
end

#connectionsObject (readonly)

Returns the value of attribute connections.



3
4
5
# File 'lib/watchmonkey_cli/application.rb', line 3

def connections
  @connections
end

#hooksObject (readonly)

Returns the value of attribute hooks.



3
4
5
# File 'lib/watchmonkey_cli/application.rb', line 3

def hooks
  @hooks
end

#optsObject (readonly)

Returns the value of attribute opts.



3
4
5
# File 'lib/watchmonkey_cli/application.rb', line 3

def opts
  @opts
end

#processedObject (readonly)

Returns the value of attribute processed.



3
4
5
# File 'lib/watchmonkey_cli/application.rb', line 3

def processed
  @processed
end

#queueObject (readonly)

Returns the value of attribute queue.



3
4
5
# File 'lib/watchmonkey_cli/application.rb', line 3

def queue
  @queue
end

#tag_listObject (readonly)

Returns the value of attribute tag_list.



3
4
5
# File 'lib/watchmonkey_cli/application.rb', line 3

def tag_list
  @tag_list
end

#threadsObject (readonly)

Returns the value of attribute threads.



3
4
5
# File 'lib/watchmonkey_cli/application.rb', line 3

def threads
  @threads
end

Class Method Details

.dispatch(*a) ⇒ Object

Setup =



15
16
17
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
# File 'lib/watchmonkey_cli/application.rb', line 15

def self.dispatch *a
  new(*a) do |app|
    app.load_appconfig
    app.parse_params
    begin
      app.dispatch
      app.haltpoint
    rescue Interrupt
      app.abort("Interrupted", 1)
    rescue SystemExit
      # silence
    ensure
      $wm_runtime_exiting = true
      app.fire(:wm_shutdown)
      if app.filtered_threads.length > 1
        app.error "[WARN] #{app.filtered_threads.length} threads remain (should be 1)..."
        app.filtered_threads.each do |thr|
          app.debug "[THR] #{Thread.main == thr ? "MAIN" : "THREAD"}\t#{thr.alive? ? "ALIVE" : "DEAD"}\t#{thr.inspect}", 10
          thr.backtrace.each do |l|
            app.debug "[THR]\t#{l}", 20
          end
        end
      else
        app.debug "1 thread remains..."
      end
    end
  end
end

Instance Method Details

#dump_and_exit!Object



121
122
123
124
125
126
# File 'lib/watchmonkey_cli/application.rb', line 121

def dump_and_exit!
  puts "   Queue: #{@queue.length}"
  puts " AppOpts: #{@opts}"
  puts "Checkers: #{@checkers.keys.join(",")}"
  exit 9
end

#init_paramsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/watchmonkey_cli/application.rb', line 77

def init_params
  @optparse = OptionParser.new do |opts|
    opts.banner = "Usage: watchmonkey [options]"

    opts.separator(c "# Application options", :blue)
    opts.on("--generate-config [myconfig]", "Generates a example config in ~/.watchmonkey") {|s| @opts[:dispatch] = :generate_config; @opts[:config_name] = s }
    opts.on("-l", "--log [file]", "Log to file, defaults to ~/.watchmonkey/logs/watchmonkey.log") {|s| @opts[:logfile] = s || logger_filename }
    opts.on("-t", "--threads [NUM]", Integer, "Amount of threads to be used for checking (default: 10)") {|s| @opts[:threads] = s }
    opts.on("-e", "--except tag1,tag2", Array, "Don't run tasks tagged with given tags") {|s| @opts[:tag_except] = s.map(&:to_sym) }
    opts.on("-o", "--only tag1,tag2", Array, "Only run tasks tagged with given tags") {|s| @opts[:tag_only] = s.map(&:to_sym) }
    opts.on("-s", "--silent", "Only print errors and infos") { @opts[:silent] = true }
    opts.on("-q", "--quiet", "Only print errors") { @opts[:quiet] = true }

    opts.separator("\n" << c("# General options", :blue))
    opts.on("-d", "--debug [lvl=1]", Integer, "Enable debug output") {|l| @opts[:debug] = l || 1 }
    opts.on("-m", "--monochrome", "Don't colorize output") { @opts[:colorize] = false }
    opts.on("-h", "--help", "Shows this help") { @opts[:dispatch] = :help }
    opts.on("-v", "--version", "Shows version and other info") { @opts[:dispatch] = :info }
    opts.on("-z", "Do not check for updates on GitHub (with -v/--version)") { @opts[:check_for_updates] = false }
    opts.on("--dump-core", "for developers") { @opts[:dump] = true }
  end
  fire(:optparse_init, @optparse)
end

#parse_paramsObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/watchmonkey_cli/application.rb', line 101

def parse_params
  fire(:optparse_parse_before, @optparse)
  fire(:optparse_parse_around, @optparse) do
    @optparse.parse!(@argv)
  end
  fire(:optparse_parse_after, @optparse)
rescue OptionParser::ParseError => e
  abort(e.message)
  dispatch(:help)
  exit 1
end

#running?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/watchmonkey_cli/application.rb', line 113

def running?
  @running
end

#sync(&block) ⇒ Object



117
118
119
# File 'lib/watchmonkey_cli/application.rb', line 117

def sync &block
  @monitor.synchronize(&block)
end