Search in sources :

Example 1 with ResolvedTaskOutputFilePropertySpec

use of org.gradle.api.internal.tasks.ResolvedTaskOutputFilePropertySpec in project gradle by gradle.

the class SkipCachedTaskExecuter method resolveProperties.

private static SortedSet<ResolvedTaskOutputFilePropertySpec> resolveProperties(ImmutableSortedSet<? extends TaskOutputFilePropertySpec> properties) {
    ImmutableSortedSet.Builder<ResolvedTaskOutputFilePropertySpec> builder = ImmutableSortedSet.naturalOrder();
    for (TaskOutputFilePropertySpec property : properties) {
        // At this point we assume that the task only has cacheable properties,
        // otherwise caching would have been disabled by now
        CacheableTaskOutputFilePropertySpec cacheableProperty = (CacheableTaskOutputFilePropertySpec) property;
        builder.add(new ResolvedTaskOutputFilePropertySpec(cacheableProperty.getPropertyName(), cacheableProperty.getOutputType(), cacheableProperty.getOutputFile()));
    }
    return builder.build();
}
Also used : CacheableTaskOutputFilePropertySpec(org.gradle.api.internal.tasks.CacheableTaskOutputFilePropertySpec) ResolvedTaskOutputFilePropertySpec(org.gradle.api.internal.tasks.ResolvedTaskOutputFilePropertySpec) TaskOutputFilePropertySpec(org.gradle.api.internal.tasks.TaskOutputFilePropertySpec) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ResolvedTaskOutputFilePropertySpec(org.gradle.api.internal.tasks.ResolvedTaskOutputFilePropertySpec) CacheableTaskOutputFilePropertySpec(org.gradle.api.internal.tasks.CacheableTaskOutputFilePropertySpec)

Example 2 with ResolvedTaskOutputFilePropertySpec

use of org.gradle.api.internal.tasks.ResolvedTaskOutputFilePropertySpec in project gradle by gradle.

the class SkipCachedTaskExecuter method execute.

