IRunUtil

public interface IRunUtil

com.android.tradefed.util.IRunUtil
Known Indirect Subclasses
RunUtil A collection of helper methods for executing operations. 


Interface for running timed operations and system commands.

Summary

Nested classes

enum IRunUtil.EnvPriority

Enum that defines whether setting or unsetting a particular env. 

interface IRunUtil.IRunnableResult

An interface for asynchronously executing an operation that returns a boolean status. 

Public methods

abstract void allowInterrupt(boolean allow)

Allows/disallows run interrupts on the current thread.

abstract void interrupt(Thread thread, String message)

Interrupts the ongoing/forthcoming run operations on the given thread.

abstract boolean isInterruptAllowed()

Give the interrupt status of the RunUtil.

abstract Process runCmdInBackground(List<String> command, OutputStream output)

Running command with a OutputStream log the output of the command.

abstract Process runCmdInBackground(String... command)

Helper method to execute a system command asynchronously.

abstract Process runCmdInBackground(List<String> command)

An alternate runCmdInBackground(String) method that accepts the command arguments in List form.

abstract boolean runEscalatingTimedRetry(long opTimeout, long initialPollInterval, long maxPollInterval, long maxTime, IRunUtil.IRunnableResult runnable)

Block and executes an operation multiple times until it is successful.

abstract boolean runFixedTimedRetry(long opTimeout, long pollInterval, long maxTime, IRunUtil.IRunnableResult runnable)

Block and executes an operation multiple times until it is successful.

abstract CommandStatus runTimed(long timeout, IRunUtil.IRunnableResult runnable, boolean logErrors)

Block and executes an operation, aborting if it takes longer than a specified time.

abstract CommandResult runTimedCmd(long timeout, String... command)

Helper method to execute a system command, and aborting if it takes longer than a specified time.

abstract CommandResult runTimedCmd(long timeout, OutputStream stdout, OutputStream stderr, String... command)

Helper method to execute a system command, abort if it takes longer than a specified time, and redirect output to files if specified.

abstract CommandResult runTimedCmdRetry(long timeout, long retryInterval, int attempts, String... command)

Helper method to execute a system command, and aborting if it takes longer than a specified time.

abstract CommandResult runTimedCmdSilently(long timeout, String... command)

Helper method to execute a system command, and aborting if it takes longer than a specified time.

abstract CommandResult runTimedCmdSilentlyRetry(long timeout, long retryInterval, int attempts, String... command)

Helper method to execute a system command, and aborting if it takes longer than a specified time.

abstract CommandResult runTimedCmdWithInput(long timeout, String input, List<String> command)

Helper method to execute a system command that requires stdin input, and aborting if it takes longer than a specified time.

abstract CommandResult runTimedCmdWithInput(long timeout, String input, String... command)

Helper method to execute a system command that requires stdin input, and aborting if it takes longer than a specified time.

abstract boolean runTimedRetry(long opTimeout, long pollInterval, int attempts, IRunUtil.IRunnableResult runnable)

Block and executes an operation multiple times until it is successful.

abstract void setEnvVariable(String key, String value)

Sets a environment variable to be used when running system commands.

abstract void setEnvVariablePriority(IRunUtil.EnvPriority priority)

Decide whether or not when creating a process, unsetting environment variable is higher priority than setting them.

abstract void setInterruptibleInFuture(Thread thread, long timeMs)

Set as interruptible after some waiting time.

abstract void setWorkingDir(File dir)

Sets the working directory for system commands.

abstract void sleep(long time)

Helper method to sleep for given time, ignoring any exceptions.

abstract void unsetEnvVariable(String key)

Unsets an environment variable, so the system commands run without this environment variable.

Public methods

allowInterrupt

void allowInterrupt (boolean allow)

Allows/disallows run interrupts on the current thread. If it is allowed, run operations of the current thread can be interrupted from other threads via interrupt(Thread, String) method.

Parameters
allow boolean: whether to allow run interrupts on the current thread.

interrupt

void interrupt (Thread thread, 
                String message)

