Search in sources :

Example 1 with BuildCacheEntryWriter

use of org.gradle.caching.BuildCacheEntryWriter 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 2 with BuildCacheEntryWriter

use of org.gradle.caching.BuildCacheEntryWriter in project gradle by gradle.

the class DispatchingBuildCacheService method pushToLocalAndRemote.

private void pushToLocalAndRemote(BuildCacheKey key, BuildCacheEntryWriter writer) {
    File destination = temporaryFileProvider.createTemporaryFile("gradle_cache", "entry");
    try {
        writeCacheEntryLocally(writer, destination);
        BuildCacheEntryWriter copier = new CopyBuildCacheEntryWriter(destination);
        local.store(key, copier);
        remote.store(key, copier);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        GFileUtils.deleteQuietly(destination);
    }
}
Also used : UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) File(java.io.File) BuildCacheEntryWriter(org.gradle.caching.BuildCacheEntryWriter)

Aggregations

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