Class: Teapot::Commands::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/teapot/commands.rb

Instance Method Summary collapse

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(options = {})
	@commands = []
	@limit = options[: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

#waitObject



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