Class: NdrStats::Ping

Inherits:
Object
  • Object
show all
Defined in:
lib/ndr_stats/ping.rb

Overview

Ping instances make regular increments to a :ping counter, as a means of checking that a progress is up.

They can be started and stopped, as well as registered and removed centrally. Generally, use via ‘NdrStats.ping` is recommended.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(every: 60, type:, **tags) ⇒ Ping

Returns a new instance of Ping.



38
39
40
41
42
43
# File 'lib/ndr_stats/ping.rb', line 38

def initialize(every: 60, type:, **tags)
  @interval = every
  @tags = tags.merge!(type: type)

  @thread = nil
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



36
37
38
# File 'lib/ndr_stats/ping.rb', line 36

def interval
  @interval
end

#tagsObject (readonly)

Returns the value of attribute tags.



36
37
38
# File 'lib/ndr_stats/ping.rb', line 36

def tags
  @tags
end

Class Method Details

.listObject



11
12
13
# File 'lib/ndr_stats/ping.rb', line 11

def list
  @list ||= []
end

.register(**args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ndr_stats/ping.rb', line 15

def register(**args)
  instance = new(**args)

  if list.detect { |other| other.tags == instance.tags }
    raise ArgumentError, 'another tagged instance already exists!'
  else
    list << instance
  end

  instance.tap(&:start)
end

.remove_allObject



27
28
29
30
# File 'lib/ndr_stats/ping.rb', line 27

def remove_all
  list.each(&:stop)
  list.clear
end

Instance Method Details

#running?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/ndr_stats/ping.rb', line 62

def running?
  @thread&.alive?
end

#startObject



45
46
47
48
49
50
51
# File 'lib/ndr_stats/ping.rb', line 45

def start
  raise 'already started!' if running?

  initial_ping
  @thread = Thread.new { ping_forever }
  self
end

#stopObject



53
54
55
56
57
58
59
60
# File 'lib/ndr_stats/ping.rb', line 53

def stop
  return unless @thread

  @thread.kill
  @thread.join

  final_ping
end