Sha256: 72599fd5ca3cdacaba8cf18c0c89c1fbfa599ecb9e3887e273e34fb67c05fcd6

Contents?: true

Size: 896 Bytes

Versions: 8

Compression:

Stored size: 896 Bytes

Contents

module EventSourcery
  module Postgres
    class QueueWithIntervalCallback < ::Queue
      attr_accessor :callback

      def initialize(callback: proc {}, callback_interval: EventSourcery::Postgres.config.callback_interval_if_no_new_events, poll_interval: 0.1)
        @callback = callback
        @callback_interval = callback_interval
        @poll_interval = poll_interval
        super()
      end

      def pop(non_block_without_callback = false)
        return super if non_block_without_callback
        pop_with_interval_callback
      end

      private

      def pop_with_interval_callback
        time = Time.now
        loop do
          return pop(true) unless empty?
          if @callback_interval && Time.now > time + @callback_interval
            @callback.call
            time = Time.now
          end
          sleep @poll_interval
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
event_sourcery-postgres-0.9.1 lib/event_sourcery/postgres/queue_with_interval_callback.rb
event_sourcery-postgres-0.9.0 lib/event_sourcery/postgres/queue_with_interval_callback.rb
event_sourcery-postgres-0.8.1 lib/event_sourcery/postgres/queue_with_interval_callback.rb
event_sourcery-postgres-0.8.0 lib/event_sourcery/postgres/queue_with_interval_callback.rb
event_sourcery-postgres-0.7.0 lib/event_sourcery/postgres/queue_with_interval_callback.rb
event_sourcery-postgres-0.6.0 lib/event_sourcery/postgres/queue_with_interval_callback.rb
event_sourcery-postgres-0.5.0 lib/event_sourcery/postgres/queue_with_interval_callback.rb
event_sourcery-postgres-0.4.0 lib/event_sourcery/postgres/queue_with_interval_callback.rb