Skip to content

Commit

Permalink
Merge pull request #160 from jenkinsci/feature/155-option-to-silently…
Browse files Browse the repository at this point in the history
…-ignore-cache-failures

Do not fail build in case of error while creating cache
  • Loading branch information
repolevedavaj committed Feb 22, 2023
2 parents 5753302 + 929649e commit 2290747
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/main/java/jenkins/plugins/jobcacher/ArbitraryFileCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,13 @@ public Saver cache(ObjectPath cachesRoot, ObjectPath fallbackCachesRoot, Run<?,

try {
existingCache.restore(resolvedPath, workspace);

long cacheRestorationEndTime = System.nanoTime();
logMessage("Cache restored in " + Duration.ofNanos(cacheRestorationEndTime - cacheRestorationStartTime).toMillis() + "ms", listener);
} catch (Exception e) {
logMessage("Failed to restore cache, cleaning up " + path + "...", listener);
logMessage("Failed to restore cache, cleaning up " + path + "...", e, listener);
resolvedPath.deleteRecursive();
}
long cacheRestorationEndTime = System.nanoTime();
logMessage("Cache restored in " + Duration.ofNanos(cacheRestorationEndTime - cacheRestorationStartTime).toMillis() + "ms", listener);

return new SaverImpl(resolvedPath);
}
Expand Down Expand Up @@ -367,12 +368,17 @@ public void save(ObjectPath cachesRoot, Run<?, ?> build, FilePath workspace, Lau

logMessage("Creating cache...", listener);
long cacheCreationStartTime = System.nanoTime();
compressionMethod.getCacheStrategy().cache(resolvedPath, includes, excludes, useDefaultExcludes, cache, workspace);
if (isCacheValidityDecidingFileConfigured() && isOneCacheValidityDecidingFilePresent(workspace)) {
updateSkipCacheTriggerFileHash(cachesRoot, workspace);

try {
compressionMethod.getCacheStrategy().cache(resolvedPath, includes, excludes, useDefaultExcludes, cache, workspace);
if (isCacheValidityDecidingFileConfigured() && isOneCacheValidityDecidingFilePresent(workspace)) {
updateSkipCacheTriggerFileHash(cachesRoot, workspace);
}
long cacheCreationEndTime = System.nanoTime();
logMessage("Cache created in " + Duration.ofNanos(cacheCreationEndTime - cacheCreationStartTime).toMillis() + "ms", listener);
} catch (Exception e) {
logMessage("Failed to create cache", e, listener);
}
long cacheCreationEndTime = System.nanoTime();
logMessage("Cache created in " + Duration.ofNanos(cacheCreationEndTime - cacheCreationStartTime).toMillis() + "ms", listener);
}

private boolean isPathOutsideWorkspace(FilePath workspace) {
Expand All @@ -393,6 +399,11 @@ private void updateSkipCacheTriggerFileHash(ObjectPath cachesRoot, FilePath work
}
}

private void logMessage(String message, Exception exception, TaskListener listener) {
logMessage(message, listener);
exception.printStackTrace(listener.getLogger());
}

private void logMessage(String message, TaskListener listener) {
String cacheIdentifier = path;
if (getCacheName() != null) {
Expand Down

0 comments on commit 2290747

Please sign in to comment.