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

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

Overview

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

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

This module is included in Polling module, and has no other direct users.

Defined Under Namespace

Modules: PrependedMethods

Constant Summary collapse

BACK_OFF_RATIO =

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

1.2
BACK_OFF_MAX =

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

5
BASE_INTERVAL =

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

1
MUTEX_INIT =

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

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

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.



104
105
106
# File 'lib/datadog/core/workers/interval_loop.rb', line 104

def loop_back_off_max
  @loop_back_off_max ||= BACK_OFF_MAX
end

#loop_back_off_ratioObject

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.



100
101
102
# File 'lib/datadog/core/workers/interval_loop.rb', line 100

def loop_back_off_ratio
  @loop_back_off_ratio ||= BACK_OFF_RATIO
end

#loop_base_intervalObject

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.



96
97
98
# File 'lib/datadog/core/workers/interval_loop.rb', line 96

def loop_base_interval
  @loop_base_interval ||= BASE_INTERVAL
end

Class Method Details

.included(base) ⇒ Object

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.



22
23
24
# File 'lib/datadog/core/workers/interval_loop.rb', line 22

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

Instance Method Details

#loop_back_off!Object

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.



116
117
118
# File 'lib/datadog/core/workers/interval_loop.rb', line 116

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

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.

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)


124
125
126
# File 'lib/datadog/core/workers/interval_loop.rb', line 124

def loop_wait_before_first_iteration?
  false
end

#loop_wait_timeObject

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.



108
109
110
# File 'lib/datadog/core/workers/interval_loop.rb', line 108

def loop_wait_time
  @loop_wait_time ||= loop_base_interval
end

#loop_wait_time=(value) ⇒ Object

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.



112
113
114
# File 'lib/datadog/core/workers/interval_loop.rb', line 112

def loop_wait_time=(value)
  @loop_wait_time = value
end

#run_loop?Boolean

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.

TODO: Probably should be renamed to continue_loop?, see work_pending? TODO.

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/datadog/core/workers/interval_loop.rb', line 90

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

  @run_loop == true
end

#stop_loopObject

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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/datadog/core/workers/interval_loop.rb', line 44

def stop_loop
  mutex.synchronize do
    # Do not call run_loop? from this method to see if the loop
    # is running, because @run_loop is normally initialized by
    # the background thread and if the stop is requested right
    # after the worker starts, the background thread may be created
    # (and scheduled) but hasn't run yet, thus skipping the
    # write to @run_loop here would leave the thread running forever.
    @run_loop = false

    # It is possible that we don't need to signal shutdown if
    # @run_loop was not initialized (i.e. we changed it from not
    # defined to false above). But let's be safe and signal the
    # shutdown anyway, I don't see what harm it can cause.
    shutdown.signal
  end

  # Previously, this method would return false (and do nothing)
  # if the worker was not running the loop. However, this was racy -
  # see https://github.com/DataDog/ruby-guild/issues/279.
  # stop_loop now always sets the state to "stop requested" and,
  # correspondingly, always returns true.
  #
  # There is some test code that returns false when mocking this
  # method - most likely this method should be treated as a void one
  # and the caller should assume that the stop was always requested.
  true
end

#work_pending?Boolean

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.

TODO: This method carries two semantics today:

  1. Is there work available to process?

  2. Should the main loop keep running?

Things get messy when Queue is mixed in, wanting to override semantic 1 but not 2. This should probably be split into two methods: work_pending? (semantic 1) and continue_loop? (semantic 2). Today, run_loop? performs semantic 2, but should probably be renamed to continue_loop?, since that would make it clear that we’re just checking if we can “keep going inside the loop”, and not checking if we should run the loop right now. Clean up Queue after this.

Returns:

  • (Boolean)


85
86
87
# File 'lib/datadog/core/workers/interval_loop.rb', line 85

def work_pending?
  run_loop?
end