Class: Azuki::Command::Ps

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

Overview

manage processes (dynos, workers)

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods inherited from Base

#api, #app, #azuki, #initialize, namespace

Methods included from Helpers

#action, #ask, #confirm, #confirm_billing, #confirm_command, #create_git_remote, #deprecate, #display, #display_header, #display_object, #display_row, #display_table, #error, error_with_failure, error_with_failure=, extended, extended_into, #fail, #format_bytes, #format_date, #format_error, #format_with_bang, #get_terminal_environment, #git, #has_git?, #home_directory, #hprint, #hputs, included, included_into, #json_decode, #json_encode, #launchy, #line_formatter, #longest, #output_with_bang, #quantify, #redisplay, #retry_on_exception, #run_command, #running_on_a_mac?, #running_on_windows?, #set_buffer, #shell, #spinner, #status, #string_distance, #styled_array, #styled_error, #styled_hash, #styled_header, #suggestion, #time_ago, #truncate, #with_tty

Constructor Details

This class inherits a constructor from Azuki::Command::Base

Instance Method Details

#dynosObject

ps:dynos [QTY]

DEPRECATED: use ‘azuki ps:scale dynos=N`

scale to QTY web processes

if QTY is not specified, display the number of web processes currently running

Example:

$ azuki ps:dynos 3 Scaling dynos… done, now running 3



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/azuki/command/ps.rb', line 20

def dynos
  # deprecation notice added to v2.21.3 on 03/16/12
  display("~ `azuki ps:dynos QTY` has been deprecated and replaced with `azuki ps:scale dynos=QTY`")

  dynos = shift_argument
  validate_arguments!

  if dynos
    action("Scaling dynos") do
      new_dynos = api.put_dynos(app, dynos).body["dynos"]
      status("now running #{new_dynos}")
    end
  else
    app_data = api.get_app(app).body
    if app_data["stack"] == "cedar"
      raise(Azuki::Command::CommandFailed, "For Cedar apps, use `azuki ps`")
    else
      display("#{app} is running #{quantify("dyno", app_data["dynos"])}")
    end
  end
end

#indexObject

ps

list processes for an app

Example:

$ azuki ps

run: one-off processes

run.1: up for 5m: ‘bash`

web: ‘bundle exec thin start -p $PORT`

web.1: created for 30s



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/azuki/command/ps.rb', line 94

def index
  validate_arguments!
  processes = api.get_ps(app).body

  processes_by_command = Hash.new {|hash,key| hash[key] = []}
  processes.each do |process|
    name    = process["process"].split(".").first
    elapsed = time_ago(Time.now - process['elapsed'])

    if name == "run"
      key  = "run: one-off processes"
      item = "%s: %s %s: `%s`" % [ process["process"], process["state"], elapsed, process["command"] ]
    else
      key  = "#{name}: `#{process["command"]}`"
      item = "%s: %s %s" % [ process["process"], process["state"], elapsed ]
    end

    processes_by_command[key] << item
  end

  processes_by_command.keys.each do |key|
    processes_by_command[key] = processes_by_command[key].sort do |x,y|
      x.match(/\.(\d+):/).captures.first.to_i <=> y.match(/\.(\d+):/).captures.first.to_i
    end
  end

  processes_by_command.keys.sort.each do |key|
    styled_header(key)
    styled_array(processes_by_command[key], :sort => false)
  end
end

#restartObject

ps:restart [PROCESS]

restart an app process

if PROCESS is not specified, restarts all processes on the app

Examples:

$ azuki ps:restart web.1 Restarting web.1 process… done

$ azuki ps:restart web Restarting web processes… done

$ azuki ps:restart Restarting processes… done



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/azuki/command/ps.rb', line 143

def restart
  process = shift_argument
  validate_arguments!

  message, options = case process
  when NilClass
    ["Restarting processes", {}]
  when /.+\..+/
    ps = args.first
    ["Restarting #{ps} process", { :ps => ps }]
  else
    type = args.first
    ["Restarting #{type} processes", { :type => type }]
  end

  action(message) do
    api.post_ps_restart(app, options)
  end
end

#scaleObject

ps:scale PROCESS1=AMOUNT1 [PROCESS2=AMOUNT2 …]

scale processes by the given amount

Examples:

$ azuki ps:scale web=3 worker+1 Scaling web processes… done, now running 3 Scaling worker processes… done, now running 1



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/azuki/command/ps.rb', line 175

def scale
  changes = {}
  args.each do |arg|
    if arg =~ /^([a-zA-Z0-9_]+)([=+-]\d+)$/
      changes[$1] = $2
    end
  end

  if changes.empty?
    error("Usage: azuki ps:scale PROCESS1=AMOUNT1 [PROCESS2=AMOUNT2 ...]\nMust specify PROCESS and AMOUNT to scale.")
  end

  changes.keys.sort.each do |process|
    amount = changes[process]
    action("Scaling #{process} processes") do
      amount.gsub!("=", "")
      new_qty = api.post_ps_scale(app, process, amount).body
      status("now running #{new_qty}")
    end
  end
end

#stopObject

ps:stop PROCESS

stop an app process

Examples:

$ azuki stop run.3 Stopping run.3 process… done

$ azuki stop run Stopping run processes… done



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/azuki/command/ps.rb', line 211

def stop
  process = shift_argument
  validate_arguments!

  message, options = case process
  when NilClass
    error("Usage: azuki ps:stop PROCESS\nMust specify PROCESS to stop.")
  when /.+\..+/
    ps = args.first
    ["Stopping #{ps} process", { :ps => ps }]
  else
    type = args.first
    ["Stopping #{type} processes", { :type => type }]
  end

  action(message) do
    api.post_ps_stop(app, options)
  end
end

#workersObject

ps:workers [QTY]

DEPRECATED: use ‘azuki ps:scale workers=N`

scale to QTY background processes

if QTY is not specified, display the number of background processes currently running

Example:

$ azuki ps:dynos 3 Scaling workers… done, now running 3



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/azuki/command/ps.rb', line 57

def workers
  # deprecation notice added to v2.21.3 on 03/16/12
  display("~ `azuki ps:workers QTY` has been deprecated and replaced with `azuki ps:scale workers=QTY`")

  workers = shift_argument
  validate_arguments!

  if workers
    action("Scaling workers") do
      new_workers = api.put_workers(app, workers).body["workers"]
      status("now running #{new_workers}")
    end
  else
    app_data = api.get_app(app).body
    if app_data["stack"] == "cedar"
      raise(Azuki::Command::CommandFailed, "For Cedar apps, use `azuki ps`")
    else
      display("#{app} is running #{quantify("worker", app_data["workers"])}")
    end
  end
end