Class: Prevoty::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/prevoty/monitor.rb

Direct Known Subclasses

ContentMonitor, QueryMonitor

Constant Summary collapse

DEFAULT_TIMEOUT =
10000
DEFAULT_MAX_SIZE =
50
DEFAULT_LOG_DESTINATION =
'log'

Instance Method Summary collapse

Constructor Details

#initialize(client, options = {}) ⇒ Monitor

Returns a new instance of Monitor.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/prevoty/monitor.rb', line 10

def initialize(client, options = {})
  options[:reporting_milliseconds] ||= DEFAULT_TIMEOUT

  @client = client
  @before_callback = options[:before_callback]
  @after_callback = options[:after_callback]
  @max_size = options[:max_size] || DEFAULT_MAX_SIZE
  @log_destination = options[:log_destination] || DEFAULT_LOG_DESTINATION
  @timeout = options[:reporting_milliseconds] / 1000
  @mutex = Mutex.new
  @queue = []
  @rd, @wr = IO.pipe
  @thread = Thread.new do
    loop do
      begin
        Timeout::timeout(@timeout) { @rd.read }
        redo
      rescue Timeout::Error
        @mutex.lock()
        if @queue.size > 0
          process_queue
        end
        @mutex.unlock()
      end
    end
  end
end

Instance Method Details

#process(data) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/prevoty/monitor.rb', line 38

def process(data)
  @mutex.lock
  @queue.push(data)
  if @queue.size >= @max_size
    process_queue
    @wr.write 1
  end
  @mutex.unlock
end