Search in sources :

Example 1 with WorkerProcessContext

use of org.gradle.process.internal.worker.WorkerProcessContext in project gradle by gradle.

the class ActionExecutionWorker method execute.

public void execute(final WorkerContext workerContext) {
    final ObjectConnection clientConnection = workerContext.getServerConnection();
    final ServiceRegistry serviceRegistry = workerContext.getServiceRegistry();
    LOGGER.debug("Starting {}.", displayName);
    WorkerProcessContext context = new WorkerProcessContext() {

        public ObjectConnection getServerConnection() {
            return clientConnection;
        }

        public ClassLoader getApplicationClassLoader() {
            return workerContext.getApplicationClassLoader();
        }

        public Object getWorkerId() {
            return workerId;
        }

        public String getDisplayName() {
            return displayName;
        }

        @Override
        public ServiceRegistry getServiceRegistry() {
            return serviceRegistry;
        }
    };
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(action.getClass().getClassLoader());
    NativeServices.initialize(gradleUserHomeDir, false);
    try {
        action.execute(context);
    } finally {
        Thread.currentThread().setContextClassLoader(contextClassLoader);
    }
    LOGGER.debug("Completed {}.", displayName);
}
Also used : WorkerProcessContext(org.gradle.process.internal.worker.WorkerProcessContext) ServiceRegistry(org.gradle.internal.service.ServiceRegistry) ObjectConnection(org.gradle.internal.remote.ObjectConnection)

Example 2 with WorkerProcessContext

use of org.gradle.process.internal.worker.WorkerProcessContext in project gradle by gradle.

the class SystemApplicationClassLoaderWorker method deserializeWorker.

