class FruityBuilder::Execution
Provides method to execute terminal commands in a reusable way
Constants
- COMMAND_RETRIES
- COMMAND_TIMEOUT
execute_with_timeout_and_retry
constants
Public Class Methods
execute(command)
click to toggle source
(1 * s) + (2 * s) + (3 * s) … up to (n * s) where s = COMMAND_TIMEOUT
n = COMMAND_RETRIES
# File lib/fruity_builder/lib/execution.rb, line 19 def self.execute(command) # Execute out to shell # Returns a struct collecting the execution results # struct = DeviceAPI::ADB.execute( 'adb devices' ) # struct.stdout #=> "std out" # struct.stderr #=> '' # strict.exit #=> 0 result = OpenStruct.new stdout, stderr, status = Open3.capture3(command) result.exit = status.exitstatus result.stdout = stdout result.stderr = stderr result end
execute_with_timeout_and_retry(command)
click to toggle source
# File lib/fruity_builder/lib/execution.rb, line 37 def self.execute_with_timeout_and_retry(command) retries_left = COMMAND_RETRIES cmd_successful = false result = 0 while (retries_left > 0) and (cmd_successful == false) do begin ::Timeout.timeout(COMMAND_TIMEOUT) do result = execute(command) cmd_successful = true end rescue ::Timeout::Error retries_left -= 1 if retries_left > 0 FruityBuilder.log.error "Command #{command} timed out after #{COMMAND_TIMEOUT.to_s} sec, retrying,"\ + " #{retries_left.to_s} attempts left.." end end end if retries_left < COMMAND_RETRIES # if we had to retry if cmd_successful == false msg = "Command #{command} timed out after #{COMMAND_RETRIES.to_s} retries. !"\ + " Exiting.." FruityBuilder.log.fatal(msg) raise FruityBuilder::CommandTimeoutError.new(msg) else FruityBuilder.log.info "Command #{command} succeeded execution after retrying" end end result end