Class: Ufo::CLI::Ps::Task

Inherits:
Base
  • Object
show all
Defined in:
lib/ufo/cli/ps/task.rb

Instance Attribute Summary

Attributes inherited from Base

#task_definition

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Sure

#sure?

Methods included from Utils::Pretty

#pretty_home, #pretty_path, #pretty_time

Methods included from Utils::Logging

#logger

Methods included from Ufo::Concerns

#build, #deploy, #info, #ps

Methods included from Ufo::Concerns::Names

#names

Methods included from AwsServices

#acm, #applicationautoscaling, #aws_options, #cloudformation, #cloudwatchlogs, #ec2, #ecr, #ecs, #elb, #find_stack, #ssm_client, #stack_resources, #task_definition_arns

Constructor Details

#initialize(options = {}) ⇒ Task

Returns a new instance of Task.



3
4
5
6
# File 'lib/ufo/cli/ps/task.rb', line 3

def initialize(options={})
  super
  @task = options[:task] # task response from ecs.list_tasks
end

Class Method Details

.headerObject



98
99
100
# File 'lib/ufo/cli/ps/task.rb', line 98

def header
  %w[Task Name Release Started Status Notes]
end

Instance Method Details

#container_instance_arnObject



26
27
28
# File 'lib/ufo/cli/ps/task.rb', line 26

def container_instance_arn
  @task['container_instance_arn'].split('/').last
end

#hide?Boolean

hide stopped tasks have been stopped for more than 5 minutes

created_at=2018-07-05 21:52:13 -0700,
started_at=2018-07-05 21:52:15 -0700,
stopping_at=2018-07-05 22:03:44 -0700,
stopped_at=2018-07-05 22:03:45 -0700,

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
# File 'lib/ufo/cli/ps/task.rb', line 51

def hide?
  return false if @options[:status] == "stopped"
  # Went back and forth with stopped_at vs started_at
  # Seems like stopped_at is better as when ECS is trying to scale it leaves a lot of tasks
  stopped_at = time(@task["stopped_at"])
  return false unless stopped_at
  time = Time.now - 60 * Ufo.config.ps.hide_age
  status == "STOPPED" && stopped_at < time
end

#idObject



13
14
15
# File 'lib/ufo/cli/ps/task.rb', line 13

def id
  @task['task_arn'].split('/').last.split('-').first
end

#nameObject



17
18
19
20
21
22
23
24
# File 'lib/ufo/cli/ps/task.rb', line 17

def name
  container_overrides = @task.dig("overrides", "container_overrides")
  overrides = container_overrides.first # assume first is one we want
  overrides["name"] if overrides # PENDING wont yet have info
rescue NoMethodError
  container = @task["containers"].first
  container["name"] if container # PENDING wont yet have info
end

#notesObject

Grab all the reasons and surface to user. Even though will make the table output ugly, debugging info merits it.

ufo ps --format json


70
71
72
73
74
75
76
77
78
# File 'lib/ufo/cli/ps/task.rb', line 70

def notes
  return unless @task["stopped_reason"]
  notes = []
  notes << "Task Stopped Reason: #{@task["stopped_reason"]}."
  @task.containers.each do |container|
    notes << "Container #{container.name} reason: #{container.reason}" unless container.reason.blank?
  end
  notes.join(" ")
end

#relative_time(start_time) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ufo/cli/ps/task.rb', line 81

def relative_time(start_time)
  diff_seconds = Time.now - start_time
  case diff_seconds
    when 0 .. 59
      "#{diff_seconds.to_i} seconds ago"
    when 60 .. (3600-1)
      "#{(diff_seconds/60).to_i} minutes ago"
    when 3600 .. (3600*24-1)
      "#{(diff_seconds/3600).to_i} hours ago"
    when (3600*24) .. (3600*24*30)
      "#{(diff_seconds/(3600*24)).to_i} days ago"
    else
      start_time.strftime("%m/%d/%Y")
  end
end

#releaseObject



30
31
32
# File 'lib/ufo/cli/ps/task.rb', line 30

def release
  @task["task_definition_arn"].split('/').last
end

#startedObject



34
35
36
37
38
# File 'lib/ufo/cli/ps/task.rb', line 34

def started
  started = time(@task["started_at"])
  return "PENDING" unless started
  relative_time(started)
end

#statusObject



61
62
63
# File 'lib/ufo/cli/ps/task.rb', line 61

def status
  @task["last_status"]
end

#time(value) ⇒ Object



40
41
42
43
44
# File 'lib/ufo/cli/ps/task.rb', line 40

def time(value)
  Time.parse(value.to_s)
rescue ArgumentError
  nil
end

#to_aObject



8
9
10
11
# File 'lib/ufo/cli/ps/task.rb', line 8

def to_a
  row = [id, name, release, started, status, notes]
  row
end