Sha256: 936ef869d75ce8df50966e5bcdfa90e5f7c5c39f8cd38b9975d59ca37477f79d

Contents?: true

Size: 1.53 KB

Versions: 115

Compression:

Stored size: 1.53 KB

Contents

module ForemanTasks
  class ProxySelector
    attr_reader :offline

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

    def strategies
      [:subnet, :fallback, :global]
    end

    def available_proxies(*_args)
      raise NotImplementedError
    end

    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 if available_proxies[strategy].blank?
        proxy = select_by_jobs_count(available_proxies[strategy])
        break if proxy
      end

      proxy || :not_available
    end

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

    private

    def get_counts(proxies)
      proxies.each_with_object({}) do |proxy, result|
        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
end

Version data entries

115 entries across 115 versions & 1 rubygems

Version Path
foreman-tasks-0.11.1 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.10.9 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.11.0 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.10.8 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.10.7 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.10.6 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.10.4 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.9.6 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.10.3 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.10.2 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.10.1 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.9.5 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.10.0 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.9.4 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.9.3 app/services/foreman_tasks/proxy_selector.rb