Class: ForemanTasks::ProxySelector
- Inherits:
-
Object
- Object
- ForemanTasks::ProxySelector
- Defined in:
- app/services/foreman_tasks/proxy_selector.rb
Instance Attribute Summary collapse
-
#offline ⇒ Object
readonly
Returns the value of attribute offline.
Instance Method Summary collapse
- #available_proxies(*args) ⇒ Object
- #determine_proxy(*args) ⇒ Object
-
#initialize ⇒ ProxySelector
constructor
A new instance of ProxySelector.
-
#select_by_jobs_count(proxies) ⇒ Object
Get the least loaded proxy from the given list of proxies.
- #strategies ⇒ Object
Constructor Details
#initialize ⇒ ProxySelector
Returns a new instance of ProxySelector.
6 7 8 9 |
# File 'app/services/foreman_tasks/proxy_selector.rb', line 6 def initialize @tasks = {} @offline = [] end |
Instance Attribute Details
#offline ⇒ Object (readonly)
Returns the value of attribute offline.
4 5 6 |
# File 'app/services/foreman_tasks/proxy_selector.rb', line 4 def offline @offline end |
Instance Method Details
#available_proxies(*args) ⇒ Object
15 16 17 |
# File 'app/services/foreman_tasks/proxy_selector.rb', line 15 def available_proxies(*args) raise NotImplementedError end |
#determine_proxy(*args) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/services/foreman_tasks/proxy_selector.rb', line 19 def determine_proxy(*args) available_proxies = self.available_proxies(*args) return :not_defined if available_proxies.empty? || available_proxies.values.all?(&:empty?) proxy = nil strategies.each do |strategy| next unless available_proxies[strategy].present? proxy = select_by_jobs_count(available_proxies[strategy]) break if proxy end proxy || :not_available end |
#select_by_jobs_count(proxies) ⇒ Object
Get the least loaded proxy from the given list of proxies
34 35 36 37 38 39 40 41 |
# File 'app/services/foreman_tasks/proxy_selector.rb', line 34 def select_by_jobs_count(proxies) exclude = @tasks.keys + @offline @tasks.merge!(get_counts(proxies - exclude)) next_proxy = @tasks.select { |proxy, _| proxies.include?(proxy) }. min_by { |_, job_count| job_count }.try(:first) @tasks[next_proxy] += 1 if next_proxy.present? next_proxy end |
#strategies ⇒ Object
11 12 13 |
# File 'app/services/foreman_tasks/proxy_selector.rb', line 11 def strategies [:subnet, :fallback, :global] end |