Sha256: 746e260cb62a737639c2c89c6db028866966d67405942bee774ae4ea346acd8c

Contents?: true

Size: 875 Bytes

Versions: 5

Compression:

Stored size: 875 Bytes

Contents

class ProxyLoadBalancer

  attr_reader :offline

  def initialize
    @tasks   = {}
    @offline = []
  end

  # Get the least loaded proxy from the given list of proxies
  def next(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

  private

  def get_counts(proxies)
    proxies.inject({}) do |result, proxy|
      begin
        proxy_api = ProxyAPI::ForemanDynflow::DynflowProxy.new(:url => proxy.url)
        result[proxy] = proxy_api.tasks_count('running')
      rescue => e
        @offline << proxy
        Foreman::Logging.exception "Could not fetch task counts from #{proxy}, skipped.", e
      end
      result
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
foreman_remote_execution-1.1.1 app/services/proxy_load_balancer.rb
foreman_remote_execution-1.1.0 app/services/proxy_load_balancer.rb
foreman_remote_execution-1.0.0 app/services/proxy_load_balancer.rb
foreman_remote_execution-0.3.2 app/services/proxy_load_balancer.rb
foreman_remote_execution-0.3.1 app/services/proxy_load_balancer.rb