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.3.19 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.18 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.17 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.16 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.15 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.14 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.13 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.12 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.11 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.10 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.9 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.8 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.7 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.6 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.5 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.4 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.3 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.2 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.1 lib/flydata/fluent-plugins/idle_event_detector.rb
flydata-0.3.0 lib/flydata/fluent-plugins/idle_event_detector.rb