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)
  @queued_jobs = ::Queue.new
  @shutdown = false
end

Instance Method Details

#async(*args, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lifeguard/infinite_threadpool.rb', line 13

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

  check_queued_jobs
  job_started = super

  unless job_started
    @queued_jobs << { :args => args, :block => block }
  end

  job_started
end

#check_queued_jobsObject



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

def check_queued_jobs
  return if busy?
  return if @queued_jobs.size <= 0

  queued_job = @queued_jobs.pop
  async(*queued_job[:args], &queued_job[:block])
  check_queued_jobs
end

#kill!(*args) ⇒ Object



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

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

#on_thread_exit(thread) ⇒ Object



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

def on_thread_exit(thread)
  return_value = super
  check_queued_jobs
  return_value
end

#prune_busy_threadsObject



46
47
48
49
50
# File 'lib/lifeguard/infinite_threadpool.rb', line 46

def prune_busy_threads
  response = super
  check_queued_jobs
  response
end

#shutdown(*args) ⇒ Object



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

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