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-1.0.0 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.17.5 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.17.4 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.17.3 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.17.2 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.16.3 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.17.1 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.15.11 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.16.2 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.15.10 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.16.1 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.17.0 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.15.9 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.15.8 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.14.6 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.15.7 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.16.0 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.15.6 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.15.5 app/services/foreman_tasks/proxy_selector.rb
foreman-tasks-0.15.4 app/services/foreman_tasks/proxy_selector.rb