Class: Trident::Pool
- Inherits:
-
Object
- Object
- Trident::Pool
- Includes:
- GemLogger::LoggerSupport, Utils
- Defined in:
- lib/trident/pool.rb
Instance Attribute Summary collapse
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#orphans ⇒ Object
readonly
Returns the value of attribute orphans.
-
#orphans_dir ⇒ Object
readonly
Returns the value of attribute orphans_dir.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#workers ⇒ Object
readonly
Returns the value of attribute workers.
Instance Method Summary collapse
-
#above_threshold? ⇒ Boolean
false otherwise.
-
#at_threshold? ⇒ Boolean
false otherwise.
-
#has_workers? ⇒ Boolean
false otherwise.
-
#initialize(name, handler, options = {}) ⇒ Pool
constructor
A new instance of Pool.
- #load_orphans(path_to_orphans_dir) ⇒ Object
- #start ⇒ Object
- #stop(action = 'stop_gracefully') ⇒ Object
-
#total_workers_count ⇒ Integer
workers.
- #update ⇒ Object
- #wait ⇒ Object
Methods included from Utils
Constructor Details
#initialize(name, handler, options = {}) ⇒ Pool
Returns a new instance of Pool.
8 9 10 11 12 13 14 15 16 |
# File 'lib/trident/pool.rb', line 8 def initialize(name, handler, ={}) @name = name @handler = handler @size = .delete('size') || 2 @options = || {} @workers = Set.new @orphans_dir = .delete('pids_dir') || File.join(Dir.pwd, 'trident-pools', name, 'pids') @orphans = load_orphans(orphans_dir) end |
Instance Attribute Details
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
6 7 8 |
# File 'lib/trident/pool.rb', line 6 def handler @handler end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/trident/pool.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/trident/pool.rb', line 6 def @options end |
#orphans ⇒ Object (readonly)
Returns the value of attribute orphans.
6 7 8 |
# File 'lib/trident/pool.rb', line 6 def orphans @orphans end |
#orphans_dir ⇒ Object (readonly)
Returns the value of attribute orphans_dir.
6 7 8 |
# File 'lib/trident/pool.rb', line 6 def orphans_dir @orphans_dir end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
6 7 8 |
# File 'lib/trident/pool.rb', line 6 def size @size end |
#workers ⇒ Object (readonly)
Returns the value of attribute workers.
6 7 8 |
# File 'lib/trident/pool.rb', line 6 def workers @workers end |
Instance Method Details
#above_threshold? ⇒ Boolean
false otherwise
64 65 66 |
# File 'lib/trident/pool.rb', line 64 def above_threshold? size < total_workers_count end |
#at_threshold? ⇒ Boolean
false otherwise
70 71 72 |
# File 'lib/trident/pool.rb', line 70 def at_threshold? size == total_workers_count end |
#has_workers? ⇒ Boolean
false otherwise
76 77 78 |
# File 'lib/trident/pool.rb', line 76 def has_workers? workers.size > 0 end |
#load_orphans(path_to_orphans_dir) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/trident/pool.rb', line 18 def load_orphans(path_to_orphans_dir) unless File.exists?(path_to_orphans_dir) FileUtils.mkdir_p(path_to_orphans_dir) end orphans = Set.new Dir.foreach(path_to_orphans_dir) do |file| path = File.join(path_to_orphans_dir, file) next if File.directory?(path) pid = Integer(IO.read(path)) orphan_worker = Worker.new(pid, self) orphans << orphan_worker end orphans end |
#start ⇒ Object
37 38 39 40 41 |
# File 'lib/trident/pool.rb', line 37 def start logger.info "<pool-#{name}> Starting pool" maintain_worker_count('stop_gracefully') logger.info "<pool-#{name}> Pool started with #{workers.size} workers" end |
#stop(action = 'stop_gracefully') ⇒ Object
43 44 45 46 47 48 |
# File 'lib/trident/pool.rb', line 43 def stop(action='stop_gracefully') logger.info "<pool-#{name}> Stopping pool" @size = 0 maintain_worker_count(action) logger.info "<pool-#{name}> Pool stopped" end |
#total_workers_count ⇒ Integer
workers.
82 83 84 |
# File 'lib/trident/pool.rb', line 82 def total_workers_count workers.size + orphans.size end |
#update ⇒ Object
56 57 58 59 60 |
# File 'lib/trident/pool.rb', line 56 def update logger.info "<pool-#{name}> Updating pool" maintain_worker_count('stop_gracefully') logger.info "<pool-#{name}> Pool up to date" end |
#wait ⇒ Object
50 51 52 53 54 |
# File 'lib/trident/pool.rb', line 50 def wait logger.info "<pool-#{name}> Waiting for pool" cleanup_dead_workers(true) logger.info "<pool-#{name}> Wait complete" end |