Search in sources :

Example 1 with DisconnectableInputStream

use of org.gradle.util.DisconnectableInputStream in project gradle by gradle.

the class InputForwarder method start.

public InputForwarder start() {
    lifecycleLock.lock();
    try {
        if (started) {
            throw new IllegalStateException("input forwarder has already been started");
        }
        disconnectableInput = new DisconnectableInputStream(input, bufferSize);
        outputBuffer = new LineBufferingOutputStream(handler, bufferSize);
        forwardingExecuter = executorFactory.create("Forward input");
        forwardingExecuter.execute(new Runnable() {

            public void run() {
                byte[] buffer = new byte[bufferSize];
                int readCount;
                Throwable readFailure = null;
                try {
                    while (true) {
                        try {
                            readCount = disconnectableInput.read(buffer, 0, bufferSize);
                            if (readCount < 0) {
                                break;
                            }
                        } catch (AsynchronousCloseException e) {
                            break;
                        } catch (IOException e) {
                            readFailure = e;
                            break;
                        }
                        outputBuffer.write(buffer, 0, readCount);
                    }
                    // will flush any unterminated lines out synchronously
                    outputBuffer.flush();
                } catch (IOException e) {
                    // should not happen
                    throw UncheckedException.throwAsUncheckedException(e);
                } finally {
                    handler.endOfStream(readFailure);
                }
            }
        });
        started = true;
    } finally {
        lifecycleLock.unlock();
    }
    return this;
}
Also used : DisconnectableInputStream(org.gradle.util.DisconnectableInputStream) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) LineBufferingOutputStream(org.gradle.internal.io.LineBufferingOutputStream) IOException(java.io.IOException)

Example 2 with DisconnectableInputStream

use of org.gradle.util.DisconnectableInputStream in project gradle by gradle.

the class StreamsForwarder method connectStreams.

public void connectStreams(Process process, String processName, ExecutorFactory executorFactory) {
    /*
            There's a potential problem here in that DisconnectableInputStream reads from input in the background.
            This won't automatically stop when the process is over. Therefore, if input is not closed then this thread
            will run forever. It would be better to ensure that this thread stops when the process does.
         */
    InputStream instr = new DisconnectableInputStream(input);
    standardOutputRunner = new ExecOutputHandleRunner("read standard output of: " + processName, process.getInputStream(), standardOutput);
    errorOutputRunner = new ExecOutputHandleRunner("read error output of: " + processName, process.getErrorStream(), errorOutput);
    standardInputRunner = new ExecOutputHandleRunner("write standard input into: " + processName, instr, process.getOutputStream());
    this.executor = executorFactory.create("Forward streams with process: " + processName);
}
Also used : DisconnectableInputStream(org.gradle.util.DisconnectableInputStream) DisconnectableInputStream(org.gradle.util.DisconnectableInputStream) InputStream(java.io.InputStream)

Example 3 with DisconnectableInputStream

use of org.gradle.util.DisconnectableInputStream in project gradle by gradle.

the class ContinuousBuildActionExecuter method executeMultipleBuilds.

private Object executeMultipleBuilds(BuildAction action, BuildRequestContext requestContext, final BuildActionParameters actionParameters, ServiceRegistry buildSessionScopeServices) {
    SingleMessageLogger.incubatingFeatureUsed("Continuous build");
    BuildCancellationToken cancellationToken = requestContext.getCancellationToken();
    final CancellableOperationManager cancellableOperationManager;
    if (actionParameters.isInteractive()) {
        if (!(System.in instanceof DisconnectableInputStream)) {
            System.setIn(new DisconnectableInputStream(System.in));
        }
        DisconnectableInputStream inputStream = (DisconnectableInputStream) System.in;
        cancellableOperationManager = new DefaultCancellableOperationManager(executorFactory.create("cancel signal monitor"), inputStream, cancellationToken);
    } else {
        cancellableOperationManager = new PassThruCancellableOperationManager(cancellationToken);
    }
    Object lastResult = null;
    int counter = 0;
    while (!cancellationToken.isCancellationRequested()) {
        if (++counter != 1) {
            // reset the time the build started so the total time makes sense
            requestContext.getBuildTimeClock().reset();
            logger.println("Change detected, executing build...").println();
        }
        final FileSystemChangeWaiter waiter = changeWaiterFactory.createChangeWaiter(cancellationToken);
        try {
            try {
                lastResult = executeBuildAndAccumulateInputs(action, requestContext, actionParameters, waiter, buildSessionScopeServices);
            } catch (ReportedException t) {
                lastResult = t;
            }
            if (!waiter.isWatching()) {
                logger.println().withStyle(StyledTextOutput.Style.Failure).println("Exiting continuous build as no executed tasks declared file system inputs.");
                if (lastResult instanceof ReportedException) {
                    throw (ReportedException) lastResult;
                }
                return lastResult;
            } else {
                cancellableOperationManager.monitorInput(new Action<BuildCancellationToken>() {

                    @Override
                    public void execute(BuildCancellationToken cancellationToken) {
                        ChangeReporter reporter = new ChangeReporter();
                        waiter.wait(new Runnable() {

                            @Override
                            public void run() {
                                logger.println().println("Waiting for changes to input files of tasks..." + determineExitHint(actionParameters));
                            }
                        }, reporter);
                        if (!cancellationToken.isCancellationRequested()) {
                            reporter.reportChanges(logger);
                        }
                    }
                });
            }
        } finally {
            waiter.stop();
        }
    }
    logger.println("Build cancelled.");
    if (lastResult instanceof ReportedException) {
        throw (ReportedException) lastResult;
    }
    return lastResult;
}
Also used : DisconnectableInputStream(org.gradle.util.DisconnectableInputStream) DefaultCancellableOperationManager(org.gradle.execution.DefaultCancellableOperationManager) PassThruCancellableOperationManager(org.gradle.execution.PassThruCancellableOperationManager) FileSystemChangeWaiter(org.gradle.internal.filewatch.FileSystemChangeWaiter) DefaultCancellableOperationManager(org.gradle.execution.DefaultCancellableOperationManager) PassThruCancellableOperationManager(org.gradle.execution.PassThruCancellableOperationManager) CancellableOperationManager(org.gradle.execution.CancellableOperationManager) BuildCancellationToken(org.gradle.initialization.BuildCancellationToken) ChangeReporter(org.gradle.internal.filewatch.ChangeReporter) ReportedException(org.gradle.initialization.ReportedException)

