Class: Krane::ResourceWatcher

Inherits:
Object
  • Object
show all
Extended by:
StatsD::MeasureMethods
Defined in:
lib/krane/resource_watcher.rb

Instance Method Summary collapse

Methods included from StatsD::MeasureMethods

measure_method

Constructor Details

#initialize(resources:, task_config:, deploy_started_at: Time.now.utc, operation_name: "deploy", timeout: nil, sha: nil) ⇒ ResourceWatcher

Returns a new instance of ResourceWatcher.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/krane/resource_watcher.rb', line 11

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

Instance Method Details

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/krane/resource_watcher.rb', line 26

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

  while remainder.present?
    report_and_give_up(remainder) if global_timeout?(monitoring_started)
    sleep_until_next_sync(delay_sync)

    sync_resources(remainder)

    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