Class: Heroku::Command::Ps

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

Overview

manage dynos (dynos, workers)

Constant Summary collapse

PRICES =
{
  "P"  => 0.8,
  "PX" => 0.8,
}

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods inherited from Base

#api, #app, #heroku, #initialize, namespace, #org, #org_api

Methods included from Helpers

#action, #app_owner, #ask, #confirm, #confirm_command, #create_git_remote, #debug, #debugging?, #default_org_host, #deprecate, #display, #display_header, #display_object, #display_row, #display_table, #error, #error_log, #error_log_path, 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?, #has_git_remote?, #has_http_git_entry_in_netrc, #home_directory, #hprint, #hputs, included, included_into, #json_decode, #json_encode, #launchy, #line_formatter, #longest, #org?, #org_host, #output_with_bang, #quantify, #redisplay, #retry_on_exception, #run_command, #running_on_a_mac?, #running_on_windows?, #set_buffer, #shell, #spinner, #status, #stderr_print, #stderr_puts, #string_distance, #styled_array, #styled_error, #styled_hash, #styled_header, #suggestion, #time_ago, #truncate, #update_git_remote, #warn_if_using_jruby, #with_tty

Constructor Details

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

Instance Method Details

#dynosObject

ps:dynos [QTY]

DEPRECATED: use `heroku ps:scale dynos=N`

scale to QTY web processes

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

Example:

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


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/heroku/command/ps.rb', line 24

