Class: LogStash::Inputs::Snmp::StoppableIntervalRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/inputs/snmp.rb

Overview

The StoppableIntervalRunner is capable of running a block of code at a repeating interval, while respecting the stop condition of the plugin.

Instance Method Summary collapse

Constructor Details

#initialize(plugin) ⇒ StoppableIntervalRunner

Returns a new instance of StoppableIntervalRunner.

Parameters:

  • plugin (#logger, #stop?)


335
336
337
# File 'lib/logstash/inputs/snmp.rb', line 335

def initialize(plugin)
  @plugin = plugin
end

Instance Method Details

#every(interval_seconds, desc = "operation") { ... } ⇒ Object

Runs the provided block repeatedly using the provided interval. After executing the block, the remainder of the interval if any is slept off using an interruptible sleep. If no time remains, a warning is emitted to the logs.

Parameters:

  • interval_seconds (Integer, Float)
  • desc (String) (defaults to: "operation")

    (default: “operation”): a description to use when logging

Yields:



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/logstash/inputs/snmp.rb', line 348

def every(interval_seconds, desc="operation", &block)
  until @plugin.stop?
    start_time = Time.now

    yield

    duration_seconds = Time.now - start_time
    if duration_seconds >= interval_seconds
      @plugin.logger.warn("#{desc} took longer than the configured interval", :interval_seconds => interval_seconds, :duration_seconds => duration_seconds.round(3))
    else
      remaining_interval = interval_seconds - duration_seconds
      sleep(remaining_interval)
    end
  end
end

#sleep(duration) ⇒ 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.



365
366
367
# File 'lib/logstash/inputs/snmp.rb', line 365

def sleep(duration)
  Stud.stoppable_sleep(duration) { @plugin.stop? }
end