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.



74
75
76
77
78
79
# File 'lib/teapot/commands.rb', line 74

def initialize(options = {})
  @commands = []
  @limit = options[:limit] || Commands.processor_count
  
  @running = Set.new
end

Instance Method Details

#run(*args) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/teapot/commands.rb', line 81

def run(*args)
  args = args.flatten.collect &:to_s
  
  @commands << args
  
  schedule!
end

#schedule!Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/teapot/commands.rb', line 89

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



105
106
107
108
109
110
111
112
# File 'lib/teapot/commands.rb', line 105

def wait
  while @running.size > 0
    pid = Process.wait(0)
    @running.delete(pid)
    
    schedule!
  end
end