Module: Rya::CoreExtensions::Process

Includes:
Time
Defined in:
lib/rya/core_extensions.rb

Instance Method Summary collapse

Methods included from Time

#date_and_time, #time_it

Instance Method Details

#run_and_time_it!(title = "", cmd = "", logger = Rya::AbortIf::logger, &b) ⇒ Object

Run a command and time it as well!

Examples:


Process.extend Rya::CoreExtensions::Process
Time.extend Rya::CoreExtensions::Time

Process.run_and_time_it! "Saying hello",
                         %Q{echo "hello world"}

Process.run_and_time_it! "This will raise SystemExit",
                         "ls arstoeiarntoairnt"


230
231
232
233
234
235
236
237
238
239
240
# File 'lib/rya/core_extensions.rb', line 230

def run_and_time_it! title = "",
                     cmd = "",
                     logger = Rya::AbortIf::logger,
                     &b

  Rya::AbortIf.logger.debug { "Running: #{cmd}" }

  time_it title, logger do
    run_it! cmd, &b
  end
end

#run_it(*a, &b) ⇒ Object

Runs a command and outputs stdout and stderr



194
195
196
197
198
199
200
201
# File 'lib/rya/core_extensions.rb', line 194

def run_it *a, &b
  exit_status, stdout, stderr = systemu *a, &b

  puts stdout unless stdout.empty?
  STDERR.puts stderr unless stderr.empty?

  exit_status
end

#run_it!(*a, &b) ⇒ Object

Like run_it() but will raise Rya::AbortIf::Exit on non-zero exit status.



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/rya/core_extensions.rb', line 204

def run_it! *a, &b
  exit_status = self.run_it *a, &b

  # Sometimes, exited? is not true and there will be no exit
  # status. Success should catch all failures.
  Rya::AbortIf.abort_unless exit_status.success?,
                            "Command failed with status " \
                                "'#{exit_status.to_s}' " \
                                "when running '#{a.inspect}', " \
                                "'#{b.inspect}'"

  exit_status
end

#run_until_success(max_attempts) { ... } ⇒ Integer

I run your program until it succeeds or I fail too many times.

Examples:

I’ll keep retrying your command line program until it succeeds.

klass = Class.new { extend Rya::CoreExtensions::Process }
max_attempts = 10

tries = klass.run_until_success max_attempts do
  # This command returns a Process::Status object!
  klass.run_it "echo 'hi'"
end

tries == 1 #=> true

I’ll raise an error if the program doesn’t succeed after max_attempts tries.

klass = Class.new { extend Rya::CoreExtensions::Process }
max_attempts = 10

begin
  klass.run_until_success max_attempts do
    # This command returns a Process::Status object!
    klass.run_it "ls 'file_that_doesnt_exist'"
  end
rescue Rya::MaxAttemptsExceededError => err
  STDERR.puts "The command didn't succeed after #{max_attempts} tries!"
end

Parameters:

  • max_attempts (Integer)

    max attempts before I fail

Yields:

  • The block specifies the command you want to run. Make sure that it returns something that responds to exitstatus!

Returns:

  • (Integer)

    the number of attempts before successful completion

Raises:

  • (Rya::Error)

    if the block does not return an object that responds to exitstatus (e.g., Prosses::Status)

  • (Rya::MaxAttemptsExceededError)

    if the program fails more than max_attempts times



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/rya/core_extensions.rb', line 176

def run_until_success max_attempts, &block
  max_attempts.times do |attempt_index|
    proc_status = yield block

    unless proc_status.respond_to? :exitstatus
      raise Rya::Error, "The block did not return an object that responds to exitstatus"
    end

    if proc_status.exitstatus.zero?
      return attempt_index + 1
    end
  end

  raise Rya::MaxAttemptsExceededError, "max_attempts exceeded"
end