Class: Trident::Pool

Inherits:
Object
  • Object
show all
Includes:
GemLogger::LoggerSupport, Utils
Defined in:
lib/trident/pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#procline

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, options={})
  @name = name
  @handler = handler
  @size = options.delete('size') || 2
  @options = options || {}
  @workers = Set.new
  @orphans_dir = options.delete('pids_dir') || File.join(Dir.pwd, 'trident-pools', name, 'pids')
  @orphans = load_orphans(orphans_dir)
end

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



6
7
8
# File 'lib/trident/pool.rb', line 6

def handler
  @handler
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/trident/pool.rb', line 6

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/trident/pool.rb', line 6

def options
  @options
end

#orphansObject (readonly)

Returns the value of attribute orphans.



6
7
8
# File 'lib/trident/pool.rb', line 6

def orphans
  @orphans
end

#orphans_dirObject (readonly)

Returns the value of attribute orphans_dir.



6
7
8
# File 'lib/trident/pool.rb', line 6

def orphans_dir
  @orphans_dir
end

#sizeObject (readonly)

Returns the value of attribute size.



6
7
8
# File 'lib/trident/pool.rb', line 6

def size
  @size
end

#workersObject (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

Returns:

  • (Boolean)

    true iff total_workers_count > size.



64
65
66
# File 'lib/trident/pool.rb', line 64

def above_threshold?
  size < total_workers_count
end

#at_threshold?Boolean

false otherwise

Returns:

  • (Boolean)

    true iff total_workers_count == size.



70
71
72
# File 'lib/trident/pool.rb', line 70

def at_threshold?
  size == total_workers_count
end

#has_workers?Boolean

false otherwise

Returns:

  • (Boolean)

    true iff workers.size > 0.



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

#startObject



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_countInteger

workers.

Returns:

  • (Integer)

    total number of workers including orphaned



82
83
84
# File 'lib/trident/pool.rb', line 82

def total_workers_count
  workers.size + orphans.size
end

#updateObject



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

#waitObject



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