Search in sources :

Example 1 with ForwardingProcessListener

use of com.facebook.buck.util.ForwardingProcessListener 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)

Example 2 with ForwardingProcessListener

use of com.facebook.buck.util.ForwardingProcessListener in project buck by facebook.

the class ProjectCommand method runPreprocessScriptIfNeeded.

private int runPreprocessScriptIfNeeded(CommandRunnerParams params) throws IOException, InterruptedException {
    Optional<String> pathToPreProcessScript = getPathToPreProcessScript(params.getBuckConfig());
    if (!pathToPreProcessScript.isPresent()) {
        return 0;
    }
    String pathToScript = pathToPreProcessScript.get();
    if (!Paths.get(pathToScript).isAbsolute()) {
        pathToScript = params.getCell().getFilesystem().getPathForRelativePath(pathToScript).toAbsolutePath().toString();
    }
    ListeningProcessExecutor processExecutor = new ListeningProcessExecutor();
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().addCommand(pathToScript).setEnvironment(ImmutableMap.<String, String>builder().putAll(params.getEnvironment()).put("BUCK_PROJECT_TARGETS", Joiner.on(" ").join(getArguments())).build()).setDirectory(params.getCell().getFilesystem().getRootPath()).build();
    ForwardingProcessListener processListener = new ForwardingProcessListener(// because this process finishes before we start parsing process.
    Channels.newChannel(params.getConsole().getStdOut().getRawStream()), Channels.newChannel(params.getConsole().getStdErr().getRawStream()));
    ListeningProcessExecutor.LaunchedProcess process = processExecutor.launchProcess(processExecutorParams, processListener);
    try {
        return processExecutor.waitForProcess(process);
    } finally {
        processExecutor.destroyProcess(process, /* force */
        false);
        processExecutor.waitForProcess(process);
    }
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ListeningProcessExecutor(com.facebook.buck.util.ListeningProcessExecutor) ForwardingProcessListener(com.facebook.buck.util.ForwardingProcessListener)

Example 3 with ForwardingProcessListener

use of com.facebook.buck.util.ForwardingProcessListener in project buck by facebook.

the class RunCommand method runWithoutHelp.

@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
    if (!hasTargetSpecified()) {
        params.getBuckEventBus().post(ConsoleEvent.severe("No target given to run"));
        params.getBuckEventBus().post(ConsoleEvent.severe("buck run <target> <arg1> <arg2>..."));
        return 1;
    }
    // Make sure the target is built.
    BuildCommand buildCommand = new BuildCommand(ImmutableList.of(getTarget(params.getBuckConfig())));
    int exitCode = buildCommand.runWithoutHelp(params);
    if (exitCode != 0) {
        return exitCode;
    }
    String targetName = getTarget(params.getBuckConfig());
    BuildTarget target = Iterables.getOnlyElement(getBuildTargets(params.getCell().getCellPathResolver(), ImmutableSet.of(targetName)));
    Build build = buildCommand.getBuild();
    BuildRule targetRule;
    try {
        targetRule = build.getRuleResolver().requireRule(target);
    } catch (NoSuchBuildTargetException e) {
        throw new HumanReadableException(e.getHumanReadableErrorMessage());
    }
    BinaryBuildRule binaryBuildRule = null;
    if (targetRule instanceof BinaryBuildRule) {
        binaryBuildRule = (BinaryBuildRule) targetRule;
    }
    if (binaryBuildRule == null) {
        params.getBuckEventBus().post(ConsoleEvent.severe("target " + targetName + " is not a binary rule (only binary rules can be `run`)"));
        return 1;
    }
    // Ideally, we would take fullCommand, disconnect from NailGun, and run the command in the
    // user's shell. Currently, if you use `buck run` with buckd and ctrl-C to kill the command
    // being run, occasionally I get the following error when I try to run `buck run` again:
    //
    //   Daemon is busy, please wait or run "buck kill" to terminate it.
    //
    // Clearly something bad has happened here. If you are using `buck run` to start up a server
    // or some other process that is meant to "run forever," then it's pretty common to do:
    // `buck run`, test server, hit ctrl-C, edit server code, repeat. This should not wedge buckd.
    SourcePathResolver resolver = new SourcePathResolver(new SourcePathRuleFinder(build.getRuleResolver()));
    Tool executable = binaryBuildRule.getExecutableCommand();
    ListeningProcessExecutor processExecutor = new ListeningProcessExecutor();
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().addAllCommand(executable.getCommandPrefix(resolver)).addAllCommand(getTargetArguments()).setEnvironment(ImmutableMap.<String, String>builder().putAll(params.getEnvironment()).putAll(executable.getEnvironment(resolver)).build()).setDirectory(params.getCell().getFilesystem().getRootPath()).build();
    ForwardingProcessListener processListener = new ForwardingProcessListener(Channels.newChannel(params.getConsole().getStdOut()), Channels.newChannel(params.getConsole().getStdErr()));
    ListeningProcessExecutor.LaunchedProcess process = processExecutor.launchProcess(processExecutorParams, processListener);
    try {
        return processExecutor.waitForProcess(process);
    } finally {
        processExecutor.destroyProcess(process, /* force */
        false);
        processExecutor.waitForProcess(process);
    }
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildTarget(com.facebook.buck.model.BuildTarget) Build(com.facebook.buck.command.Build) HumanReadableException(com.facebook.buck.util.HumanReadableException) ListeningProcessExecutor(com.facebook.buck.util.ListeningProcessExecutor) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) ForwardingProcessListener(com.facebook.buck.util.ForwardingProcessListener) BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) Tool(com.facebook.buck.rules.Tool)

