Module: Datadog::Core::Workers::IntervalLoop

Defined in:
lib/datadog/core/workers/interval_loop.rb

Overview

Adds looping behavior to workers, with a sleep interval between each loop.

Defined Under Namespace

Modules: PrependedMethods

Constant Summary collapse

BACK_OFF_RATIO =
1.2
BACK_OFF_MAX =
5
BASE_INTERVAL =
1
MUTEX_INIT =

This single shared mutex is used to avoid concurrency issues during the initialization of per-instance lazy-initialized mutexes.

Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loop_back_off_maxObject



57
58
59
# File 'lib/datadog/core/workers/interval_loop.rb', line 57

def loop_back_off_max
  @loop_back_off_max ||= BACK_OFF_MAX
end

#loop_back_off_ratioObject



53
54
55
# File 'lib/datadog/core/workers/interval_loop.rb', line 53

def loop_back_off_ratio
  @loop_back_off_ratio ||= BACK_OFF_RATIO
end

#loop_base_intervalObject



49
50
51
# File 'lib/datadog/core/workers/interval_loop.rb', line 49

def loop_base_interval
  @loop_base_interval ||= BASE_INTERVAL
end

Class Method Details

.included(base) ⇒ Object



17
18
19
# File 'lib/datadog/core/workers/interval_loop.rb', line 17

def self.included(base)
  base.prepend(PrependedMethods)
end

Instance Method Details

#loop_back_off!Object



69
70
71
# File 'lib/datadog/core/workers/interval_loop.rb', line 69

def loop_back_off!
  self.loop_wait_time = [loop_wait_time * BACK_OFF_RATIO, BACK_OFF_MAX].min
end

#loop_wait_before_first_iteration?Boolean

Should perform_loop just straight into work, or start by waiting?

The use case is if we want to report some information (like profiles) from time to time, we may not want to report empty/zero/some residual value immediately when the worker starts.

Returns:

  • (Boolean)


77
78
79
# File 'lib/datadog/core/workers/interval_loop.rb', line 77

def loop_wait_before_first_iteration?
  false
end

#loop_wait_timeObject



61
62
63
# File 'lib/datadog/core/workers/interval_loop.rb', line 61

def loop_wait_time
  @loop_wait_time ||= loop_base_interval
end

#loop_wait_time=(value) ⇒ Object



65
66
67
# File 'lib/datadog/core/workers/interval_loop.rb', line 65

def loop_wait_time=(value)
  @loop_wait_time = value
end

#run_loop?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
# File 'lib/datadog/core/workers/interval_loop.rb', line 43

def run_loop?
  return false unless instance_variable_defined?(:@run_loop)

  @run_loop == true
end

#stop_loopObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/datadog/core/workers/interval_loop.rb', line 28

def stop_loop
  mutex.synchronize do
    return false unless run_loop?

    @run_loop = false
    shutdown.signal
  end

  true
end

#work_pending?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/datadog/core/workers/interval_loop.rb', line 39

def work_pending?
  run_loop?
end