Interrupts the ongoing/forthcoming run operations on the given thread. The run operations on the given thread will throw RunInterruptedException.

Parameters
message String: the message for RunInterruptedException.

isInterruptAllowed

boolean isInterruptAllowed ()

Give the interrupt status of the RunUtil.

Returns
boolean true if the Run can be interrupted, false otherwise.

runCmdInBackground

Process runCmdInBackground (List<String> command, 
                OutputStream output)

Running command with a OutputStream log the output of the command. Stdout and stderr are merged together.

Parameters
command List: the command to run

output OutputStream: the OutputStream to save the output

Returns
Process the Process running the command

Throws
IOException

runCmdInBackground

Process runCmdInBackground (String... command)

Helper method to execute a system command asynchronously.

Will return immediately after launching command.

Parameters
command String: the specified system command and optionally arguments to exec

Returns
Process the Process of the executed command

Throws
IOException if command failed to run

runCmdInBackground

Process runCmdInBackground (List<String> command)

An alternate runCmdInBackground(String) method that accepts the command arguments in List form.

Parameters
command List: the List containing specified system command and optionally arguments to exec

Returns
Process the Process of the executed command

Throws
IOException if command failed to run

runEscalatingTimedRetry

boolean runEscalatingTimedRetry (long opTimeout, 
                long initialPollInterval, 
                long maxPollInterval, 
                long maxTime, 
                IRunUtil.IRunnableResult runnable)

Block and executes an operation multiple times until it is successful.

Exponentially increase the wait time between operation attempts. This is intended to be used when performing an operation such as polling a server, to give it time to recover in case it is temporarily down.

Parameters
opTimeout long: maximum time to wait in ms for a single operation attempt

initialPollInterval long: initial time to wait between operation attempts

maxPollInterval long: the max time to wait between operation attempts

maxTime long: the total approximate maximum time to keep trying the operation

runnable IRunUtil.IRunnableResult: IRunUtil.IRunnableResult to execute

Returns
boolean true if operation completed successfully before maxTime expired

runFixedTimedRetry

boolean runFixedTimedRetry (long opTimeout, 
                long pollInterval, 
                long maxTime, 
                IRunUtil.IRunnableResult runnable)

Block and executes an operation multiple times until it is successful.

Parameters
opTimeout long: maximum time to wait in ms for a single operation attempt

pollInterval long: initial time to wait between operation attempts

maxTime long: the total approximate maximum time to keep trying the operation

runnable IRunUtil.IRunnableResult: IRunUtil.IRunnableResult to execute

Returns
boolean true if operation completed successfully before maxTime expired

runTimed

CommandStatus runTimed (long timeout, 
                IRunUtil.IRunnableResult runnable, 
                boolean logErrors)

Block and executes an operation, aborting if it takes longer than a specified time.

Parameters
timeout long: maximum time to wait in ms

runnable IRunUtil.IRunnableResult: IRunUtil.IRunnableResult to execute

logErrors boolean: log errors on exception or not.

Returns
CommandStatus the CommandStatus result of operation.

runTimedCmd

CommandResult runTimedCmd (long timeout, 
                String... command)

Helper method to execute a system command, and aborting if it takes longer than a specified time.

Parameters
timeout long: maximum time to wait in ms

command String: the specified system command and optionally arguments to exec

Returns
CommandResult a CommandResult containing result from command run

runTimedCmd

CommandResult runTimedCmd (long timeout, 
                OutputStream stdout, 
                OutputStream stderr, 
                String... command)

Helper method to execute a system command, abort if it takes longer than a specified time, and redirect output to files if specified. When OutputStream are provided this way, they will be left open at the end of the function.

Parameters
timeout long: timeout maximum time to wait in ms

stdout OutputStream: OutputStream where the std output will be redirected. Can be null.

stderr OutputStream: OutputStream where the error output will be redirected. Can be null.

command String: the specified system command and optionally arguments to exec

Returns
CommandResult a CommandResult containing result from command run

runTimedCmdRetry

