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, **_kwargs) ⇒ Object
- #determine_proxy(*args, **kwargs) ⇒ 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.
5 6 7 8 |
# File 'app/services/foreman_tasks/proxy_selector.rb', line 5 def initialize @tasks = {} @offline = [] end |
Instance Attribute Details
#offline ⇒ Object (readonly)
Returns the value of attribute offline.
3 4 5 |
# File 'app/services/foreman_tasks/proxy_selector.rb', line 3 def offline @offline end |
Instance Method Details
#available_proxies(*_args, **_kwargs) ⇒ Object
14 15 16 |
# File 'app/services/foreman_tasks/proxy_selector.rb', line 14 def available_proxies(*_args, **_kwargs) raise NotImplementedError end |
#determine_proxy(*args, **kwargs) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/services/foreman_tasks/proxy_selector.rb', line 18 def determine_proxy(*args, **kwargs) available_proxies = self.available_proxies(*args, **kwargs) return :not_defined if available_proxies.empty? || available_proxies.values.all?(&:empty?) proxy = nil strategies.each do |strategy| next if available_proxies[strategy].blank? 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
33 34 35 36 37 38 39 40 |
# File 'app/services/foreman_tasks/proxy_selector.rb', line 33 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
10 11 12 |
# File 'app/services/foreman_tasks/proxy_selector.rb', line 10 def strategies [:subnet, :fallback, :global] end |