Class: Command::RunDetached

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

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

NAME =
"run:detached"
USAGE =
"run:detached COMMAND"
REQUIRES_ARGS =
true
OPTIONS =
[
  app_option(required: true),
  image_option,
  workload_option
].freeze
DESCRIPTION =
"Runs one-off **_non-interactive_** replicas (close analog of `heroku run:detached`)"
LONG_DESCRIPTION =
<<~DESC
  - Runs one-off **_non-interactive_** replicas (close analog of `heroku run:detached`)
  - Uses `Cron` workload type with log async fetching
  - Implemented with only async execution methods, more suitable for production tasks
  - Has alternative log fetch implementation with only JSON-polling and no WebSockets
  - Less responsive but more stable, useful for CI tasks
DESC
EXAMPLES =
<<~EX
  ```sh
  cpl run:detached rails db:prepare -a $APP_NAME

  # Need to quote COMMAND if setting ENV value or passing args.
  cpl run:detached 'LOG_LEVEL=warn rails db:migrate' -a $APP_NAME

  # COMMAND may also be passed at the end (in this case, no need to quote).
  cpl run:detached -a $APP_NAME -- rails db:migrate

  # Uses some other image.
  cpl run:detached rails db:migrate -a $APP_NAME --image /some/full/image/path

  # Uses latest app image (which may not be promoted yet).
  cpl run:detached rails db:migrate -a $APP_NAME --image latest

  # Uses a different image (which may not be promoted yet).
  cpl run:detached rails db:migrate -a $APP_NAME --image appimage:123 # Exact image name
  cpl run:detached rails db:migrate -a $APP_NAME --image latest       # Latest sequential image

  # Uses a different workload than `one_off_workload` from `.controlplane/controlplane.yml`.
  cpl run:detached rails db:migrate:status -a $APP_NAME -w other-workload
  ```
EX
WORKLOAD_SLEEP_CHECK =
2

Constants inherited from Base

Base::DEFAULT_ARGS, Base::HIDE, Base::NO_IMAGE_AVAILABLE

Instance Attribute Summary collapse

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

all_commands, all_options, all_options_by_key_name, app_option, #args_join, commit_option, #cp, #ensure_workload_deleted, image_option, #initialize, #latest_image, #latest_image_from, #latest_image_next, org_option, #perform, #progress, skip_confirm_option, #step, #step_error, #step_finish, terminal_size_option, upstream_token_option, use_local_token_option, version_option, #wait_for_replica, #wait_for_workload, wait_option, workload_option

Constructor Details

This class inherits a constructor from Command::Base

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



48
49
50
# File 'lib/command/run_detached.rb', line 48

def container
  @container
end

#locationObject (readonly)

Returns the value of attribute location.



48
49
50
# File 'lib/command/run_detached.rb', line 48

def location
  @location
end

#one_offObject (readonly)

Returns the value of attribute one_off.



48
49
50
# File 'lib/command/run_detached.rb', line 48

def one_off
  @one_off
end

#workloadObject (readonly)

Returns the value of attribute workload.



48
49
50
# File 'lib/command/run_detached.rb', line 48

def workload
  @workload
end

Instance Method Details

#callObject

rubocop:disable Metrics/MethodLength



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/command/run_detached.rb', line 50

def call # rubocop:disable Metrics/MethodLength
  @location = config[:default_location]
  @workload = config.options["workload"] || config[:one_off_workload]
  @one_off = "#{workload}-runner-#{rand(1000..9999)}"

  step("Cloning workload '#{workload}' on app '#{config.options[:app]}' to '#{one_off}'") do
    clone_workload
  end

  wait_for_workload(one_off)
  show_logs_waiting
ensure
  if cp.fetch_workload(one_off)
    progress.puts
    ensure_workload_deleted(one_off)
  end
  exit(1) if @crashed
end