def dynos
  # deprecation notice added to v2.21.3 on 03/16/12
  display("~ `heroku ps:dynos QTY` has been deprecated and replaced with `heroku 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(Heroku::Command::CommandFailed, "For Cedar apps, use `heroku ps`")
    else
      display("#{app} is running #{quantify("dyno", app_data["dynos"])}")
    end
  end
end

#indexObject

ps

list dynos for an app

Example:

$ heroku ps

run: one-off dyno

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

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

web.1: created for 30s


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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/heroku/command/ps.rb', line 98

def index
  validate_arguments!
  resp = api.request(
    :expects => 200,
    :method  => :get,
    :path    => "/apps/#{app}/dynos",
    :headers => {
      "Accept"       => "application/vnd.heroku+json; version=3",
      "Content-Type" => "application/json"
    }
  )
  processes = resp.body

  processes_by_command = Hash.new {|hash,key| hash[key] = []}
  processes.each do |process|
    now     = Time.now
    type    = process["type"]
    elapsed = now - Time.iso8601(process['updated_at'])
    since   = time_ago(now - elapsed)
    size    = process["size"] || "1X"

    if type == "run"
      key  = "run: one-off processes"
      item = "%s (%s): %s %s: `%s`" % [ process["name"], size, process["state"], since, process["command"] ]
    else
      key  = "#{type} (#{size}): `#{process["command"]}`"
      item = "%s: %s %s" % [ process['name'], process['state'], since ]
    end

    processes_by_command[key] << item
  end

  extract_run_id = /\.(\d+).*:/
  processes_by_command.keys.each do |key|
    processes_by_command[key] = processes_by_command[key].sort do |x,y|
      x.match(extract_run_id).captures.first.to_i <=> y.match(extract_run_id).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

#resizeObject

ps:resize DYNO1=1X|2X|PX [DYNO2=1X|2X|PX …]

resize dynos to the given size

Example:

$ heroku ps:resize web=PX worker=2X Resizing and restarting the specified dynos… done web dynos now PX ($0.80/dyno-hour) worker dynos now 2X ($0.10/dyno-hour)


276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/heroku/command/ps.rb', line 276

def resize
  app
  change_map = {}

  changes = args.map do |arg|
    if arg =~ /^([a-zA-Z0-9_]+)=(\w+)$/
      change_map[$1] = $2
      { "process" => $1, "size" => $2 }
    end
  end.compact

  if changes.empty?
    message = [
        "Usage: heroku ps:resize DYNO1=1X|2X|PX [DYNO2=1X|2X|PX ...]",
        "Must specify DYNO and SIZE to resize."
    ]
    error(message.join("\n"))
  end

  resp = nil
  action("Resizing and restarting the specified dynos") do
    resp = api.request(
      :expects => 200,
      :method  => :patch,
      :path    => "/apps/#{app}/formation",
      :body    => json_encode("updates" => changes),
      :headers => {
        "Accept"       => "application/vnd.heroku+json; version=3",
        "Content-Type" => "application/json"
      }
    )
  end

  resp.body.select {|p| change_map.key?(p['type']) }.each do |p|
    size = p["size"]
    price = if size.to_i > 0
              sprintf("%.2f", 0.05 * size.to_i)
            else
              sprintf("%.2f", PRICES[size])
            end
    display "#{p["type"]} dynos now #{size} ($#{price}/dyno-hour)"
  end
end

#restartObject

ps:restart [DYNO]

restart an app dyno

if DYNO is not specified, restarts all dynos on the app

Examples:

$ heroku ps:restart web.1 Restarting web.1 dyno… done

$ heroku ps:restart web Restarting web dyno… done

$ heroku ps:restart Restarting dynos… done


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/heroku/command/ps.rb', line 160

def restart
  dyno = shift_argument
  validate_arguments!

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

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

#scaleObject

ps:scale DYNO1=AMOUNT1 [DYNO2=AMOUNT2 …]

scale dynos by the given amount

appending a size (eg. web=2:2X) allows simultaneous scaling and resizing

Examples:

$ heroku ps:scale web=3:2X worker+1 Scaling dynos… done, now running web at 3:2X, worker at 1:1X.


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/heroku/command/ps.rb', line 193

def scale
  change_map = {}

  changes = args.map do |arg|
    if change = arg.scan(/^([a-zA-Z0-9_]+)([=+-]\d+)(?::(\w+))?$/).first
      formation, quantity, size = change
      quantity.gsub!("=", "") # only allow + and - on quantity
      change_map[formation] = [quantity, size]
      { "process" => formation, "quantity" => quantity, "size" => size}
    end
  end.compact

  if changes.empty?
    error("Usage: heroku ps:scale DYNO1=AMOUNT1[:SIZE] [DYNO2=AMOUNT2 ...]\nMust specify DYNO and AMOUNT to scale.")
  end

  action("Scaling dynos") do
    # The V3 API supports atomic scale+resize, so we make a raw request here
    # since the heroku-api gem still only supports V2.
    resp = api.request(
      :expects => 200,
      :method  => :patch,
      :path    => "/apps/#{app}/formation",
      :body    => json_encode("updates" => changes),
      :headers => {
        "Accept"       => "application/vnd.heroku+json; version=3",
        "Content-Type" => "application/json"
      }
    )
    new_scales = resp.body.
      select {|p| change_map[p['type']] }.
      map {|p| "#{p["type"]} at #{p["quantity"]}:#{p["size"]}" }
    status("now running " + new_scales.join(", ") + ".")
  end
end

#stopObject

ps:stop DYNOS

stop an app dyno

Examples:

$ heroku stop run.3 Stopping run.3 dyno… done

$ heroku stop run Stopping run dynos… done


243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/heroku/command/ps.rb', line 243

def stop
  dyno = shift_argument
  validate_arguments!

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

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

#workersObject

ps:workers [QTY]

DEPRECATED: use `heroku ps:scale workers=N`

scale to QTY background processes

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

Example:

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


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/heroku/command/ps.rb', line 61

def workers
  # deprecation notice added to v2.21.3 on 03/16/12
  display("~ `heroku ps:workers QTY` has been deprecated and replaced with `heroku 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(Heroku::Command::CommandFailed, "For Cedar apps, use `heroku ps`")
    else
      display("#{app} is running #{quantify("worker", app_data["workers"])}")
    end
  end
end