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, run: true, &b) ⇒ Object

Note:

The ‘run’ keyword argument is nice if you have some big pipeline and you need to temporarily prevent sections of the code from running.

Run a command and time it as well!

Examples:


Process.extend Rya::CoreExtensions::Process

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

Process.run_and_time_it! "This will not run",
                         "echo 'hey'",
                         run: false

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

Parameters:

  • title (defaults to: "")

    Give your command a snappy title to log!

  • cmd (defaults to: "")

    The actual command you want to run

  • logger (defaults to: Rya::AbortIf::logger)

    Something that responds to #info for printing. If nil, just print to STDERR.

  • run (defaults to: true)

    If true, actually run the command, if false, then don’t.



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/rya/core_extensions.rb', line 243

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

  time_it title, logger, run: run do
    Rya::AbortIf.logger.debug { "Running: #{cmd}" }

    run_it! cmd, &b
  end
end

#run_it(*a, &b) ⇒ Object

Runs a command and outputs stdout and stderr



197
198
199
200
201
202
203
204
# File 'lib/rya/core_extensions.rb', line 197

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.



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rya/core_extensions.rb', line 207

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



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rya/core_extensions.rb', line 179

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