Class: TranscodingMachine::Client::ServerManager

Inherits:
Object
  • Object
show all
Defined in:
lib/transcoding_machine/client/server_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(queue_settings, options) ⇒ ServerManager

Sets up a new ServerManager queue_settings are a in the following format: => {:ami => ‘ami-e444444d’,

:location => 'us-east-1c',
:key => 'my_awesome_key',
:type => 'm1.large'

}

options are:

  • :sleep_time the time to sleep between queue checks (default 30)

  • :transcoding_settings a string or lambda with the userdata to send to new transcoding servers

  • :server_count a lambda returning the needed number of transcoding servers for a given queue (defaults to the queue size)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/transcoding_machine/client/server_manager.rb', line 19

def initialize(queue_settings, options)
  @sqs = RightAws::SqsGen2.new
  @ec2 = RightAws::Ec2.new
  
  @queues = Hash.new
  queue_settings.each do |queue_name, settings|
    @queues[@sqs.queue(queue_name.to_s)] = settings
  end
  
  @server_count = options[:server_count] || lambda {|queue| queue.size}
  
  @transcoding_settings = options[:transcoding_settings]
  
  @sleep_time = options[:sleep_time] || 20
  
  @running = false
end

Instance Method Details

#ec2_ami(queue) ⇒ Object



59
60
61
# File 'lib/transcoding_machine/client/server_manager.rb', line 59

def ec2_ami(queue)
  @queues[queue][:ami]
end

#ec2_instance_type(queue) ⇒ Object



71
72
73
# File 'lib/transcoding_machine/client/server_manager.rb', line 71

def ec2_instance_type(queue)
  @queues[queue][:type]
end

#ec2_key(queue) ⇒ Object



67
68
69
# File 'lib/transcoding_machine/client/server_manager.rb', line 67

def ec2_key(queue)
  @queues[queue][:key]
end

#ec2_location(queue) ⇒ Object



63
64
65
# File 'lib/transcoding_machine/client/server_manager.rb', line 63

def ec2_location(queue)
  @queues[queue][:location]
end

#ec2_security_groups(queue) ⇒ Object



75
76
77
# File 'lib/transcoding_machine/client/server_manager.rb', line 75

def ec2_security_groups(queue)
  @queues[queue][:security_groups]
end

#manage_servers(options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/transcoding_machine/client/server_manager.rb', line 79

def manage_servers(options = {})
  @running = true
  
  while @running
    @queues.keys.each do |queue|
      needed = needed_server_count(queue)
      running = running_server_count(queue)
      
      #if needed > 0 || running > 0
        puts "#{running} of #{needed} needed servers are running for queue #{queue.name}"
      #end
      
      if running < needed
        puts "requesting #{needed - running} new servers for queue #{queue.name}"
        puts [ec2_ami(queue), 1, needed - running, ec2_security_groups(queue),
                                         ec2_key(queue), transcoding_settings(queue),
                                         nil, ec2_instance_type(queue), nil, nil, ec2_location(queue)].inspect
                                         
        new_servers = @ec2.run_instances(ec2_ami(queue), 1, needed - running, ec2_security_groups(queue),
                                         ec2_key(queue), transcoding_settings(queue),
                                         nil, ec2_instance_type(queue), nil, nil, ec2_location(queue))
      end
    end
    sleep(@sleep_time)
  end
end

#needed_server_count(queue) ⇒ Object



37
38
39
# File 'lib/transcoding_machine/client/server_manager.rb', line 37

def needed_server_count(queue)
  @server_count.call(queue)
end

#running_server_count(queue) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/transcoding_machine/client/server_manager.rb', line 46

def running_server_count(queue)
  @ec2.describe_instances.find_all do |instance|
    state = instance[:aws_state_code].to_i
    zone = instance[:aws_availability_zone]
    ami = instance[:aws_image_id]
    
    matches = state <= 16
    matches &&= ami == ec2_ami(queue)
    matches &&= zone == ec2_location(queue) if ec2_location(queue)
    matches
  end.size
end

#transcoding_settings(queue) ⇒ Object



41
42
43
44
# File 'lib/transcoding_machine/client/server_manager.rb', line 41

def transcoding_settings(queue)
  "test"
  #@transcoding_settings.respond_to?(:call) ? @transcoding_settings.call(queue) : @transcoding_settings
end