Module: WatchmonkeyCli::Application::Core
- Included in:
- WatchmonkeyCli::Application
- Defined in:
- lib/watchmonkey_cli/application/core.rb
Instance Method Summary collapse
- #_fire_around(hooks, args, index = 0, &block) ⇒ Object
- #_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 = =======================.
- #filtered_threads ⇒ Object
- #fire(which, *args, &block) ⇒ 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
#_fire_around(hooks, args, index = 0, &block) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/watchmonkey_cli/application/core.rb', line 64 def _fire_around hooks, args, index = 0, &block return block.call unless hook = hooks[index] skip = catch(:skip) { hook.call(*args) { _fire_around(hooks, args, index + 1, &block) } nil } _fire_around(hooks, args, index + 1, &block) if skip end |
#_queueoff ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/watchmonkey_cli/application/core.rb', line 193 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
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/watchmonkey_cli/application/core.rb', line 109 def close_connections! @connections.each do |type, clist| clist.each do |id, con| if con.established? debug "[SHUTDOWN] closing #{type} connection #{id} #{con}" con.close! end end end end |
#enqueue(checker, *a, &block) ⇒ Object
Queue tasks & methods =
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/watchmonkey_cli/application/core.rb', line 124 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) # assign tags taskopts = a. result. = taskopts[:tags] || [] a << taskopts checker.debug(result.str_running) checker.rsafe(result) { timeout = checker.class.maxrt.nil? ? @opts[:maxrt] : checker.class.maxrt timeout = timeout.call(self, checker, a) if timeout.respond_to?(:call) begin if timeout && timeout > 0 Timeout::timeout(timeout) { cb.call(result, *a) } else cb.call(result, *a) end rescue Timeout::Error => ex result.error! "TIMEOUT: did not finish within #{timeout} seconds, task killed!" end } fire(:result_dump, result, a, checker) result.dump! ensure fire(:dequeue, checker, a) unless evreg end }] end end |
#enqueue_sub(checker, which, *args) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/watchmonkey_cli/application/core.rb', line 161 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 =
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/watchmonkey_cli/application/core.rb', line 94 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 |
#filtered_threads ⇒ Object
4 5 6 7 8 |
# File 'lib/watchmonkey_cli/application/core.rb', line 4 def filtered_threads Thread.list.reject do |thr| thr.backtrace[0]["gems/concurrent-ruby"] rescue false end end |
#fire(which, *args, &block) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/watchmonkey_cli/application/core.rb', line 46 def fire which, *args, &block return if @disable_event_firing sync { debug "[Event] Firing #{which} (#{@hooks[which].try(:length) || 0} handlers) #{args.map(&:class)}#{" HAS_BLOCK" if block}", 99 } if block && (!@hooks[which] || @hooks[which].empty?) block.call else if @hooks[which] && @hooks[which].any? if block catch :abort do _fire_around(@hooks[which], args, 0, &block) end else @hooks[which].all?{|h| h.call(*args) } end end end end |
#haltpoint ⇒ Object
31 32 33 |
# File 'lib/watchmonkey_cli/application/core.rb', line 31 def haltpoint raise Interrupt if $wm_runtime_exiting end |
#hook(*which, &hook_block) ⇒ Object
Events =
39 40 41 42 43 44 |
# File 'lib/watchmonkey_cli/application/core.rb', line 39 def hook *which, &hook_block which.each do |w| @hooks[w.to_sym] ||= [] @hooks[w.to_sym] << hook_block end end |
#logger ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/watchmonkey_cli/application/core.rb', line 81 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 =
77 78 79 |
# File 'lib/watchmonkey_cli/application/core.rb', line 77 def logger_filename "#{wm_cfg_path}/logs/watchmonkey.log" end |
#release_signals ⇒ Object
25 26 27 28 29 |
# File 'lib/watchmonkey_cli/application/core.rb', line 25 def release_signals debug "Releasing INT signal..." Signal.trap("INT", "DEFAULT") Signal.trap("TERM", "DEFAULT") end |
#spawn_threads_and_run! ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/watchmonkey_cli/application/core.rb', line 178 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 =
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/watchmonkey_cli/application/core.rb', line 13 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 |