Class: Lifeguard::InfiniteThreadpool

Inherits:
Threadpool show all
Defined in:
lib/lifeguard/infinite_threadpool.rb

Constant Summary

Constants inherited from Threadpool

Threadpool::DEFAULT_POOL_SIZE, Threadpool::DEFAULT_REAPING_INTERVAL

Instance Attribute Summary

Attributes inherited from Threadpool

#name, #options, #pool_size

Instance Method Summary collapse

Methods inherited from Threadpool

#busy?, #busy_size, #timeout!, #timeout?

Constructor Details

#initialize(opts = {}) ⇒ InfiniteThreadpool

Returns a new instance of InfiniteThreadpool.



7
8
9
10
11
# File 'lib/lifeguard/infinite_threadpool.rb', line 7

def initialize(opts = {})
  super(opts)
  @shutdown = false
  @super_async_mutex = ::Mutex.new
end

Instance Method Details

#async(*args, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lifeguard/infinite_threadpool.rb', line 13

def async(*args, &block)
  return false if @shutdown

  if busy?
    job_mutex = ::Mutex.new
    job_condition = ::ConditionVariable.new
    # Account for "weird" exceptions like Java Exceptions or higher up the chain
    # than what `rescue nil` will capture
    job_mutex.synchronize do
      new_thread = ::Thread.new(block, args) do |callable, call_args|
        job_mutex.synchronize do
          begin
            ::Thread.current[:__start_time_in_seconds__] = Time.now.to_i
            ::Thread.current.abort_on_exception = false

            callable.call(*call_args)
          ensure
            job_condition.signal
          end
        end
      end

      job_condition.wait(job_mutex)
    end
  else
    super(*args, &block)
  end

  return true
end

#kill!(*args) ⇒ Object



44
45
46
47
# File 'lib/lifeguard/infinite_threadpool.rb', line 44

def kill!(*args)
  super
  @shutdown = true
end

#shutdown(*args) ⇒ Object



49
50
51
52
# File 'lib/lifeguard/infinite_threadpool.rb', line 49

def shutdown(*args)
  @shutdown = true
  super
end