Search in sources :

Example 1 with BuildCacheEntryReader

use of org.gradle.caching.BuildCacheEntryReader 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)

Aggregations

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