Search in sources :

Example 1 with Deadline

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");
}
Also used : Deadline(com.peterphi.std.threading.Deadline) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with Deadline

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;
}
Also used : Deadline(com.peterphi.std.threading.Deadline) ArrayList(java.util.ArrayList)

Example 3 with Deadline

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);
    }
}
Also used : Exec(com.peterphi.std.system.exec.Exec) Deadline(com.peterphi.std.threading.Deadline) Execed(com.peterphi.std.system.exec.Execed) IOException(java.io.IOException)

Aggregations

Deadline (com.peterphi.std.threading.Deadline)3 Exec (com.peterphi.std.system.exec.Exec)1 Execed (com.peterphi.std.system.exec.Execed)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1