Search in sources :

Example 1 with TaskOutputCachingBuildCacheKey

use of org.gradle.caching.internal.tasks.TaskOutputCachingBuildCacheKey in project gradle by gradle.

the class ResolveBuildCacheKeyExecuter method execute.

@Override
public void execute(TaskInternal task, TaskStateInternal state, TaskExecutionContext context) {
    TaskArtifactState taskState = context.getTaskArtifactState();
    TaskOutputCachingBuildCacheKey cacheKey = taskState.calculateCacheKey();
    context.setBuildCacheKey(cacheKey);
    if (task.getOutputs().getHasOutput()) {
        // A task with no outputs an no cache key.
        listener.cacheKeyEvaluated(task, cacheKey);
        if (cacheKey.isValid()) {
            LOGGER.info("Cache key for {} is {}", task, cacheKey.getHashCode());
        }
    }
    delegate.execute(task, state, context);
}
Also used : TaskArtifactState(org.gradle.api.internal.changedetection.TaskArtifactState) TaskOutputCachingBuildCacheKey(org.gradle.caching.internal.tasks.TaskOutputCachingBuildCacheKey)

Example 2 with TaskOutputCachingBuildCacheKey

use of org.gradle.caching.internal.tasks.TaskOutputCachingBuildCacheKey in project gradle by gradle.

the class SkipCachedTaskExecuter method execute.

@Override
public void execute(final TaskInternal task, TaskStateInternal state, TaskExecutionContext context) {
    final Timer clock = Timers.startTimer();
    LOGGER.debug("Determining if {} is cached already", task);
    final TaskOutputsInternal taskOutputs = task.getOutputs();
    TaskOutputCachingBuildCacheKey cacheKey = context.getBuildCacheKey();
    boolean taskOutputCachingEnabled = state.getTaskOutputCaching().isEnabled();
    if (taskOutputCachingEnabled) {
        if (cacheKey.isValid()) {
            TaskArtifactState taskState = context.getTaskArtifactState();
            if (taskState.isAllowedToUseCachedResults()) {
                boolean found = buildCache.load(cacheKey, new BuildCacheEntryReader() {

                    @Override
                    public void readFrom(final InputStream input) {
                        taskOutputsGenerationListener.beforeTaskOutputsGenerated();
                        packer.unpack(taskOutputs, input, taskOutputOriginFactory.createReader(task));
                        LOGGER.info("Unpacked output for {} from cache (took {}).", task, clock.getElapsed());
                    }
                });
                if (found) {
                    state.setOutcome(TaskExecutionOutcome.FROM_CACHE);
                    return;
                }
            } else {
                LOGGER.info("Not loading {} from cache because pulling from cache is disabled for this task", task);
            }
        } else {
            LOGGER.info("Not caching {} because no valid cache key was generated", task);
        }
    }
    delegate.execute(task, state, context);
    if (taskOutputCachingEnabled) {
        if (cacheKey.isValid()) {
            if (state.getFailure() == null) {
                buildCache.store(cacheKey, new BuildCacheEntryWriter() {

                    @Override
                    public void writeTo(OutputStream output) {
                        LOGGER.info("Packing {}", task.getPath());
                        packer.pack(taskOutputs, output, taskOutputOriginFactory.createWriter(task, clock.getElapsedMillis()));
                    }
                });
            } else {
                LOGGER.debug("Not pushing result from {} to cache because the task failed", task);
            }
        } else {
            LOGGER.info("Not pushing results from {} to cache because no valid cache key was generated", task);
        }
    }
}
Also used : Timer(org.gradle.internal.time.Timer) TaskArtifactState(org.gradle.api.internal.changedetection.TaskArtifactState) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) TaskOutputCachingBuildCacheKey(org.gradle.caching.internal.tasks.TaskOutputCachingBuildCacheKey) BuildCacheEntryReader(org.gradle.caching.BuildCacheEntryReader) TaskOutputsInternal(org.gradle.api.internal.TaskOutputsInternal) BuildCacheEntryWriter(org.gradle.caching.BuildCacheEntryWriter)

Example 3 with TaskOutputCachingBuildCacheKey

use of org.gradle.caching.internal.tasks.TaskOutputCachingBuildCacheKey in project gradle by gradle.

the class VerifyNoInputChangesTaskExecuter method execute.

@Override
public void execute(TaskInternal task, TaskStateInternal state, TaskExecutionContext context) {
    TaskOutputCachingBuildCacheKey beforeExecution = context.getBuildCacheKey();
    delegate.execute(task, state, context);
    if (beforeExecution.isValid()) {
        TaskOutputCachingBuildCacheKey afterExecution = repository.getStateFor(task).calculateCacheKey();
        if (!afterExecution.isValid()) {
            throw new TaskExecutionException(task, new GradleException("The build cache key became invalid after the task has been executed!"));
        }
        if (!beforeExecution.getHashCode().equals(afterExecution.getHashCode())) {
            throw new TaskExecutionException(task, new GradleException("The inputs for the task changed during the execution! Check if you have a `doFirst` changing the inputs."));
        }
    }
}
Also used : TaskExecutionException(org.gradle.api.tasks.TaskExecutionException) GradleException(org.gradle.api.GradleException) TaskOutputCachingBuildCacheKey(org.gradle.caching.internal.tasks.TaskOutputCachingBuildCacheKey)

Aggregations

TaskOutputCachingBuildCacheKey (org.gradle.caching.internal.tasks.TaskOutputCachingBuildCacheKey)3 TaskArtifactState (org.gradle.api.internal.changedetection.TaskArtifactState)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 GradleException (org.gradle.api.GradleException)1 TaskOutputsInternal (org.gradle.api.internal.TaskOutputsInternal)1 TaskExecutionException (org.gradle.api.tasks.TaskExecutionException)1 BuildCacheEntryReader (org.gradle.caching.BuildCacheEntryReader)1 BuildCacheEntryWriter (org.gradle.caching.BuildCacheEntryWriter)1 Timer (org.gradle.internal.time.Timer)1