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,
  location_option,
  use_local_token_option,
  clean_on_failure_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
  - Deletes the workload whenever finished with success
  - Deletes the workload whenever finished with failure by default
  - Use `--no-clean-on-failure` to disable cleanup to help with debugging failed runs
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 -a $APP_NAME -- 'LOG_LEVEL=warn rails db:migrate'

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

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

  # Overrides remote CPLN_TOKEN env variable with local token.
  # Useful when superuser rights are needed in remote container.
  cpl run:detached -a $APP_NAME --use-local-token -- rails db:migrate:status
  ```
EX
WORKLOAD_SLEEP_CHECK =
2

Constants inherited from Base

Base::ACCEPTS_EXTRA_OPTIONS, Base::DEFAULT_ARGS, Base::HIDE, Base::NO_IMAGE_AVAILABLE, Base::WITH_INFO_HEADER

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_identity, #app_identity_link, #app_image_link, #app_location_link, app_option, #app_secrets, #app_secrets_policy, #args_join, clean_on_failure_option, commit_option, common_options, #cp, domain_option, #ensure_workload_deleted, #extract_image_commit, image_option, #initialize, #latest_image, #latest_image_from, #latest_image_next, location_option, org_option, #perform!, #progress, run_release_phase_option, skip_confirm_option, skip_secret_access_binding_option, #step, #step_error, #step_finish, terminal_size_option, trace_option, upstream_token_option, use_local_token_option, verbose_option, version_option, #wait_for_replica, #wait_for_workload, wait_option, workload_option

Methods included from Helpers

#random_four_digits, #strip_str_and_validate

Constructor Details

This class inherits a constructor from Command::Base

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



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

def container
  @container
end

#locationObject (readonly)

Returns the value of attribute location.



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

def location
  @location
end

#workload_cloneObject (readonly)

Returns the value of attribute workload_clone.



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

def workload_clone
  @workload_clone
end

#workload_to_cloneObject (readonly)

Returns the value of attribute workload_to_clone.



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

def workload_to_clone
  @workload_to_clone
end

Instance Method Details

#callObject



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

def call
  @location = config.location
  @workload_to_clone = config.options["workload"] || config[:one_off_workload]
  @workload_clone = "#{workload_to_clone}-runner-#{random_four_digits}"

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

  wait_for_workload(workload_clone)
  show_logs_waiting
ensure
  exit(1) if @crashed
end