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, #on_thread_exit, #prune_busy_threads, #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
# File 'lib/lifeguard/infinite_threadpool.rb', line 13

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

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

      callable.call(*call_args)
    end

    ::Thread.pass while new_thread.alive?
  else
    super(*args, &block)
  end

  return true
end

#kill!(*args) ⇒ Object



34
35
36
37
# File 'lib/lifeguard/infinite_threadpool.rb', line 34

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

#shutdown(*args) ⇒ Object



39
40
41
42
# File 'lib/lifeguard/infinite_threadpool.rb', line 39

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