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.



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

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
  @last_failure = Concurrent::AtomicReference.new
  @last_signature = Concurrent::AtomicReference.new
  @running = Concurrent::AtomicBoolean.new
  @schedule_mutex = Mutex.new
  @serializer_pool_key = :"julewire_core_meta_observer_serializers_#{object_id}"
end

Class Method Details

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



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/julewire/core/diagnostics/meta_observer.rb', line 14

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



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/julewire/core/diagnostics/meta_observer.rb', line 83

def health
  failure = @last_failure.get
  {
    event: @event,
    include_ok: @include_ok,
    interval: @interval,
    last_failure: failure,
    observed_runtime: @runtime_name,
    running: @running.true?,
    status: failure ? :degraded : :ok,
    target_runtime: @target_name
  }.compact.freeze
end

#sample!Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/julewire/core/diagnostics/meta_observer.rb', line 70

def sample!
  health = @runtime.health
  signature = signature_for(health)
  return false if signature.eql?(@last_signature.get_and_set(signature))
  return false unless emit_health?(health)

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

#start!Object



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

def start!
  return self unless @running.make_true

  schedule_next
  self
end

#stop!Object



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

def stop!
  token = @schedule_mutex.synchronize do
    @running.make_false
    token = @token
    @token = nil
    token
  end
  @scheduler.cancel(token) if token
  self
end