Search in sources :

Example 1 with Receiver

use of org.gradle.process.internal.worker.request.Receiver in project gradle by gradle.

the class DefaultSingleRequestWorkerProcessBuilder method build.

@Override
public PROTOCOL build() {
    return protocolType.cast(Proxy.newProxyInstance(protocolType.getClassLoader(), new Class[] { protocolType }, new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Receiver receiver = new Receiver(getBaseName());
            try {
                WorkerProcess workerProcess = builder.build();
                workerProcess.start();
                ObjectConnection connection = workerProcess.getConnection();
                RequestProtocol requestProtocol = connection.addOutgoing(RequestProtocol.class);
                connection.addIncoming(ResponseProtocol.class, receiver);
                connection.useJavaSerializationForParameters(workerImplementation.getClassLoader());
                connection.connect();
                // TODO(ew): inject BuildOperationIdentifierRegistry instead of static use
                requestProtocol.runThenStop(method.getName(), method.getParameterTypes(), args, CurrentBuildOperationRef.instance().get());
                boolean hasResult = receiver.awaitNextResult();
                workerProcess.waitForStop();
                if (!hasResult) {
                    // Reached the end of input, worker has exited without failing
                    throw new IllegalStateException(String.format("No response was received from %s but the worker process has finished.", getBaseName()));
                }
            } catch (Exception e) {
                throw WorkerProcessException.runFailed(getBaseName(), e);
            }
            return receiver.getNextResult();
        }
    }));
}
Also used : Receiver(org.gradle.process.internal.worker.request.Receiver) RequestProtocol(org.gradle.process.internal.worker.request.RequestProtocol) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) ObjectConnection(org.gradle.internal.remote.ObjectConnection)

Example 2 with Receiver

use of org.gradle.process.internal.worker.request.Receiver in project gradle by gradle.

the class DefaultMultiRequestWorkerProcessBuilder method build.

@Override
public WORKER build() {
    // Always publish process info for multi-request workers
    workerProcessBuilder.enableJvmMemoryInfoPublishing(true);
    final WorkerProcess workerProcess = workerProcessBuilder.build();
    return workerType.cast(Proxy.newProxyInstance(workerType.getClassLoader(), new Class[] { workerType }, new InvocationHandler() {

        private Receiver receiver = new Receiver(getBaseName());

        private RequestProtocol requestProtocol;

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (method.equals(START_METHOD)) {
                try {
                    workerProcess.start();
                } catch (Exception e) {
                    throw WorkerProcessException.runFailed(getBaseName(), e);
                }
                workerProcess.getConnection().addIncoming(ResponseProtocol.class, receiver);
                workerProcess.getConnection().useJavaSerializationForParameters(workerImplementation.getClassLoader());
                requestProtocol = workerProcess.getConnection().addOutgoing(RequestProtocol.class);
                workerProcess.getConnection().connect();
                return workerProcess;
            }
            if (method.equals(STOP_METHOD)) {
                if (requestProtocol != null) {
                    requestProtocol.stop();
                }
                try {
                    return workerProcess.waitForStop();
                } finally {
                    requestProtocol = null;
                }
            }
            requestProtocol.run(method.getName(), method.getParameterTypes(), args, CurrentBuildOperationRef.instance().get());
            boolean hasResult = receiver.awaitNextResult();
            if (!hasResult) {
                try {
                    // Reached the end of input, worker has crashed or exited
                    requestProtocol = null;
                    workerProcess.waitForStop();
                    // Worker didn't crash
                    throw new IllegalStateException(String.format("No response was received from %s but the worker process has finished.", getBaseName()));
                } catch (Exception e) {
                    throw WorkerProcessException.runFailed(getBaseName(), e);
                }
            }
            return receiver.getNextResult();
        }
    }));
}
Also used : Receiver(org.gradle.process.internal.worker.request.Receiver) RequestProtocol(org.gradle.process.internal.worker.request.RequestProtocol) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) UncheckedException(org.gradle.internal.UncheckedException)

Aggregations

InvocationHandler (java.lang.reflect.InvocationHandler)2 Method (java.lang.reflect.Method)2 Receiver (org.gradle.process.internal.worker.request.Receiver)2 RequestProtocol (org.gradle.process.internal.worker.request.RequestProtocol)2 UncheckedException (org.gradle.internal.UncheckedException)1 ObjectConnection (org.gradle.internal.remote.ObjectConnection)1