Class: ScoutApm::BackgroundRecorder

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/background_recorder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ BackgroundRecorder

Returns a new instance of BackgroundRecorder.



11
12
13
14
# File 'lib/scout_apm/background_recorder.rb', line 11

def initialize(logger)
  @logger = logger
  @queue = Queue.new
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/scout_apm/background_recorder.rb', line 9

def logger
  @logger
end

#queueObject (readonly)

Returns the value of attribute queue.



7
8
9
# File 'lib/scout_apm/background_recorder.rb', line 7

def queue
  @queue
end

#threadObject (readonly)

Returns the value of attribute thread.



8
9
10
# File 'lib/scout_apm/background_recorder.rb', line 8

def thread
  @thread
end

Instance Method Details

#record!(request) ⇒ Object



26
27
28
29
# File 'lib/scout_apm/background_recorder.rb', line 26

def record!(request)
  start unless @thread.alive?
  @queue.push(request)
end

#startObject



16
17
18
19
20
# File 'lib/scout_apm/background_recorder.rb', line 16

def start
  logger.info("Starting BackgroundRecorder")
  @thread = Thread.new(&method(:thread_func))
  self
end

#stopObject



22
23
24
# File 'lib/scout_apm/background_recorder.rb', line 22

def stop
  @thread.kill
end

#thread_funcObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/scout_apm/background_recorder.rb', line 31

def thread_func
  while req = queue.pop
    begin
      logger.debug("recording in thread. Queue size: #{queue.size}")
      # For now, just proxy right back into the TrackedRequest object's record function
      req.record!
    rescue => e
      logger.warn("Error in BackgroundRecorder - #{e.message} : #{e.backtrace}")
    end
  end
end