Class: WrataApi::ServerList
- Inherits:
-
Object
- Object
- WrataApi::ServerList
- Defined in:
- lib/wrata_api/server_list.rb
Overview
Class for working with several servers at once
Instance Attribute Summary collapse
-
#servers ⇒ Array, Hash
List of servers.
Instance Method Summary collapse
-
#book ⇒ Nothing
Book each server.
-
#concurrent_action ⇒ Object
Turn on all servers in list.
-
#initialize(api) ⇒ ServerList
constructor
A new instance of ServerList.
-
#length ⇒ Integer
Count of servers.
-
#power_off ⇒ Nothing
Power on all servers.
-
#power_on(size = nil) ⇒ Nothing
Power on all servers.
-
#powered_off? ⇒ True, False
Is all servers powered on?.
-
#powered_on? ⇒ True, False
Is all servers powered on?.
-
#unbook ⇒ Nothing
Unbook each server.
Constructor Details
#initialize(api) ⇒ ServerList
Returns a new instance of ServerList.
9 10 11 12 13 |
# File 'lib/wrata_api/server_list.rb', line 9 def initialize(api) @api = api @servers = [] @concurrent_operation_at_once = 5 end |
Instance Attribute Details
#servers ⇒ Array, Hash
Returns list of servers.
7 8 9 |
# File 'lib/wrata_api/server_list.rb', line 7 def servers @servers end |
Instance Method Details
#book ⇒ Nothing
Book each server
68 69 70 71 72 |
# File 'lib/wrata_api/server_list.rb', line 68 def book @servers.each do |current| @api.book_server(current['name']) end end |
#concurrent_action ⇒ Object
Turn on all servers in list
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/wrata_api/server_list.rb', line 21 def concurrent_action splitted_servers = @servers.each_slice(@concurrent_operation_at_once).to_a splitted_servers.each do |each_slice| threads = [] each_slice.each do |current_server| threads << Thread.new(current_server) do yield(current_server) end end threads.each(&:join) end end |
#length ⇒ Integer
Returns count of servers.
16 17 18 |
# File 'lib/wrata_api/server_list.rb', line 16 def length @servers.length end |
#power_off ⇒ Nothing
Power on all servers
52 53 54 55 56 |
# File 'lib/wrata_api/server_list.rb', line 52 def power_off concurrent_action do |server| @api.power_off_server(server['name']) end end |
#power_on(size = nil) ⇒ Nothing
Power on all servers
36 37 38 39 40 |
# File 'lib/wrata_api/server_list.rb', line 36 def power_on(size = nil) concurrent_action do |server| @api.power_on_server(server['name'], size) end end |
#powered_off? ⇒ True, False
Returns is all servers powered on?.
59 60 61 62 63 64 |
# File 'lib/wrata_api/server_list.rb', line 59 def powered_off? @servers.each do |current| return false unless @api.powering_status(current['name']) == :off end true end |
#powered_on? ⇒ True, False
Returns is all servers powered on?.
43 44 45 46 47 48 |
# File 'lib/wrata_api/server_list.rb', line 43 def powered_on? @servers.each do |current| return false unless @api.powering_status(current['name']) == :on end true end |
#unbook ⇒ Nothing
Unbook each server
76 77 78 79 80 |
# File 'lib/wrata_api/server_list.rb', line 76 def unbook @servers.each do |current| @api.unbook_server(current['name']) end end |