Class: SourceMonitor::Health::SourceHealthMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/source_monitor/health/source_health_monitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, config: SourceMonitor.config.health, now: Time.current) ⇒ SourceHealthMonitor

Returns a new instance of SourceHealthMonitor.



10
11
12
13
14
# File 'lib/source_monitor/health/source_health_monitor.rb', line 10

def initialize(source:, config: SourceMonitor.config.health, now: Time.current)
  @source = source
  @config = config
  @now = now
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/source_monitor/health/source_health_monitor.rb', line 8

def config
  @config
end

#nowObject (readonly)

Returns the value of attribute now.



8
9
10
# File 'lib/source_monitor/health/source_health_monitor.rb', line 8

def now
  @now
end

#sourceObject (readonly)

Returns the value of attribute source.



8
9
10
# File 'lib/source_monitor/health/source_health_monitor.rb', line 8

def source
  @source
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
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
50
51
52
53
54
55
56
57
58
# File 'lib/source_monitor/health/source_health_monitor.rb', line 16

def call
  reload_source

  logs = recent_logs.to_a
  return if logs.empty?

  rate = calculate_success_rate(logs)
  attrs = { rolling_success_rate: rate }
  sample_size = logs.size
  thresholds_active = thresholds_applicable?(sample_size)

  auto_paused_until = current_auto_paused_until
  auto_paused_at = current_auto_paused_at

  if thresholds_active && should_resume?(auto_paused_until, rate)
    auto_paused_until = nil
    auto_paused_at = nil
    attrs[:auto_paused_until] = nil
    attrs[:auto_paused_at] = nil
    attrs[:consecutive_fetch_failures] = 0
    attrs[:backoff_until] = nil if source.backoff_until.present?
  end

  if thresholds_active && should_auto_pause?(rate)
    new_until = compute_auto_pause_until(auto_paused_until)
    auto_paused_until = new_until
    auto_paused_at ||= now

    attrs[:auto_paused_until] = new_until
    attrs[:auto_paused_at] = auto_paused_at
    apply_backoff(attrs, new_until)
  end

  enforce_fixed_interval(attrs, auto_paused_until)

  status = determine_status(rate, auto_paused_until, logs)
  apply_status(attrs, status)

  source.update!(attrs)
rescue ActiveRecord::RecordNotFound
  # Source was deleted between fetch and health update.
  nil
end