Class: Errplane::Worker

Inherits:
Object
  • Object
show all
Extended by:
Logger
Defined in:
lib/errplane/worker.rb

Constant Summary collapse

MAX_POST_LINES =
200
MAX_TIME_SERIES_NAME_LENGTH =
255

Constants included from Logger

Logger::PREFIX

Class Method Summary collapse

Class Method Details

.check_background_queue(thread_num = 0) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/errplane/worker.rb', line 60

def check_background_queue(thread_num = 0)
  log :debug, "Checking background queue on thread #{thread_num} (#{current_threads.count} active)"

  begin
    data = []

    while data.size < MAX_POST_LINES && !Errplane.queue.empty?
      n = Errplane.queue.pop(true) rescue next;
      log :debug, "Found data in the queue! (#{n[:name]})"

      begin
        if n[:name].split("|").any?{|x| x.length > MAX_TIME_SERIES_NAME_LENGTH}
          log :error, "Time series name too long! Discarding data for: #{n[:name]}"
        else
          data << Errplane.process_line(n)
        end
      rescue => e
        log :info, "Instrumentation Error! #{e.inspect}"
      end
    end

    post_data(data.join("\n")) unless data.empty?
  end while Errplane.queue.length > MAX_POST_LINES
end

.current_thread_countObject



36
37
38
# File 'lib/errplane/worker.rb', line 36

def current_thread_count()
  Thread.list.count {|t| t[:errplane]}
end

.current_threadsObject



32
33
34
# File 'lib/errplane/worker.rb', line 32

def current_threads()
  Thread.list.select {|t| t[:errplane]}
end

.indent_lines(lines, num) ⇒ Object



14
15
16
# File 'lib/errplane/worker.rb', line 14

def indent_lines(lines, num)
  lines.split("\n").map {|line| (" " * num) + line[0..64]}.join("\n")
end

.post_data(data) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/errplane/worker.rb', line 18

def post_data(data)
  if Errplane.configuration.ignore_current_environment?
    log :debug, "Current environment is ignored, skipping POST."
    return false
  else
    log :debug, "POSTing data:\n#{indent_lines(data, 13)}"
    begin
      Errplane.api.post(data)
    rescue => e
      log :error, "Error calling API: #{e.inspect}"
    end
  end
end

.spawn_threadsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/errplane/worker.rb', line 40

def spawn_threads()
  Errplane.configuration.queue_worker_threads.times do |thread_num|
    log :debug, "Spawning background worker thread #{thread_num}."

    Thread.new do
      Thread.current[:errplane] = true

      at_exit do
        log :debug, "Thread exiting, flushing queue."
        check_background_queue(thread_num) until Errplane.queue.empty?
      end

      while true
        sleep Errplane.configuration.queue_worker_polling_interval
        check_background_queue(thread_num)
      end
    end
  end
end