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,
  use_local_token_option
].freeze
DESCRIPTION =
"Runs one-off **_non-interactive_** replicas (close analog of `heroku run:detached`)"
LONG_DESCRIPTION =
"- Runs one-off **_non-interactive_** replicas (close analog of `heroku run:detached`)\n- Uses `Cron` workload type with log async fetching\n- Implemented with only async execution methods, more suitable for production tasks\n- Has alternative log fetch implementation with only JSON-polling and no WebSockets\n- Less responsive but more stable, useful for CI tasks\n"
EXAMPLES =
"```sh\ncpl run:detached rails db:prepare -a $APP_NAME\n\n# Need to quote COMMAND if setting ENV value or passing args.\ncpl run:detached 'LOG_LEVEL=warn rails db:migrate' -a $APP_NAME\n\n# COMMAND may also be passed at the end.\ncpl run:detached -a $APP_NAME -- 'LOG_LEVEL=warn rails db:migrate'\n\n# Uses a different image (which may not be promoted yet).\ncpl run:detached rails db:migrate -a $APP_NAME --image appimage:123 # Exact image name\ncpl run:detached rails db:migrate -a $APP_NAME --image latest       # Latest sequential image\n\n# Uses a different workload than `one_off_workload` from `.controlplane/controlplane.yml`.\ncpl run:detached rails db:migrate:status -a $APP_NAME -w other-workload\n\n# Overrides remote CPLN_TOKEN env variable with local token.\n# Useful when superuser rights are needed in remote container.\ncpl run:detached rails db:migrate:status -a $APP_NAME --use-local-token\n```\n"
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, #extract_image_commit, 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.



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

def container
  @container
end

#locationObject (readonly)

Returns the value of attribute location.



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

def location
  @location
end

#one_offObject (readonly)

Returns the value of attribute one_off.



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

def one_off
  @one_off
end

#workloadObject (readonly)

Returns the value of attribute workload.



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

def workload
  @workload
end

Instance Method Details

#callObject

rubocop:disable Metrics/MethodLength



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

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