class LogStash::Outputs::GoogleCloudStorage::Interval

Bare-bones utility for running a block of code at an interval.

Public Class Methods

new(interval, procsy) click to toggle source

@param interval [Integer]: time in seconds to wait between calling the given proc @param procsy [#call]: proc or lambda to call periodically; must not raise exceptions.

# File lib/logstash/outputs/googlecloudstorage.rb, line 396
def initialize(interval, procsy)
  @interval = interval
  @procsy = procsy

  require 'thread' # Mutex, ConditionVariable, etc.
  @mutex = Mutex.new
  @sleeper = ConditionVariable.new
end
start(interval, procsy) click to toggle source

Initializes a new Interval with the given arguments and starts it before returning it.

@param interval [Integer] (see: Interval#initialize) @param procsy [#call] (see: Interval#initialize)

@return [Interval]

# File lib/logstash/outputs/googlecloudstorage.rb, line 389
def self.start(interval, procsy)
  self.new(interval, procsy).tap(&:start)
end

Public Instance Methods

alive?() click to toggle source

@return [Boolean]

# File lib/logstash/outputs/googlecloudstorage.rb, line 430
def alive?
  @thread && @thread.alive?
end
start() click to toggle source

Starts the interval, or returns if it has already been started.

@return [void]

# File lib/logstash/outputs/googlecloudstorage.rb, line 409
def start
  @mutex.synchronize do
    return if @thread && @thread.alive?

    @thread = Thread.new { run }
  end
end
stop() click to toggle source

Stop the interval. Does not interrupt if execution is in-progress.

# File lib/logstash/outputs/googlecloudstorage.rb, line 420
def stop
  @mutex.synchronize do
    @stopped = true
  end

  @thread && @thread.join
end

Private Instance Methods

run() click to toggle source
# File lib/logstash/outputs/googlecloudstorage.rb, line 436
def run
  @mutex.synchronize do
    loop do
      @sleeper.wait(@mutex, @interval)
      break if @stopped

      @procsy.call
    end
  end
ensure
  @sleeper.broadcast
end