private Action<WorkerProcessContext> deserializeWorker(byte[] serializedWorker) {
    Action<WorkerProcessContext> action;
    try {
        ObjectInputStream instr = new ClassLoaderObjectInputStream(new ByteArrayInputStream(serializedWorker), getClass().getClassLoader());
        @SuppressWarnings("unchecked") Action<WorkerProcessContext> deserializedAction = (Action<WorkerProcessContext>) instr.readObject();
        action = deserializedAction;
    } catch (Exception e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
    return action;
}
Also used : Action(org.gradle.api.Action) ByteArrayInputStream(java.io.ByteArrayInputStream) WorkerProcessContext(org.gradle.process.internal.worker.WorkerProcessContext) ClassLoaderObjectInputStream(org.gradle.internal.io.ClassLoaderObjectInputStream) UncheckedException(org.gradle.internal.UncheckedException) FileNotFoundException(java.io.FileNotFoundException) ObjectInputStream(java.io.ObjectInputStream) ClassLoaderObjectInputStream(org.gradle.internal.io.ClassLoaderObjectInputStream)

Example 3 with WorkerProcessContext

use of org.gradle.process.internal.worker.WorkerProcessContext in project gradle by gradle.

the class SystemApplicationClassLoaderWorker method call.

@Override
public Void call() throws Exception {
    if (System.getProperty("org.gradle.worker.test.stuck") != null) {
        // Simulate a stuck worker. There's probably a way to inject this failure...
        Thread.sleep(30000);
        return null;
    }
    Decoder decoder = new InputStreamBackedDecoder(configInputStream);
    // Read logging config and setup logging
    int logLevel = decoder.readSmallInt();
    LoggingServiceRegistry loggingServiceRegistry = LoggingServiceRegistry.newEmbeddableLogging();
    LoggingManagerInternal loggingManager = createLoggingManager(loggingServiceRegistry).setLevelInternal(LogLevel.values()[logLevel]);
    // Read whether process info should be published
    boolean shouldPublishJvmMemoryInfo = decoder.readBoolean();
    // Read path to Gradle user home
    String gradleUserHomeDirPath = decoder.readString();
    File gradleUserHomeDir = new File(gradleUserHomeDirPath);
    // Read server address and start connecting
    MultiChoiceAddress serverAddress = new MultiChoiceAddressSerializer().read(decoder);
    NativeServices.initializeOnWorker(gradleUserHomeDir);
    DefaultServiceRegistry basicWorkerServices = new DefaultServiceRegistry(NativeServices.getInstance(), loggingServiceRegistry);
    basicWorkerServices.add(ExecutorFactory.class, new DefaultExecutorFactory());
    basicWorkerServices.addProvider(new MessagingServices());
    final WorkerServices workerServices = new WorkerServices(basicWorkerServices, gradleUserHomeDir);
    WorkerLogEventListener workerLogEventListener = new WorkerLogEventListener();
    workerServices.add(WorkerLogEventListener.class, workerLogEventListener);
    File workingDirectory = workerServices.get(WorkerDirectoryProvider.class).getWorkingDirectory();
    File errorLog = getLastResortErrorLogFile(workingDirectory);
    PrintUnrecoverableErrorToFileHandler unrecoverableErrorHandler = new PrintUnrecoverableErrorToFileHandler(errorLog);
    ObjectConnection connection = null;
    try {
        // Read serialized worker details
        final long workerId = decoder.readSmallLong();
        final String displayName = decoder.readString();
        byte[] serializedWorker = decoder.readBinary();
        Action<WorkerProcessContext> workerAction = deserializeWorker(serializedWorker);
        connection = basicWorkerServices.get(MessagingClient.class).getConnection(serverAddress);
        connection.addUnrecoverableErrorHandler(unrecoverableErrorHandler);
        configureLogging(loggingManager, connection, workerLogEventListener);
        // start logging now that the logging manager is connected
        loggingManager.start();
        if (shouldPublishJvmMemoryInfo) {
            configureWorkerJvmMemoryInfoEvents(workerServices, connection);
        }
        ActionExecutionWorker worker = new ActionExecutionWorker(workerAction);
        worker.execute(new ContextImpl(workerId, displayName, connection, workerServices));
    } finally {
        try {
            loggingManager.removeOutputEventListener(workerLogEventListener);
            CompositeStoppable.stoppable(connection, basicWorkerServices).stop();
            loggingManager.stop();
        } catch (Throwable t) {
            // We're failing while shutting down, so log whatever might have happened.
            unrecoverableErrorHandler.execute(t);
        }
    }
    return null;
}
Also used : InputStreamBackedDecoder(org.gradle.internal.serialize.InputStreamBackedDecoder) MessagingServices(org.gradle.internal.remote.services.MessagingServices) Decoder(org.gradle.internal.serialize.Decoder) InputStreamBackedDecoder(org.gradle.internal.serialize.InputStreamBackedDecoder) DefaultServiceRegistry(org.gradle.internal.service.DefaultServiceRegistry) LoggingManagerInternal(org.gradle.internal.logging.LoggingManagerInternal) LoggingServiceRegistry(org.gradle.internal.logging.services.LoggingServiceRegistry) DefaultExecutorFactory(org.gradle.internal.concurrent.DefaultExecutorFactory) ObjectConnection(org.gradle.internal.remote.ObjectConnection) MultiChoiceAddress(org.gradle.internal.remote.internal.inet.MultiChoiceAddress) WorkerProcessContext(org.gradle.process.internal.worker.WorkerProcessContext) File(java.io.File) MultiChoiceAddressSerializer(org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer)

Aggregations

WorkerProcessContext (org.gradle.process.internal.worker.WorkerProcessContext)3 ObjectConnection (org.gradle.internal.remote.ObjectConnection)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 ObjectInputStream (java.io.ObjectInputStream)1 Action (org.gradle.api.Action)1 UncheckedException (org.gradle.internal.UncheckedException)1 DefaultExecutorFactory (org.gradle.internal.concurrent.DefaultExecutorFactory)1 ClassLoaderObjectInputStream (org.gradle.internal.io.ClassLoaderObjectInputStream)1 LoggingManagerInternal (org.gradle.internal.logging.LoggingManagerInternal)1 LoggingServiceRegistry (org.gradle.internal.logging.services.LoggingServiceRegistry)1 MultiChoiceAddress (org.gradle.internal.remote.internal.inet.MultiChoiceAddress)1 MultiChoiceAddressSerializer (org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer)1 MessagingServices (org.gradle.internal.remote.services.MessagingServices)1 Decoder (org.gradle.internal.serialize.Decoder)1 InputStreamBackedDecoder (org.gradle.internal.serialize.InputStreamBackedDecoder)1 DefaultServiceRegistry (org.gradle.internal.service.DefaultServiceRegistry)1 ServiceRegistry (org.gradle.internal.service.ServiceRegistry)1