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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Monitor.



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

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

  @client = client
  @max_size = options[:max_size] || DEFAULT_MAX_SIZE
  @timeout = options[:reporting_milliseconds] / 1000
  @log_verbosity = options[:log_verbosity] ||= 'incident'
  @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



35
36
37
38
39
40
41
42
43
# File 'lib/prevoty/monitor.rb', line 35

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