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

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

Instance Method Details

#async(*args, &block) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/lifeguard/infinite_threadpool.rb', line 18

def async(*args, &block)
  return false if @shutdown
  @queued_jobs << { :args => args, :block => block }
  check_queued_jobs

  return true
end

#check_queued_jobsObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lifeguard/infinite_threadpool.rb', line 26

def check_queued_jobs
  loop do
    break if busy?
    break if @queued_jobs.size <= 0

    @super_async_mutex.synchronize do
      break if busy?
      queued_job = @queued_jobs.pop
      super_async(*queued_job[:args], &queued_job[:block])
    end
  end
end

#kill!(*args) ⇒ Object



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

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

#on_thread_exit(thread) ⇒ Object



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

def on_thread_exit(thread)
  return_value = super
  check_queued_jobs
  return_value
end

#prune_busy_threadsObject



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

def prune_busy_threads
  response = super
  check_queued_jobs
  response
end

#shutdown(*args) ⇒ Object



56
57
58
59
# File 'lib/lifeguard/infinite_threadpool.rb', line 56

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

#super_asyncObject

Handle to original async method for check_queued_jobs to use directly



16
# File 'lib/lifeguard/infinite_threadpool.rb', line 16

alias_method :super_async, :async