Class: Anomonitor::Poller

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/anomonitor/poller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePoller

Returns a new instance of Poller.



12
13
14
15
16
17
18
19
20
# File 'lib/anomonitor/poller.rb', line 12

def initialize
  @mutex = Mutex.new
  @thread = nil
  @running = false
  @last_run_at = nil
  @last_error = nil
  @collector_status = {}
  @last_schema_drift_at = nil
end

Instance Attribute Details

#last_errorObject (readonly)

Returns the value of attribute last_error.



10
11
12
# File 'lib/anomonitor/poller.rb', line 10

def last_error
  @last_error
end

#last_run_atObject (readonly)

Returns the value of attribute last_run_at.



10
11
12
# File 'lib/anomonitor/poller.rb', line 10

def last_run_at
  @last_run_at
end

#runningObject (readonly)

Returns the value of attribute running.



10
11
12
# File 'lib/anomonitor/poller.rb', line 10

def running
  @running
end

Instance Method Details

#startObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/anomonitor/poller.rb', line 22

def start
  @mutex.synchronize do
    return if @running

    @running = true
    @thread = Thread.new { loop_poll }
    @thread.abort_on_exception = false
    Anomonitor.logger.info("[Anomonitor] Poller started (interval=#{Anomonitor.config.poll_interval}s)")
  end
end

#statusObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/anomonitor/poller.rb', line 64

def status
  {
    poll_mode: Anomonitor.config.poll_mode,
    running: @running,
    last_run_at: effective_last_run_at,
    last_error: @last_error,
    collectors: @collector_status,
    poll_interval: Anomonitor.config.poll_interval,
    schema_drift_interval: Anomonitor.config.schema_drift_interval,
    poll_lock: Anomonitor.config.poll_lock
  }
end

#stopObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/anomonitor/poller.rb', line 33

def stop
  thread = nil
  @mutex.synchronize do
    @running = false
    thread = @thread
    @thread = nil
  end
  return unless thread

  # Prefer a cooperative stop over Thread#kill mid-insert/webhook
  thread.join(Anomonitor.config.poll_interval.to_i + 5)
  thread.kill if thread.alive?
end

#tickObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/anomonitor/poller.rb', line 47

def tick
  PollLock.with_lock do
    points = collect_all
    persist(points)
    Detector.new.evaluate(points)
    Mute.prune_expired! if defined?(Anomonitor::Mute)
    prune_old_records
    @last_run_at = Time.current
    @last_error = nil
    points
  end
rescue StandardError => e
  @last_error = e.message
  Anomonitor.logger.warn("[Anomonitor] Poller tick failed: #{e.message}")
  []
end