Class: Coach::Notifications

Inherits:
Object
  • Object
show all
Defined in:
lib/coach/notifications.rb

Overview

By default, Coach will trigger ActiveSupport::Notifications at specific times in a request lifecycle.

Notifications is used to coordinate the listening and aggregation of these middleware notifications, while RequestEvent processes the published data.

Once a request has completed, Notifications will emit a ‘coach.request’ with aggregated request data.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



24
25
26
# File 'lib/coach/notifications.rb', line 24

def self.instance
  @instance ||= new
end

.subscribe!Object

Begin processing/emitting ‘coach.request’s



15
16
17
# File 'lib/coach/notifications.rb', line 15

def self.subscribe!
  instance.subscribe!
end

.unsubscribe!Object

Cease to emit ‘coach.request’s



20
21
22
# File 'lib/coach/notifications.rb', line 20

def self.unsubscribe!
  instance.unsubscribe!
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/coach/notifications.rb', line 52

def active?
  @subscriptions.any?
end

#subscribe!Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/coach/notifications.rb', line 28

def subscribe!
  return if active?

  @subscriptions << subscribe('handler.start') do |_, event|
    @benchmarks[event[:request].uuid] = RequestBenchmark.new(event[:middleware])
  end

  @subscriptions << subscribe('middleware.finish') do |name, start, finish, _, event|
    log_middleware_finish(event, start, finish)
  end

  @subscriptions << subscribe('handler.finish') do |name, start, finish, _, event|
    log_handler_finish(event, start, finish)
  end
end

#unsubscribe!Object



44
45
46
47
48
49
50
# File 'lib/coach/notifications.rb', line 44

def unsubscribe!
  return unless active?
  while @subscriptions.any?
    ActiveSupport::Notifications.unsubscribe(@subscriptions.pop)
  end
  true
end