Module: WatchmonkeyCli::Application::Core

Included in:
WatchmonkeyCli::Application
Defined in:
lib/watchmonkey_cli/application/core.rb

Instance Method Summary collapse

Instance Method Details

#_queueoffObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/watchmonkey_cli/application/core.rb', line 138

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



77
78
79
80
81
# File 'lib/watchmonkey_cli/application/core.rb', line 77

def close_connections!
  @connections.each do |type, clist|
    clist.each{|id, con| con.close! }
  end
end

#enqueue(checker, *a, &block) ⇒ Object

Queue tasks & methods =



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/watchmonkey_cli/application/core.rb', line 87

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/watchmonkey_cli/application/core.rb', line 106

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 =



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/watchmonkey_cli/application/core.rb', line 62

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



35
36
37
38
39
# File 'lib/watchmonkey_cli/application/core.rb', line 35

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

#haltpointObject

Raises:



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

def haltpoint
  raise Interrupt if $wm_runtime_exiting
end

#hook(*which, &hook_block) ⇒ Object

Events =



28
29
30
31
32
33
# File 'lib/watchmonkey_cli/application/core.rb', line 28

def hook *which, &hook_block
  which.each do |w|
    @hooks[w.to_sym] ||= []
    @hooks[w.to_sym] << hook_block
  end
end

#loggerObject



49
50
51
52
53
54
55
56
# File 'lib/watchmonkey_cli/application/core.rb', line 49

def logger
  sync do
    @logger ||= begin
      FileUtils.mkdir_p(File.dirname(@opts[:logfile]))
      Logger.new(@opts[:logfile], 10, 1024000)
    end
  end
end

#logger_filenameObject

Logger =



45
46
47
# File 'lib/watchmonkey_cli/application/core.rb', line 45

def logger_filename
  "#{wm_cfg_path}/logs/watchmonkey.log"
end

#release_signalsObject



15
16
17
18
# File 'lib/watchmonkey_cli/application/core.rb', line 15

def release_signals
  debug "Releasing INT signal..."
  Signal.trap("INT", "DEFAULT")
end

#spawn_threads_and_run!Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/watchmonkey_cli/application/core.rb', line 123

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_signalsObject

Signal trapping =



7
8
9
10
11
12
13
# 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
end