@Override
public void execute(final TaskInternal task, TaskStateInternal state, TaskExecutionContext context) {
    LOGGER.debug("Determining if {} is cached already", task);
    TaskProperties taskProperties = context.getTaskProperties();
    TaskOutputCachingBuildCacheKey cacheKey = context.getBuildCacheKey();
    boolean taskOutputCachingEnabled = state.getTaskOutputCaching().isEnabled();
    SortedSet<ResolvedTaskOutputFilePropertySpec> outputProperties = null;
    if (taskOutputCachingEnabled) {
        if (task.isHasCustomActions()) {
            LOGGER.info("Custom actions are attached to {}.", task);
        }
        if (cacheKey.isValid()) {
            TaskArtifactState taskState = context.getTaskArtifactState();
            // TODO: This is really something we should do at an earlier/higher level so that the input and output
            // property values are locked in at this point.
            outputProperties = resolveProperties(taskProperties.getOutputFileProperties());
            if (taskState.isAllowedToUseCachedResults()) {
                try {
                    OriginTaskExecutionMetadata originMetadata = buildCache.load(buildCacheCommandFactory.createLoad(cacheKey, outputProperties, task, taskProperties, taskOutputChangesListener, taskState));
                    if (originMetadata != null) {
                        state.setOutcome(TaskExecutionOutcome.FROM_CACHE);
                        context.setOriginExecutionMetadata(originMetadata);
                        return;
                    }
                } catch (UnrecoverableTaskOutputUnpackingException e) {
                    // garbage among the task's outputs, thus we must fail the build
                    throw e;
                } catch (Exception e) {
                    // There was a failure during downloading, previous task outputs should bu unaffected
                    LOGGER.warn("Failed to load cache entry for {}, falling back to executing task", task, e);
                }
            } 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) {
                try {
                    TaskArtifactState taskState = context.getTaskArtifactState();
                    Map<String, Map<String, FileContentSnapshot>> outputSnapshots = taskState.getOutputContentSnapshots();
                    buildCache.store(buildCacheCommandFactory.createStore(cacheKey, outputProperties, outputSnapshots, task, context.getExecutionTime()));
                } catch (Exception e) {
                    LOGGER.warn("Failed to store cache entry {}", cacheKey.getDisplayName(), task, e);
                }
            } 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 : TaskArtifactState(org.gradle.api.internal.changedetection.TaskArtifactState) OriginTaskExecutionMetadata(org.gradle.api.internal.tasks.OriginTaskExecutionMetadata) TaskOutputCachingBuildCacheKey(org.gradle.caching.internal.tasks.TaskOutputCachingBuildCacheKey) ResolvedTaskOutputFilePropertySpec(org.gradle.api.internal.tasks.ResolvedTaskOutputFilePropertySpec) Map(java.util.Map) UnrecoverableTaskOutputUnpackingException(org.gradle.caching.internal.tasks.UnrecoverableTaskOutputUnpackingException) UnrecoverableTaskOutputUnpackingException(org.gradle.caching.internal.tasks.UnrecoverableTaskOutputUnpackingException)

Example 3 with ResolvedTaskOutputFilePropertySpec

use of org.gradle.api.internal.tasks.ResolvedTaskOutputFilePropertySpec in project gradle by gradle.

the class TarTaskOutputPacker method pack.

private long pack(Collection<ResolvedTaskOutputFilePropertySpec> propertySpecs, Map<String, Map<String, FileContentSnapshot>> outputSnapshots, TarArchiveOutputStream tarOutput) {
    long entries = 0;
    for (ResolvedTaskOutputFilePropertySpec propertySpec : propertySpecs) {
        String propertyName = propertySpec.getPropertyName();
        Map<String, FileContentSnapshot> outputs = outputSnapshots.get(propertyName);
        try {
            entries += packProperty(propertySpec, outputs, tarOutput);
        } catch (Exception ex) {
            throw new GradleException(String.format("Could not pack property '%s': %s", propertyName, ex.getMessage()), ex);
        }
    }
    return entries;
}
Also used : FileContentSnapshot(org.gradle.api.internal.changedetection.state.FileContentSnapshot) GradleException(org.gradle.api.GradleException) ResolvedTaskOutputFilePropertySpec(org.gradle.api.internal.tasks.ResolvedTaskOutputFilePropertySpec) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) GradleException(org.gradle.api.GradleException)

Example 4 with ResolvedTaskOutputFilePropertySpec

use of org.gradle.api.internal.tasks.ResolvedTaskOutputFilePropertySpec in project gradle by gradle.

the class TarTaskOutputPacker method unpack.

private UnpackResult unpack(SortedSet<ResolvedTaskOutputFilePropertySpec> propertySpecs, TarArchiveInputStream tarInput, TaskOutputOriginReader readOriginAction) throws IOException {
    Map<String, ResolvedTaskOutputFilePropertySpec> propertySpecsMap = Maps.uniqueIndex(propertySpecs, new Function<TaskFilePropertySpec, String>() {

        @Override
        public String apply(TaskFilePropertySpec propertySpec) {
            return propertySpec.getPropertyName();
        }
    });
    TarArchiveEntry tarEntry;
    OriginTaskExecutionMetadata originMetadata = null;
    ImmutableListMultimap.Builder<String, FileSnapshot> propertyFileSnapshots = ImmutableListMultimap.builder();
    long entries = 0;
    while ((tarEntry = tarInput.getNextTarEntry()) != null) {
        ++entries;
        String path = tarEntry.getName();
        if (path.equals(METADATA_PATH)) {
            // handle origin metadata
            originMetadata = readOriginAction.execute(new CloseShieldInputStream(tarInput));
        } else {
            // handle output property
            Matcher matcher = PROPERTY_PATH.matcher(path);
            if (!matcher.matches()) {
                throw new IllegalStateException("Cached result format error, invalid contents: " + path);
            }
            String propertyName = unescape(matcher.group(2));
            ResolvedTaskOutputFilePropertySpec propertySpec = propertySpecsMap.get(propertyName);
            if (propertySpec == null) {
                throw new IllegalStateException(String.format("No output property '%s' registered", propertyName));
            }
            boolean outputMissing = matcher.group(1) != null;
            String childPath = matcher.group(3);
            unpackPropertyEntry(propertySpec, tarInput, tarEntry, childPath, outputMissing, propertyFileSnapshots);
        }
    }
    if (originMetadata == null) {
        throw new IllegalStateException("Cached result format error, no origin metadata was found.");
    }
    return new UnpackResult(originMetadata, entries, propertyFileSnapshots.build());
}
Also used : Matcher(java.util.regex.Matcher) TaskFilePropertySpec(org.gradle.api.internal.tasks.TaskFilePropertySpec) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) DirectoryFileSnapshot(org.gradle.api.internal.changedetection.state.DirectoryFileSnapshot) FileSnapshot(org.gradle.api.internal.changedetection.state.FileSnapshot) RegularFileSnapshot(org.gradle.api.internal.changedetection.state.RegularFileSnapshot) OriginTaskExecutionMetadata(org.gradle.api.internal.tasks.OriginTaskExecutionMetadata) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ResolvedTaskOutputFilePropertySpec(org.gradle.api.internal.tasks.ResolvedTaskOutputFilePropertySpec) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream)

Aggregations

ResolvedTaskOutputFilePropertySpec (org.gradle.api.internal.tasks.ResolvedTaskOutputFilePropertySpec)4 OriginTaskExecutionMetadata (org.gradle.api.internal.tasks.OriginTaskExecutionMetadata)2 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)1 CloseShieldInputStream (org.apache.commons.io.input.CloseShieldInputStream)1 GradleException (org.gradle.api.GradleException)1 TaskArtifactState (org.gradle.api.internal.changedetection.TaskArtifactState)1 DirectoryFileSnapshot (org.gradle.api.internal.changedetection.state.DirectoryFileSnapshot)1 FileContentSnapshot (org.gradle.api.internal.changedetection.state.FileContentSnapshot)1 FileSnapshot (org.gradle.api.internal.changedetection.state.FileSnapshot)1 RegularFileSnapshot (org.gradle.api.internal.changedetection.state.RegularFileSnapshot)1 CacheableTaskOutputFilePropertySpec (org.gradle.api.internal.tasks.CacheableTaskOutputFilePropertySpec)1 TaskFilePropertySpec (org.gradle.api.internal.tasks.TaskFilePropertySpec)1 TaskOutputFilePropertySpec (org.gradle.api.internal.tasks.TaskOutputFilePropertySpec)1 TaskOutputCachingBuildCacheKey (org.gradle.caching.internal.tasks.TaskOutputCachingBuildCacheKey)1