Module: WatchmonkeyCli::Application::Core
- Included in:
- WatchmonkeyCli::Application
- Defined in:
- lib/watchmonkey_cli/application/core.rb
Instance Method Summary collapse
- #_queueoff ⇒ Object
- #close_connections! ⇒ Object
-
#enqueue(checker, *a, &block) ⇒ Object
= Queue tasks & methods = =========================.
- #enqueue_sub(checker, which, *args) ⇒ Object
-
#fetch_connection(type, id, opts = {}, &initializer) ⇒ Object
= Connection handling = =======================.
- #fire(which, *args) ⇒ Object
- #haltpoint ⇒ Object
-
#hook(*which, &hook_block) ⇒ Object
= Events = ==========.
- #logger ⇒ Object
-
#logger_filename ⇒ Object
= Logger = ==========.
- #release_signals ⇒ Object
- #spawn_threads_and_run! ⇒ Object
-
#trap_signals ⇒ Object
= Signal trapping = ===================.
Instance Method Details
#_queueoff ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/watchmonkey_cli/application/core.rb', line 143 def _queueoff while !@queue.empty? || @opts[:loop_forever] break if $wm_runtime_exiting item = queue.pop(true) rescue false if item Thread.current[:working] = true fire(:wm_work_start, Thread.current) sync { @processed += 1 } item[2].call(*item[1]) Thread.current[:working] = false fire(:wm_work_end, Thread.current) end sleep @opts[:loop_wait_empty] if @opts[:loop_forever] && @opts[:loop_wait_empty] && @queue.empty? end end |
#close_connections! ⇒ Object
82 83 84 85 86 |
# File 'lib/watchmonkey_cli/application/core.rb', line 82 def close_connections! @connections.each do |type, clist| clist.each{|id, con| con.close! } end end |
#enqueue(checker, *a, &block) ⇒ Object
Queue tasks & methods =
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/watchmonkey_cli/application/core.rb', line 92 def enqueue checker, *a, &block sync do cb = block || checker.method(:check!) evreg = @disable_event_registration fire(:enqueue, checker, a, cb) unless evreg @queue << [checker, a, ->(*a) { begin result = Checker::Result.new(checker, *a) checker.debug(result.str_running) checker.safe(result.str_safe) { cb.call(result, *a) } fire(:result_dump, result, a, checker) result.dump! ensure fire(:dequeue, checker, a) unless evreg end }] end end |
#enqueue_sub(checker, which, *args) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/watchmonkey_cli/application/core.rb', line 111 def enqueue_sub checker, which, *args sync do if sec = @checkers[which.to_s] begin # ef_was = @disable_event_firing er_was = @disable_event_registration # @disable_event_firing = true @disable_event_registration = true sec.enqueue(*args) ensure # @disable_event_firing = ef_was @disable_event_registration = er_was end end end end |
#fetch_connection(type, id, opts = {}, &initializer) ⇒ Object
Connection handling =
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/watchmonkey_cli/application/core.rb', line 67 def fetch_connection type, id, opts = {}, &initializer if !@connections[type] || !@connections[type][id] @connections[type] ||= {} case type when :loopback @connections[type][id] = LoopbackConnection.new(id, opts, &initializer) when :ssh @connections[type][id] = SshConnection.new(id, opts, &initializer) else raise NotImplementedError, "unknown connection type `#{type}'!" end end @connections[type][id] end |
#fire(which, *args) ⇒ Object
40 41 42 43 44 |
# File 'lib/watchmonkey_cli/application/core.rb', line 40 def fire which, *args return if @disable_event_firing sync { debug "[Event] Firing #{which} (#{@hooks[which].try(:length) || 0} handlers) #{args.map(&:class)}", 99 } @hooks[which] && @hooks[which].each{|h| h.call(*args) } end |
#haltpoint ⇒ Object
25 26 27 |
# File 'lib/watchmonkey_cli/application/core.rb', line 25 def haltpoint raise Interrupt if $wm_runtime_exiting end |
#hook(*which, &hook_block) ⇒ Object
Events =
33 34 35 36 37 38 |
# File 'lib/watchmonkey_cli/application/core.rb', line 33 def hook *which, &hook_block which.each do |w| @hooks[w.to_sym] ||= [] @hooks[w.to_sym] << hook_block end end |
#logger ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/watchmonkey_cli/application/core.rb', line 54 def logger sync do @logger ||= begin FileUtils.mkdir_p(File.dirname(@opts[:logfile])) Logger.new(@opts[:logfile], 10, 1024000) end end end |
#logger_filename ⇒ Object
Logger =
50 51 52 |
# File 'lib/watchmonkey_cli/application/core.rb', line 50 def logger_filename "#{wm_cfg_path}/logs/watchmonkey.log" end |
#release_signals ⇒ Object
19 20 21 22 23 |
# File 'lib/watchmonkey_cli/application/core.rb', line 19 def release_signals debug "Releasing INT signal..." Signal.trap("INT", "DEFAULT") Signal.trap("TERM", "DEFAULT") end |
#spawn_threads_and_run! ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/watchmonkey_cli/application/core.rb', line 128 def spawn_threads_and_run! if @opts[:threads] > 1 debug "Spawning #{@opts[:threads]} consumer threads..." @opts[:threads].times do @threads << Thread.new do Thread.current.abort_on_exception = true _queueoff end end else debug "Running threadless..." _queueoff end end |
#trap_signals ⇒ Object
Signal trapping =
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/watchmonkey_cli/application/core.rb', line 7 def trap_signals debug "Trapping INT signal..." Signal.trap("INT") do $wm_runtime_exiting = true Kernel.puts "Interrupting..." end Signal.trap("TERM") do $wm_runtime_exiting = true Kernel.puts "Terminating..." end end |