Class: Jefe::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/jefe/loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Loader

Returns a new instance of Loader.



3
4
5
# File 'lib/jefe/loader.rb', line 3

def initialize(file)
	@process_types = Hash[file.lines.map &method(:decompose_line)]
end

Instance Attribute Details

#process_typesObject (readonly)

Returns the value of attribute process_types.



2
3
4
# File 'lib/jefe/loader.rb', line 2

def process_types
  @process_types
end

Instance Method Details

#decompose_line(line) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/jefe/loader.rb', line 6

def decompose_line line
	if line =~ /^([A-Za-z0-9_]+):\s*(.+)$/
		[$1, $2]
	elsif line =~ /^#/
		# Line is commented-out, ignore
	else
		raise ArgumentError
	end
end

#scale(concurrency, port) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jefe/loader.rb', line 15

def scale(concurrency, port)
	tasks = []
	if concurrency.empty?
		concurrency = Hash[process_types.keys.map { |name| [name, 1] }]
	end
	concurrency.each do |(name, num)|
		num.times do |i|
			env = { "PORT" => (port + i) }
			command = @process_types[name].gsub(/\$(\w+)/) { env[$1] || ENV[$1] }
			tasks.push ["#{name}.#{i}", command]
		end
		port += 100
	end
	tasks
end