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);
}
Aggregations