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(context) ⇒ BackgroundRecorder

Returns a new instance of BackgroundRecorder.



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

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

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#queueObject (readonly)

Returns the value of attribute queue.



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

def queue
  @queue
end

#threadObject (readonly)

Returns the value of attribute thread.



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

def thread
  @thread
end

Instance Method Details

#loggerObject



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

def logger
  context.logger
end

#record!(request) ⇒ Object



31
32
33
34
# File 'lib/scout_apm/background_recorder.rb', line 31

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

#startObject



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

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

#stopObject



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

def stop
  @thread.kill
end

#thread_funcObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/scout_apm/background_recorder.rb', line 36

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