Class: Dexter::Processor

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/dexter/processor.rb

Constant Summary

Constants included from Logging

Logging::COLOR_CODES

Instance Method Summary collapse

Methods included from Logging

#colorize, #log, #output

Constructor Details

#initialize(logfile, options) ⇒ Processor

Returns a new instance of Processor.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dexter/processor.rb', line 5

def initialize(logfile, options)
  @logfile = logfile

  @collector = Collector.new(min_time: options[:min_time], min_calls: options[:min_calls])
  @indexer = Indexer.new(options)

  @log_parser =
    if @logfile == :pg_stat_activity
      PgStatActivityParser.new(@indexer, @collector)
    elsif options[:input_format] == "csv"
      CsvLogParser.new(logfile, @collector)
    elsif options[:input_format] == "json"
      JsonLogParser.new(logfile, @collector)
    elsif options[:input_format] == "sql"
      SqlLogParser.new(logfile, @collector)
    else
      StderrLogParser.new(logfile, @collector)
    end

  @starting_interval = 3
  @interval = options[:interval]

  @mutex = Mutex.new
  @last_checked_at = {}

  log "Started"
end

Instance Method Details

#performObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dexter/processor.rb', line 33

def perform
  if [STDIN].include?(@logfile)
    Thread.abort_on_exception = true
    Thread.new do
      sleep(@starting_interval)
      loop do
        begin
          process_queries
        rescue PG::ServerError => e
          log colorize("ERROR: #{e.class.name}: #{e.message}", :red)
        end
        sleep(@interval)
      end
    end
  end

  begin
    @log_parser.perform
  rescue Errno::ENOENT => e
    raise Dexter::Abort, "ERROR: #{e.message}"
  end

  process_queries
end