Class: Aikido::Zen::Agent::HeartbeatsManager

Inherits:
Object
  • Object
show all
Defined in:
lib/aikido/zen/agent/heartbeats_manager.rb

Overview

Handles scheduling the heartbeats we send to the Aikido servers, managing runtime changes to the heartbeat interval.

Instance Method Summary collapse

Constructor Details

#initialize(worker:, settings: Aikido::Zen.runtime_settings, config: Aikido::Zen.config) ⇒ HeartbeatsManager

Returns a new instance of HeartbeatsManager.



7
8
9
10
11
12
13
# File 'lib/aikido/zen/agent/heartbeats_manager.rb', line 7

def initialize(worker:, settings: Aikido::Zen.runtime_settings, config: Aikido::Zen.config)
  @settings = settings
  @config = config
  @worker = worker

  @timer = nil
end

Instance Method Details

#intervalInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the current delay between events.

Returns:

  • (Integer)

    the current delay between events.



62
63
64
# File 'lib/aikido/zen/agent/heartbeats_manager.rb', line 62

def interval
  @settings.heartbeat_interval
end

#restart(&task) ⇒ void

This method returns an undefined value.

Resets the timer to start with any new settings, if needed.



54
55
56
57
# File 'lib/aikido/zen/agent/heartbeats_manager.rb', line 54

def restart(&task)
  stop
  start(&task)
end

#running?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/aikido/zen/agent/heartbeats_manager.rb', line 16

def running?
  !!@timer&.running?
end

#stale_settings?Boolean

Returns whether the currently running heartbeat matches the expected interval in the runtime settings.

Returns:

  • (Boolean)

    whether the currently running heartbeat matches the expected interval in the runtime settings.



22
23
24
# File 'lib/aikido/zen/agent/heartbeats_manager.rb', line 22

def stale_settings?
  running? && @timer.execution_interval != @settings.heartbeat_interval
end

#start(&task) ⇒ void

This method returns an undefined value.

Sets up the the timer to run the given block at the appropriate interval. Re-entrant, and does nothing if already running.



30
31
32
33
34
35
36
37
38
39
# File 'lib/aikido/zen/agent/heartbeats_manager.rb', line 30

def start(&task)
  return if running?

  if @settings.heartbeat_interval&.nonzero?
    @config.logger.debug "Scheduling heartbeats every #{@settings.heartbeat_interval} seconds"
    @timer = @worker.every(@settings.heartbeat_interval, run_now: false, &task)
  else
    @config.logger.warn(format("Heartbeat could not be set up (interval: %p)", @settings.heartbeat_interval))
  end
end

#stopvoid

This method returns an undefined value.

Cleans up the timer.



44
45
46
47
48
49
# File 'lib/aikido/zen/agent/heartbeats_manager.rb', line 44

def stop
  return unless running?

  @timer.shutdown
  @timer = nil
end