Example 4 with DisconnectableInputStream

use of org.gradle.util.DisconnectableInputStream in project gradle by gradle.

the class ForwardStdinStreamsHandler method connectStreams.

@Override
public void connectStreams(Process process, String processName, Executor executor) {
    this.executor = executor;
    /*
            There's a potential problem here in that DisconnectableInputStream reads from input in the background.
            This won't automatically stop when the process is over. Therefore, if input is not closed then this thread
            will run forever. It would be better to ensure that this thread stops when the process does.
         */
    InputStream instr = new DisconnectableInputStream(input);
    standardInputWriter = new ExecOutputHandleRunner("write standard input to " + processName, instr, process.getOutputStream(), completed);
}
Also used : DisconnectableInputStream(org.gradle.util.DisconnectableInputStream) DisconnectableInputStream(org.gradle.util.DisconnectableInputStream) InputStream(java.io.InputStream)

Example 5 with DisconnectableInputStream

use of org.gradle.util.DisconnectableInputStream in project gradle by gradle.

the class ContinuousBuildActionExecuter method createCancellableOperationManager.

private CancellableOperationManager createCancellableOperationManager(BuildActionParameters actionParameters, BuildCancellationToken cancellationToken) {
    final CancellableOperationManager cancellableOperationManager;
    if (actionParameters.isInteractive()) {
        if (!(System.in instanceof DisconnectableInputStream)) {
            System.setIn(new DisconnectableInputStream(System.in));
        }
        DisconnectableInputStream inputStream = (DisconnectableInputStream) System.in;
        cancellableOperationManager = new DefaultCancellableOperationManager(executorFactory.create("Cancel signal monitor"), inputStream, cancellationToken);
    } else {
        cancellableOperationManager = new PassThruCancellableOperationManager(cancellationToken);
    }
    return cancellableOperationManager;
}
Also used : DisconnectableInputStream(org.gradle.util.DisconnectableInputStream) DefaultCancellableOperationManager(org.gradle.execution.DefaultCancellableOperationManager) DefaultCancellableOperationManager(org.gradle.execution.DefaultCancellableOperationManager) PassThruCancellableOperationManager(org.gradle.execution.PassThruCancellableOperationManager) CancellableOperationManager(org.gradle.execution.CancellableOperationManager) PassThruCancellableOperationManager(org.gradle.execution.PassThruCancellableOperationManager)

Aggregations

DisconnectableInputStream (org.gradle.util.DisconnectableInputStream)5 InputStream (java.io.InputStream)2 CancellableOperationManager (org.gradle.execution.CancellableOperationManager)2 DefaultCancellableOperationManager (org.gradle.execution.DefaultCancellableOperationManager)2 PassThruCancellableOperationManager (org.gradle.execution.PassThruCancellableOperationManager)2 IOException (java.io.IOException)1 AsynchronousCloseException (java.nio.channels.AsynchronousCloseException)1 BuildCancellationToken (org.gradle.initialization.BuildCancellationToken)1 ReportedException (org.gradle.initialization.ReportedException)1 ChangeReporter (org.gradle.internal.filewatch.ChangeReporter)1 FileSystemChangeWaiter (org.gradle.internal.filewatch.FileSystemChangeWaiter)1 LineBufferingOutputStream (org.gradle.internal.io.LineBufferingOutputStream)1