Class: KubernetesDeploy::ResourceWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/kubernetes-deploy/resource_watcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(resources, logger:, deploy_started_at: Time.now.utc, operation_name: "deploy") ⇒ ResourceWatcher

Returns a new instance of ResourceWatcher.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/kubernetes-deploy/resource_watcher.rb', line 4

def initialize(resources, logger:, deploy_started_at: Time.now.utc, operation_name: "deploy")
  unless resources.is_a?(Enumerable)
    raise ArgumentError, <<~MSG
      ResourceWatcher expects Enumerable collection, got `#{resources.class}` instead
    MSG
  end
  @resources = resources
  @logger = logger
  @deploy_started_at = deploy_started_at
  @operation_name = operation_name
end

Instance Method Details

#run(delay_sync: 3.seconds, reminder_interval: 30.seconds, record_summary: true) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kubernetes-deploy/resource_watcher.rb', line 16

def run(delay_sync: 3.seconds, reminder_interval: 30.seconds, record_summary: true)
  delay_sync_until = last_message_logged_at = Time.now.utc
  remainder = @resources.dup

  while remainder.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

    KubernetesDeploy::Concurrency.split_across_threads(remainder, &:sync)
    new_successes, remainder = remainder.partition(&:deploy_succeeded?)
    new_failures, remainder = remainder.partition(&:deploy_failed?)
    new_timeouts, remainder = remainder.partition(&:deploy_timed_out?)

    if new_successes.present? || new_failures.present? || new_timeouts.present?
      report_what_just_happened(new_successes, new_failures, new_timeouts)
      report_what_is_left(remainder, reminder: false)
      last_message_logged_at = Time.now.utc
    elsif due_for_reminder?(last_message_logged_at, reminder_interval)
      report_what_is_left(remainder, reminder: true)
      last_message_logged_at = Time.now.utc
    end
  end
  record_statuses_for_summary(@resources) if record_summary
end