CommandResult runTimedCmdRetry (long timeout, 
                long retryInterval, 
                int attempts, 
                String... command)

Helper method to execute a system command, and aborting if it takes longer than a specified time.

Parameters
timeout long: maximum time to wait in ms for each attempt

retryInterval long: time to wait between command retries

attempts int: the maximum number of attempts to try

command String: the specified system command and optionally arguments to exec

Returns
CommandResult a CommandResult containing result from command run

runTimedCmdSilently

CommandResult runTimedCmdSilently (long timeout, 
                String... command)

Helper method to execute a system command, and aborting if it takes longer than a specified time. Similar to runTimedCmd(long, String), but does not log any errors on exception.

Parameters
timeout long: maximum time to wait in ms

command String: the specified system command and optionally arguments to exec

Returns
CommandResult a CommandResult containing result from command run

runTimedCmdSilentlyRetry

CommandResult runTimedCmdSilentlyRetry (long timeout, 
                long retryInterval, 
                int attempts, 
                String... command)

Helper method to execute a system command, and aborting if it takes longer than a specified time. Similar to runTimedCmdRetry(long, long, int, String[]), but does not log any errors on exception.

Parameters
timeout long: maximum time to wait in ms

retryInterval long: time to wait between command retries

attempts int: the maximum number of attempts to try

command String: the specified system command and optionally arguments to exec

Returns
CommandResult a CommandResult containing result from command run

runTimedCmdWithInput

CommandResult runTimedCmdWithInput (long timeout, 
                String input, 
                List<String> command)

Helper method to execute a system command that requires stdin input, and aborting if it takes longer than a specified time.

Parameters
timeout long: maximum time to wait in ms

input String: the stdin input to pass to process

command List: List containing the system command and optionally arguments to exec

Returns
CommandResult a CommandResult containing result from command run

runTimedCmdWithInput

CommandResult runTimedCmdWithInput (long timeout, 
                String input, 
                String... command)

Helper method to execute a system command that requires stdin input, and aborting if it takes longer than a specified time.

Parameters
timeout long: maximum time to wait in ms

input String: the stdin input to pass to process

command String: the specified system command and optionally arguments to exec

Returns
CommandResult a CommandResult containing result from command run

runTimedRetry

boolean runTimedRetry (long opTimeout, 
                long pollInterval, 
                int attempts, 
                IRunUtil.IRunnableResult runnable)

Block and executes an operation multiple times until it is successful.

Parameters
opTimeout long: maximum time to wait in ms for one operation attempt

pollInterval long: time to wait between command retries

attempts int: the maximum number of attempts to try

runnable IRunUtil.IRunnableResult: IRunUtil.IRunnableResult to execute

Returns
boolean true if operation completed successfully before attempts reached.

setEnvVariable

void setEnvVariable (String key, 
                String value)

Sets a environment variable to be used when running system commands.

Parameters
key String: the variable name

value String: the variable value

See also:

setEnvVariablePriority

void setEnvVariablePriority (IRunUtil.EnvPriority priority)

Decide whether or not when creating a process, unsetting environment variable is higher priority than setting them. By Default, unsetting is higher priority: meaning if an attempt to set a variable with the same name is made, it won't happen since the variable will be unset. Cannot be used on the default IRunUtil instance.

Parameters
priority IRunUtil.EnvPriority

setInterruptibleInFuture

void setInterruptibleInFuture (Thread thread, 
                long timeMs)

Set as interruptible after some waiting time. shutdownHard() to enforce we terminate eventually.

Parameters
thread Thread: the thread that will become interruptible.

timeMs long: time to wait before setting interruptible.

setWorkingDir

void setWorkingDir (File dir)

Sets the working directory for system commands.

Parameters
dir File: the working directory

See also:

sleep

void sleep (long time)

Helper method to sleep for given time, ignoring any exceptions.

Parameters
time long: ms to sleep. values less than or equal to 0 will be ignored

unsetEnvVariable

void unsetEnvVariable (String key)

Unsets an environment variable, so the system commands run without this environment variable.

Parameters
key String: the variable name

See also: