Class PoolingExecutor
In: vendor/plugins/acts_as_searchable/lib/pooling_executor.rb
Parent: Object

Methods

<<   add_handler   new   num_tasks   run   wait_for_all  

Attributes

pool  [R] 

Public Class methods

[Source]

    # File vendor/plugins/acts_as_searchable/lib/pooling_executor.rb, line 9
 9:   def initialize
10:     @mutex = Mutex.new
11:     @pool = []
12:     @handleravail = ConditionVariable.new
13:     @threads = {}
14:     yield self if block_given?
15:   end

Public Instance methods

<<(handler)

Alias for add_handler

[Source]

    # File vendor/plugins/acts_as_searchable/lib/pooling_executor.rb, line 17
17:   def add_handler(handler)
18:     @pool << handler 
19:   end

[Source]

    # File vendor/plugins/acts_as_searchable/lib/pooling_executor.rb, line 60
60:   def num_tasks
61:     @threads.size
62:   end

[Source]

    # File vendor/plugins/acts_as_searchable/lib/pooling_executor.rb, line 23
23:   def run(logger = nil)
24:     handler = nil
25:     ref = []
26:     @mutex.synchronize do
27:       if @pool.size == 0
28:         @handleravail.wait @mutex until @pool.size > 0
29:       end
30:       handler = @pool.shift
31:       @threads[handler] = ref # so it can be removed from @threads in due time
32:       logger and logger.info("Got handler #{handler.inspect}.")
33:     end
34:     ref << Thread.new do
35:       begin
36:         logger and logger.info("Yielding handler #{handler.inspect} for execution.")
37:         yield handler
38:         logger and logger.info("Finished execution with #{handler.inspect}.")
39:       ensure
40:         @mutex.synchronize do
41:           @pool << handler
42:           @threads.delete handler
43:           @handleravail.signal
44:         end
45:       end
46:     end
47:     ref[0]
48:   end

[Source]

    # File vendor/plugins/acts_as_searchable/lib/pooling_executor.rb, line 50
50:   def wait_for_all
51:     @threads.each do |handler, thread|
52:       begin
53:         thread[0].join 
54:       rescue Exception
55:       end
56:     end
57:     @threads.clear
58:   end

[Validate]