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.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hatchet/heroku_run.rb', line 18

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
  @output = ""
  @status = nil
  @empty_fail_count = 0
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



16
17
18
# File 'lib/hatchet/heroku_run.rb', line 16

def command
  @command
end

Instance Method Details

#callObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hatchet/heroku_run.rb', line 46

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



36
37
38
39
# File 'lib/hatchet/heroku_run.rb', line 36

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

#statusObject



41
42
43
44
# File 'lib/hatchet/heroku_run.rb', line 41

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