Class: Teapot::Commands::Pool
- Inherits:
-
Object
- Object
- Teapot::Commands::Pool
- Defined in:
- lib/teapot/commands.rb
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Pool
constructor
A new instance of Pool.
- #run(*args) ⇒ Object
- #schedule! ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Pool
Returns a new instance of Pool.
90 91 92 93 94 95 |
# File 'lib/teapot/commands.rb', line 90 def initialize( = {}) @commands = [] @limit = [:limit] || Commands.processor_count @running = Set.new end |
Instance Method Details
#run(*args) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/teapot/commands.rb', line 97 def run(*args) args = args.flatten.collect &:to_s @commands << args schedule! end |
#schedule! ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/teapot/commands.rb', line 105 def schedule! while @running.size < @limit and @commands.size > 0 command = @commands.shift puts command.join(' ').color(:blue) pid = Process.fork do exec(*command) exit!(0) end @running << pid end end |
#wait ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/teapot/commands.rb', line 121 def wait while @running.size > 0 pid = Process.wait(0) @running.delete(pid) schedule! end end |