Class: ElasticBeans::Command::Ps

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_beans/command/ps.rb

Overview

:nodoc: all

Constant Summary collapse

USAGE =
"ps"
DESC =
"List scheduled and running one-off commands run using `beans exec`"
LONG_DESC =
<<-LONG_DESC
List scheduled and running one-off commands run using `beans exec`.
See `beans help exec` for more information about running one-off commands.

Requires AWS credentials to be set in the environment, i.e. AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
LONG_DESC
COLUMNS =
{
  "COMMAND ID" => ->(command) { command.id },
  "INSTANCE" => ->(command) { command.instance_id || SCHEDULED_INSTANCE },
  "RUN TIME" => ->(command) {
    if command.start_time
      seconds = Time.now - command.start_time
      hours, seconds = seconds / 3600, seconds % 3600
      minutes, seconds = seconds / 60, seconds % 60
      sprintf("%02d:%02d:%02d", hours, minutes, seconds)
    else
      SCHEDULED_RUN_TIME
    end
  },
  "COMMAND" => ->(command) { command.command_string },
}
SCHEDULED_INSTANCE =
"scheduled"
SCHEDULED_RUN_TIME =
"00:00:00"

Instance Method Summary collapse

Constructor Details

#initialize(application:, show_all:, ui:) ⇒ Ps

Returns a new instance of Ps.



32
33
34
35
36
# File 'lib/elastic_beans/command/ps.rb', line 32

def initialize(application:, show_all:, ui:)
  @application = application
  @show_all = show_all
  @ui = ui
end

Instance Method Details

#runObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/elastic_beans/command/ps.rb', line 38

def run
  ui.debug { "Fetching enqueued commands from #{application.name}..." }
  enqueued_commands = application.enqueued_commands
  if !show_all?
    enqueued_commands.reject!(&:freeze_instance?)
  end

  enqueued_commands, running_commands = enqueued_commands.partition { |cmd| cmd.start_time.nil? }
  running_commands.sort_by!(&:start_time)
  commands = running_commands + enqueued_commands
  if commands.any?
    ui.table(level: :info, columns: COLUMNS, rows: commands)
  else
    ui.info("No commands enqueued or running")
  end
end