Class: Hatchet::HerokuRun

Inherits:
Object
  • Object
show all
Defined in:
lib/hatchet/heroku_run.rb

Overview

Used for running Heroku commands

Example:

run_obj = HerokuRun.new("ruby -v", app: app).call
puts run_obj.output #=> "ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]"
puts run_obj.status.success? #=> true

There’s a bug in specs sometimes where App#run will return an empty value. When that’s detected then the command will be re-run. This can be optionally disabled by setting ‘retry_on_empty: false` if you’re expecting the command to be empty.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, app:, heroku: {}, retry_on_empty: !ENV["HATCHET_DISABLE_EMPTY_RUN_RETRY"],, raw: false, stderr: $stderr) ⇒ HerokuRun

Returns a new instance of HerokuRun.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hatchet/heroku_run.rb', line 53

def initialize(
  command,
  app: ,
  heroku: {},
  retry_on_empty: !ENV["HATCHET_DISABLE_EMPTY_RUN_RETRY"],
  raw: false,
  stderr: $stderr)

  @raw = raw
  @app = app
  @command = build_heroku_command(command, heroku || {})
  @retry_on_empty = retry_on_empty
  @stderr = stderr
  @result = nil
  @empty_fail_count = 0
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



51
52
53
# File 'lib/hatchet/heroku_run.rb', line 51

def command
  @command
end

Instance Method Details

#callObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hatchet/heroku_run.rb', line 84

def call
  loop do
    execute!

    break unless output.empty?
    break unless @retry_on_empty

    @empty_fail_count += 1

    break if @empty_fail_count >= 3

    message = String.new("Empty output from command #{@command}, retrying the command.")
    message << "\nTo disable pass in `retry_on_empty: false` or set HATCHET_DISABLE_EMPTY_RUN_RETRY=1 globally"
    message << "\nfailed_count: #{@empty_fail_count}"
    message << "\nreleases: #{@app.releases}"
    message << "\n#{caller.join("\n")}"
    @stderr.puts message
  end

  self
end

#outputObject



75
76
77
# File 'lib/hatchet/heroku_run.rb', line 75

def output
  result.stdout
end

#resultObject



70
71
72
73
# File 'lib/hatchet/heroku_run.rb', line 70

def result
  raise "You must run `call` on this object first" unless @result
  @result
end

#statusObject



79
80
81
82
# File 'lib/hatchet/heroku_run.rb', line 79

def status
  result
  @status
end