Skip to content

Commit df8dc07

Browse files
committed
Delete versionCacheDir if retrieve fails
1 parent 1759452 commit df8dc07

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryAdder.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@
6767
@Extension public class LibraryAdder extends ClasspathAdder {
6868

6969
private static final Logger LOGGER = Logger.getLogger(LibraryAdder.class.getName());
70-
70+
7171
private static ConcurrentHashMap<String, ReentrantReadWriteLock> cacheRetrieveLock = new ConcurrentHashMap<>();
7272

7373
static @NonNull ReentrantReadWriteLock getReadWriteLockFor(@NonNull String name) {
7474
return cacheRetrieveLock.computeIfAbsent(name, s -> new ReentrantReadWriteLock(true));
7575
}
76-
76+
7777
@Override public List<Addition> add(CpsFlowExecution execution, List<String> libraries, HashMap<String, Boolean> changelogs) throws Exception {
7878
Queue.Executable executable = execution.getOwner().getExecutable();
7979
Run<?,?> build;
@@ -156,7 +156,7 @@
156156
}
157157

158158
static @NonNull String[] parse(@NonNull String identifier) {
159-
int at = identifier.indexOf('@');
159+
int at = identifier.indexOf('@');
160160
if (at == -1) {
161161
return new String[] {identifier, null}; // pick up defaultVersion
162162
} else {
@@ -169,9 +169,9 @@ private enum CacheStatus {
169169
DOES_NOT_EXIST,
170170
EXPIRED;
171171
}
172-
172+
173173
private static CacheStatus getCacheStatus(@NonNull LibraryCachingConfiguration cachingConfiguration, @NonNull final FilePath versionCacheDir)
174-
throws IOException, InterruptedException
174+
throws IOException, InterruptedException
175175
{
176176
if (cachingConfiguration.isRefreshEnabled()) {
177177
final long cachingMilliseconds = cachingConfiguration.getRefreshTimeMilliseconds();
@@ -193,7 +193,7 @@ private static CacheStatus getCacheStatus(@NonNull LibraryCachingConfiguration c
193193
}
194194
}
195195
}
196-
196+
197197
/** Retrieve library files. */
198198
static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriever retriever, @NonNull TaskListener listener, @NonNull Run<?,?> run, @NonNull CpsFlowExecution execution) throws Exception {
199199
String name = record.name;
@@ -222,29 +222,36 @@ static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriev
222222
retrieveLock.readLock().unlock();
223223
retrieveLock.writeLock().lockInterruptibly();
224224
try {
225-
boolean retrieve = false;
226-
switch (getCacheStatus(cachingConfiguration, versionCacheDir)) {
227-
case VALID:
228-
listener.getLogger().println("Library " + name + "@" + version + " is cached. Copying from home.");
225+
boolean retrieve = false;
226+
switch (getCacheStatus(cachingConfiguration, versionCacheDir)) {
227+
case VALID:
228+
listener.getLogger().println("Library " + name + "@" + version + " is cached. Copying from home.");
229229
break;
230-
case DOES_NOT_EXIST:
231-
retrieve = true;
232-
break;
233-
case EXPIRED:
234-
long cachingMinutes = cachingConfiguration.getRefreshTimeMinutes();
235-
listener.getLogger().println("Library " + name + "@" + version + " is due for a refresh after " + cachingMinutes + " minutes, clearing.");
230+
case DOES_NOT_EXIST:
231+
retrieve = true;
232+
break;
233+
case EXPIRED:
234+
long cachingMinutes = cachingConfiguration.getRefreshTimeMinutes();
235+
listener.getLogger().println("Library " + name + "@" + version + " is due for a refresh after " + cachingMinutes + " minutes, clearing.");
236236
if (versionCacheDir.exists()) {
237237
versionCacheDir.deleteRecursive();
238238
versionCacheDir.withSuffix("-name.txt").delete();
239239
}
240240
retrieve = true;
241241
break;
242-
}
242+
}
243243

244244
if (retrieve) {
245245
listener.getLogger().println("Caching library " + name + "@" + version);
246246
versionCacheDir.mkdirs();
247-
retriever.retrieve(name, version, changelog, versionCacheDir, run, listener);
247+
// try to retrieve the library and delete the versionCacheDir if it fails
248+
try {
249+
retriever.retrieve(name, version, changelog, versionCacheDir, run, listener);
250+
} catch (Exception e) {
251+
listener.getLogger().println("Failed to cache library " + name + "@" + version + ". Error message: " + e.getMessage() + ". Cleaning up cache directory.");
252+
versionCacheDir.deleteRecursive();
253+
throw e;
254+
}
248255
}
249256
retrieveLock.readLock().lock();
250257
} finally {
@@ -258,7 +265,7 @@ static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriev
258265
versionCacheDir.withSuffix("-name.txt").write(name, "UTF-8");
259266
versionCacheDir.copyRecursiveTo(libDir);
260267
} finally {
261-
retrieveLock.readLock().unlock();
268+
retrieveLock.readLock().unlock();
262269
}
263270
} else {
264271
retriever.retrieve(name, version, changelog, libDir, run, listener);

0 commit comments

Comments
 (0)