Class: Sneakers::Spawner

Inherits:
Object
  • Object
show all
Defined in:
lib/sneakers/spawner.rb

Class Method Summary collapse

Class Method Details

.spawnObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sneakers/spawner.rb', line 6

def self.spawn
  worker_group_config_file = ENV['WORKER_GROUP_CONFIG'] || './config/sneaker_worker_groups.yml'
  unless File.exist?(worker_group_config_file)
    puts 'No worker group file found.'
    puts "Specify via ENV 'WORKER_GROUP_CONFIG' or by convention ./config/sneaker_worker_groups.yml"
    Kernel.exit(1)
  end
  @pids = []
  @exec_string = 'bundle exec rake sneakers:run'
  worker_config = YAML.load(ERB.new(File.read(worker_group_config_file)).result)
  worker_config.keys.each do |group_name|
    workers = worker_config[group_name]['classes']
    workers = workers.join ',' if workers.is_a?(Array)
    @pids << fork do
      @exec_hash = { 'WORKERS' => workers, 'WORKER_COUNT' => worker_config[group_name]['workers'].to_s }
      Kernel.exec(@exec_hash, @exec_string)
    end
  end
  %w[TERM USR1 HUP USR2].each do |signal|
    Signal.trap(signal) { @pids.each { |pid| Process.kill(signal, pid) } }
  end
  Process.waitall
end