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) ⇒ ResourceWatcher

Returns a new instance of ResourceWatcher.



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

def initialize(resources, logger:, deploy_started_at: Time.now.utc)
  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
end

Instance Method Details

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



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

def run(delay_sync: 3.seconds, reminder_interval: 30.seconds)
  delay_sync_until = last_message_logged_at = Time.now.utc

  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

    KubernetesDeploy::Concurrency.split_across_threads(@resources, &:sync)
    newly_finished_resources, @resources = @resources.partition(&:deploy_finished?)

    if newly_finished_resources.present?
      watch_time = (Time.now.utc - @deploy_started_at).round(1)
      report_what_just_happened(newly_finished_resources, watch_time)
      report_what_is_left(reminder: false)
      last_message_logged_at = Time.now.utc
    elsif due_for_reminder?(last_message_logged_at, reminder_interval)
      report_what_is_left(reminder: true)
      last_message_logged_at = Time.now.utc
    end
  end
end