Search in sources :

Example 1 with BserDeserializer

use of com.facebook.buck.bser.BserDeserializer in project buck by facebook.

the class Watchman method execute.

@SuppressWarnings("unchecked")
private static Optional<Map<String, Object>> execute(ListeningProcessExecutor executor, Console console, Clock clock, long commandTimeoutMillis, long timeoutNanos, Path watchmanPath, String... args) throws InterruptedException, IOException {
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    ByteArrayOutputStream stderr = new ByteArrayOutputStream();
    ForwardingProcessListener listener = new ForwardingProcessListener(Channels.newChannel(stdout), Channels.newChannel(stderr));
    ListeningProcessExecutor.LaunchedProcess process = executor.launchProcess(ProcessExecutorParams.builder().addCommand(watchmanPath.toString(), "--output-encoding=bser").addCommand(args).build(), listener);
    long startTimeNanos = clock.nanoTime();
    int exitCode = executor.waitForProcess(process, Math.min(timeoutNanos, POLL_TIME_NANOS), TimeUnit.NANOSECONDS);
    if (exitCode == Integer.MIN_VALUE) {
        // Let the user know we're still here waiting for Watchman, then wait the
        // rest of the timeout period.
        long remainingNanos = timeoutNanos - (clock.nanoTime() - startTimeNanos);
        if (remainingNanos > 0) {
            console.getStdErr().getRawStream().format("Waiting for Watchman command [%s]...\n", Joiner.on(" ").join(args));
            exitCode = executor.waitForProcess(process, remainingNanos, TimeUnit.NANOSECONDS);
        }
    }
    LOG.debug("Waited %d ms for Watchman command %s, exit code %d", TimeUnit.NANOSECONDS.toMillis(clock.nanoTime() - startTimeNanos), Joiner.on(" ").join(args), exitCode);
    if (exitCode == Integer.MIN_VALUE) {
        LOG.warn("Watchman did not respond within %d ms, disabling.", commandTimeoutMillis);
        console.getStdErr().getRawStream().format("Timed out after %d ms waiting for Watchman command [%s]. Disabling Watchman.\n", commandTimeoutMillis, Joiner.on(" ").join(args));
        return Optional.empty();
    }
    if (exitCode != 0) {
        LOG.debug("Watchman's stderr: %s", new String(stderr.toByteArray(), Charsets.UTF_8));
        LOG.error("Error %d executing %s", exitCode, Joiner.on(" ").join(args));
        return Optional.empty();
    }
    Object response = new BserDeserializer(BserDeserializer.KeyOrdering.UNSORTED).deserializeBserValue(new ByteArrayInputStream(stdout.toByteArray()));
    LOG.debug("stdout of command: " + response);
    if (!(response instanceof Map<?, ?>)) {
        LOG.error("Unexpected response from Watchman: %s", response);
        return Optional.empty();
    }
    return Optional.of((Map<String, Object>) response);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ListeningProcessExecutor(com.facebook.buck.util.ListeningProcessExecutor) ForwardingProcessListener(com.facebook.buck.util.ForwardingProcessListener) BserDeserializer(com.facebook.buck.bser.BserDeserializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

BserDeserializer (com.facebook.buck.bser.BserDeserializer)1 ForwardingProcessListener (com.facebook.buck.util.ForwardingProcessListener)1 ListeningProcessExecutor (com.facebook.buck.util.ListeningProcessExecutor)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Map (java.util.Map)1