use of com.peterphi.std.threading.Deadline in project stdlib by petergeneric.
the class AzureVMControlImpl method waitForResult.
private void waitForResult(final ListenableFuture<Void> future, final Timeout timeout) throws InterruptedException {
Deadline deadline = timeout.start();
while (deadline.isValid()) {
if (future.isDone()) {
try {
// get the future to allow any thrown exceptions to appear
future.get();
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
return;
}
if (deadline.isValid()) {
try {
Thread.sleep(Timeout.TEN_SECONDS.getMilliseconds());
} catch (InterruptedException ie) {
// cancel the future im waiting on
future.cancel(true);
throw ie;
}
}
}
// taken too long
future.cancel(true);
throw new RuntimeException("Hit timeout of " + timeout + " waiting for azure api call to complete");
}
use of com.peterphi.std.threading.Deadline in project stdlib by petergeneric.
the class SudoFeature method hasSudo.
/**
* Determines whether sudo is supported on the local machine
*
* @return
*/
public static synchronized boolean hasSudo() {
if (!sudoTested) {
String[] cmdArr = new String[] { "which", "sudo" };
List<String> cmd = new ArrayList<String>(cmdArr.length);
Collections.addAll(cmd, cmdArr);
ProcessBuilder whichsudo = new ProcessBuilder(cmd);
try {
Process p = whichsudo.start();
try {
Execed e = new Execed(cmd, p, false);
// Let it run
int returnCode = e.waitForExit(new Deadline(3, TimeUnit.SECONDS));
sudoSupported = (returnCode == 0);
sudoTested = true;
} finally {
p.destroy();
}
} catch (Throwable t) {
sudoSupported = false;
sudoTested = true;
}
}
return sudoSupported;
}
use of com.peterphi.std.threading.Deadline in project stdlib by petergeneric.
the class MediaInfoCommand method inspect.
public MediaInfo inspect(File mediafile) throws IOException {
log.debug("Retrieving mediainfo output for " + mediafile.getAbsolutePath());
if (!mediafile.exists())
throw new IllegalArgumentException("Media file " + mediafile + " does not exist!");
Exec exec = new Exec(mediainfo.getAbsolutePath(), "--output=XML", "--full", mediafile.getAbsolutePath());
final Execed process = exec.start();
final Deadline timeOut = new Deadline(Timeout.ONE_MINUTE);
final int result = process.waitForExit(timeOut);
if (result == 0) {
return parse(process.getStandardOut());
} else {
throw new IOException("MediaInfo failed with non-zero code: " + result);
}
}
Aggregations