Sha256: 1aa4e5f28324e944ed20b659df2859cdbc076e6064a7be4589dace1f603f35a8

Contents?: true

Size: 992 Bytes

Versions: 3

Compression:

Stored size: 992 Bytes

Contents

module TestQueue
  class Iterator
    attr_reader :stats

    def initialize(sock)
      @sock = sock
      @done = false
      @stats = {}
      @procline = $0
    end

    def each
      fail 'already used this iterator' if @done

      while true
        client = UNIXSocket.new(@sock)
        r, w, e = IO.select([client], nil, [client], nil)
        break if !e.empty?

        if data = client.read(16384)
          client.close
          item = Marshal.load(data)
          $0 = "#{@procline} - #{item.respond_to?(:description) ? item.description : item}"

          start = Time.now
          yield item
          @stats[item] = Time.now - start
        else
          break
        end
      end
    rescue Errno::ENOENT, Errno::ECONNRESET, Errno::ECONNREFUSED
    ensure
      @done = true
      File.open("/tmp/test_queue_worker_#{$$}_stats", "wb") do |f|
        f.write Marshal.dump(@stats)
      end
    end

    include Enumerable

    def empty?
      false
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
test-queue-0.1.3 lib/test_queue/iterator.rb
test-queue-0.1.2 lib/test_queue/iterator.rb
test-queue-0.1.1 lib/test_queue/iterator.rb