Sha256: 4d081918b2ff7a6bb4f7c950ab2bf9cacd8e2dd667a3738dc1da00c5fea6ba29

Contents?: true

Size: 1.49 KB

Versions: 29

Compression:

Stored size: 1.49 KB

Contents

module Fluent

class IdleEventDetector
  def initialize(initial_interval, continuous_interval, check_interval = 5)
    @mutex = Mutex.new
    @initial_interval = initial_interval
    @continuous_interval = continuous_interval
    @check_interval = check_interval
  end

  def start(&block)
    raise "block must be given" unless block_given?
    @callback = block
    stop if @thread
    @last_event_at = @timestamp = Time.current
    @is_absent = false

    @thread = Thread.new do
      loop do
        check_state
        sleep @check_interval
      end
    end
  end

  def stop
    @thread.exit if @thread
    @thread = nil
    @is_absent = false
    @last_event_at = @timestamp = nil
    @callback = nil
  end

  def notify
    update_last_event_time
  end

  private

  def update_last_event_time
    @mutex.synchronize do
      @last_event_at = @timestamp = Time.current
      if @is_absent
        @callback.call(:event_arrived_finally, @last_event_at) if @callback
        @is_absent = false
      end
    end
  end

  def check_state
    @mutex.synchronize do
      if @is_absent
        if Time.current - @timestamp >= @continuous_interval
          @callback.call(:event_still_not_coming, @last_event_at) if @callback
          @timestamp = Time.current
        end
      else
        if Time.current - @timestamp >= @initial_interval
          @callback.call(:event_not_coming, @last_event_at) if @callback
          @is_absent = true
          @timestamp = Time.current
        end
      end
    end
  end
end

end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
flydata-0.2.30 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.2.29 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.2.28 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.2.27 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.2.26 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.2.25 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.2.24 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.2.23 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.2.22 lib/flydata/fluent-plugins/idle_event_detector.rb