Search in sources :

Example 6 with CommandLineException

use of com.thoughtworks.go.util.command.CommandLineException in project gocd by gocd.

the class ExecCommandExecutor method executeCommandLine.

private int executeCommandLine(final BuildSession buildSession, final CommandLine commandLine) {
    final AtomicInteger exitCode = new AtomicInteger(-1);
    final CountDownLatch canceledOrDone = new CountDownLatch(1);
    buildSession.submitRunnable(new Runnable() {

        @Override
        public void run() {
            try {
                exitCode.set(commandLine.run(buildSession.processOutputStreamConsumer(), null));
            } catch (CommandLineException e) {
                LOG.error("Command failed", e);
                String message = format("Error happened while attempting to execute '%s'. \nPlease make sure [%s] can be executed on this agent.\n", commandLine.toStringForDisplay(), commandLine.getExecutable());
                String path = System.getenv("PATH");
                buildSession.println(message);
                buildSession.println(format("[Debug Information] Environment variable PATH: %s", path));
                LOG.error(format("[Command Line] %s. Path: %s", message, path));
            } finally {
                canceledOrDone.countDown();
            }
        }
    });
    Future<?> cancelMonitor = buildSession.submitRunnable(new Runnable() {

        @Override
        public void run() {
            try {
                buildSession.waitUntilCanceled();
            } catch (InterruptedException e) {
            // ignore
            } finally {
                canceledOrDone.countDown();
            }
        }
    });
    try {
        canceledOrDone.await();
    } catch (InterruptedException e) {
        LOG.error("Building thread interrupted", e);
    }
    cancelMonitor.cancel(true);
    return exitCode.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) CommandLineException(com.thoughtworks.go.util.command.CommandLineException)

Aggregations

CommandLineException (com.thoughtworks.go.util.command.CommandLineException)6 CommandLine (com.thoughtworks.go.util.command.CommandLine)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)1 ConsoleResult (com.thoughtworks.go.util.command.ConsoleResult)1 EnvironmentVariableContext (com.thoughtworks.go.util.command.EnvironmentVariableContext)1 InMemoryStreamConsumer (com.thoughtworks.go.util.command.InMemoryStreamConsumer)1 ProcessOutputStreamConsumer (com.thoughtworks.go.util.command.ProcessOutputStreamConsumer)1 File (java.io.File)1 IOException (java.io.IOException)1