Example 4 with ForwardingProcessListener

use of com.facebook.buck.util.ForwardingProcessListener in project buck by facebook.

the class TestCommand method runTestsExternal.

private int runTestsExternal(final CommandRunnerParams params, Build build, Iterable<String> command, Iterable<TestRule> testRules, SourcePathResolver pathResolver) throws InterruptedException, IOException {
    TestRunningOptions options = getTestRunningOptions(params);
    // Walk the test rules, collecting all the specs.
    List<ExternalTestRunnerTestSpec> specs = Lists.newArrayList();
    for (TestRule testRule : testRules) {
        if (!(testRule instanceof ExternalTestRunnerRule)) {
            params.getBuckEventBus().post(ConsoleEvent.severe(String.format("Test %s does not support external test running", testRule.getBuildTarget())));
            return 1;
        }
        ExternalTestRunnerRule rule = (ExternalTestRunnerRule) testRule;
        specs.add(rule.getExternalTestRunnerSpec(build.getExecutionContext(), options, pathResolver));
    }
    // Serialize the specs to a file to pass into the test runner.
    Path infoFile = params.getCell().getFilesystem().resolve(params.getCell().getFilesystem().getBuckPaths().getScratchDir()).resolve("external_runner_specs.json");
    Files.createDirectories(infoFile.getParent());
    Files.deleteIfExists(infoFile);
    params.getObjectMapper().writerWithDefaultPrettyPrinter().writeValue(infoFile.toFile(), specs);
    // Launch and run the external test runner, forwarding it's stdout/stderr to the console.
    // We wait for it to complete then returns its error code.
    ListeningProcessExecutor processExecutor = new ListeningProcessExecutor();
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().addAllCommand(command).addAllCommand(withDashArguments).setEnvironment(params.getEnvironment()).addCommand("--buck-test-info", infoFile.toString()).addCommand("--jobs", String.valueOf(getConcurrencyLimit(params.getBuckConfig()).threadLimit)).setDirectory(params.getCell().getFilesystem().getRootPath()).build();
    ForwardingProcessListener processListener = new ForwardingProcessListener(Channels.newChannel(params.getConsole().getStdOut()), Channels.newChannel(params.getConsole().getStdErr()));
    ListeningProcessExecutor.LaunchedProcess process = processExecutor.launchProcess(processExecutorParams, processListener);
    try {
        return processExecutor.waitForProcess(process);
    } finally {
        processExecutor.destroyProcess(process, /* force */
        false);
        processExecutor.waitForProcess(process);
    }
}
Also used : Path(java.nio.file.Path) TestRule(com.facebook.buck.rules.TestRule) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ListeningProcessExecutor(com.facebook.buck.util.ListeningProcessExecutor) ExternalTestRunnerRule(com.facebook.buck.rules.ExternalTestRunnerRule) ForwardingProcessListener(com.facebook.buck.util.ForwardingProcessListener) TestRunningOptions(com.facebook.buck.test.TestRunningOptions) ExternalTestRunnerTestSpec(com.facebook.buck.rules.ExternalTestRunnerTestSpec)

Aggregations

ForwardingProcessListener (com.facebook.buck.util.ForwardingProcessListener)4 ListeningProcessExecutor (com.facebook.buck.util.ListeningProcessExecutor)4 ProcessExecutorParams (com.facebook.buck.util.ProcessExecutorParams)3 BserDeserializer (com.facebook.buck.bser.BserDeserializer)1 Build (com.facebook.buck.command.Build)1 BuildTarget (com.facebook.buck.model.BuildTarget)1 NoSuchBuildTargetException (com.facebook.buck.parser.NoSuchBuildTargetException)1 BinaryBuildRule (com.facebook.buck.rules.BinaryBuildRule)1 BuildRule (com.facebook.buck.rules.BuildRule)1 ExternalTestRunnerRule (com.facebook.buck.rules.ExternalTestRunnerRule)1 ExternalTestRunnerTestSpec (com.facebook.buck.rules.ExternalTestRunnerTestSpec)1 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)1 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)1 TestRule (com.facebook.buck.rules.TestRule)1 Tool (com.facebook.buck.rules.Tool)1 TestRunningOptions (com.facebook.buck.test.TestRunningOptions)1 HumanReadableException (com.facebook.buck.util.HumanReadableException)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1