Sha256: 42e78f642240fde7d9f6ca329b4e7a1ff20314cd0331f260e7dba53e15de8ab7

Contents?: true

Size: 1.36 KB

Versions: 11

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true
module KubernetesDeploy
  class ResourceWatcher
    def initialize(resources)
      unless resources.is_a?(Enumerable)
        raise ArgumentError, <<-MSG.strip
ResourceWatcher expects Enumerable collection, got `#{resources.class}` instead
MSG
      end
      @resources = resources
    end

    def run(delay_sync: 3.seconds, logger: KubernetesDeploy.logger)
      delay_sync_until = Time.now.utc
      started_at = delay_sync_until
      human_resources = @resources.map(&:id).join(", ")
      max_wait_time = @resources.map(&:timeout).max
      logger.info("Waiting for #{human_resources} with #{max_wait_time}s timeout")

      while @resources.present?
        if Time.now.utc < delay_sync_until
          sleep(delay_sync_until - Time.now.utc)
        end
        delay_sync_until = Time.now.utc + delay_sync # don't pummel the API if the sync is fast
        @resources.each(&:sync)
        newly_finished_resources, @resources = @resources.partition(&:deploy_finished?)
        newly_finished_resources.each do |resource|
          next unless resource.deploy_failed? || resource.deploy_timed_out?
          logger.error("#{resource.id} failed to deploy with status '#{resource.status}'.")
        end
      end

      watch_time = Time.now.utc - started_at
      logger.info("Spent #{watch_time.round(2)}s waiting for #{human_resources}")
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
kubernetes-deploy-0.6.6 lib/kubernetes-deploy/resource_watcher.rb
kubernetes-deploy-0.6.5 lib/kubernetes-deploy/resource_watcher.rb
kubernetes-deploy-0.6.4 lib/kubernetes-deploy/resource_watcher.rb
kubernetes-deploy-0.6.3 lib/kubernetes-deploy/resource_watcher.rb
kubernetes-deploy-0.6.2 lib/kubernetes-deploy/resource_watcher.rb
kubernetes-deploy-0.6.1 lib/kubernetes-deploy/resource_watcher.rb
kubernetes-deploy-0.6.0 lib/kubernetes-deploy/resource_watcher.rb
kubernetes-deploy-0.5.0 lib/kubernetes-deploy/resource_watcher.rb
kubernetes-deploy-0.4.2 lib/kubernetes-deploy/resource_watcher.rb
kubernetes-deploy-0.4.1 lib/kubernetes-deploy/resource_watcher.rb
kubernetes-deploy-0.4.0 lib/kubernetes-deploy/resource_watcher.rb