Class: Julewire::Core::Diagnostics::MetaObserver

Inherits:
Object
  • Object
show all
Defined in:
lib/julewire/core/diagnostics/meta_observer.rb

Constant Summary collapse

DEFAULT_EVENT =
"julewire.runtime_health"
DEFAULT_INTERVAL =
30

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime:, target_runtime:, runtime_name: :default, target_name: :meta, event: DEFAULT_EVENT, interval: DEFAULT_INTERVAL, include_ok: false, scheduler: Scheduling::SharedScheduler) ⇒ MetaObserver

Returns a new instance of MetaObserver.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/julewire/core/diagnostics/meta_observer.rb', line 24

def initialize(
  runtime:,
  target_runtime:,
  runtime_name: :default,
  target_name: :meta,
  event: DEFAULT_EVENT,
  interval: DEFAULT_INTERVAL,
  include_ok: false,
  scheduler: Scheduling::SharedScheduler
)
  @runtime = runtime
  @target_runtime = target_runtime
  @runtime_name = Core.normalize_name(runtime_name, name: :runtime_name)
  @target_name = Core.normalize_name(target_name, name: :target_name)
  @event = event.to_s
  @interval = Validation.validate_integer_limit!(interval, name: :interval, positive: true)
  @include_ok = include_ok ? true : false
  @scheduler = scheduler
  @mutex = Mutex.new
  @last_signature = nil
  @last_failure = nil
  @started = false
  @stopped = false
  @token = nil
  @serializer_pool_key = :"julewire_core_meta_observer_serializers_#{object_id}"
end

Class Method Details

.attach!(runtime_name = :default, target: :meta, start: true) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/julewire/core/diagnostics/meta_observer.rb', line 11

def attach!(runtime_name = :default, target: :meta, start: true, **)
  observer = new(
    runtime: Julewire.runtime(runtime_name),
    target_runtime: Julewire.runtime(target),
    runtime_name: runtime_name,
    target_name: target,
    **
  )
  observer.start! if start
  observer
end

Instance Method Details

#healthObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/julewire/core/diagnostics/meta_observer.rb', line 90

def health
  @mutex.synchronize do
    {
      event: @event,
      include_ok: @include_ok,
      interval: @interval,
      last_failure: @last_failure,
      observed_runtime: @runtime_name,
      running: @started && !@stopped,
      status: @last_failure ? :degraded : :ok,
      target_runtime: @target_name
    }.compact.freeze
  end
end

#sample!Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/julewire/core/diagnostics/meta_observer.rb', line 72

def sample!
  health = @runtime.health
  signature = signature_for(health)
  changed = @mutex.synchronize do
    changed = signature != @last_signature
    @last_signature = signature
    changed
  end
  return false unless changed
  return false unless emit_health?(health)

  emit_health(health)
  true
rescue StandardError => e
  record_failure(e)
  false
end

#start!Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/julewire/core/diagnostics/meta_observer.rb', line 51

def start!
  @mutex.synchronize do
    return self if @started && !@stopped

    @started = true
    @stopped = false
    schedule_next
  end
  self
end

#stop!Object



62
63
64
65
66
67
68
69
70
# File 'lib/julewire/core/diagnostics/meta_observer.rb', line 62

def stop!
  token = @mutex.synchronize do
    @stopped = true
    @started = false
    @token
  end
  @scheduler.cancel(token) if token
  self
end