Sha256: 387ea9ef0f0bdcac8736c135f53713af96bafc7916aaf2845330c96de1ea2099

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 KB

Contents

require "thread"

module Bunny
  # Thread pool that dispatches consumer deliveries. Not supposed to be shared between channels
  # or threads.
  class ConsumerWorkPool

    #
    # API
    #

    def initialize(size = 1)
      @size  = size
      @queue = ::Queue.new
    end


    def submit(callable = nil, &block)
      @queue.push(callable || block)
    end

    def start
      @threads = []

      @size.times do
        t = Thread.new(&method(:run_loop))
        @threads << t
      end

      @started = true
    end

    def started?
      @started
    end

    def shutdown
      @size.times do
        submit do |*args|
          throw :terminate
        end
      end
    end

    def join
      @threads.each { |t| t.join }
    end

    protected

    def run_loop
      catch(:terminate) do
        loop do
          callable = @queue.pop

          begin
            callable.call
          rescue Exception => e
            # TODO
            puts e.class.name
            puts e.message
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bunny-0.9.0.pre5 lib/bunny/consumer_work_pool.rb
bunny-0.9.0.pre4 lib/bunny/consumer_work_pool.rb
bunny-0.9.0.pre3 lib/bunny/consumer_work_pool.rb
bunny-0.9.0.pre2 lib/bunny/consumer_work_pool.rb
bunny-0.9.0.pre1 lib/bunny/consumer_work_pool.rb