@@ -31,6 +31,7 @@ public final class LibraryCachingConfiguration extends AbstractDescribableImpl<L
31
31
private String excludedVersionsStr ;
32
32
private String includedVersionsStr ;
33
33
34
+
34
35
private static final String VERSIONS_SEPARATOR = " " ;
35
36
public static final String GLOBAL_LIBRARIES_DIR = "global-libraries-cache" ;
36
37
public static final String LAST_READ_FILE = "last_read" ;
@@ -39,9 +40,10 @@ public final class LibraryCachingConfiguration extends AbstractDescribableImpl<L
39
40
this .refreshTimeMinutes = refreshTimeMinutes ;
40
41
this .excludedVersionsStr = excludedVersionsStr ;
41
42
this .includedVersionsStr = includedVersionsStr ;
42
- }
43
43
44
44
45
+ }
46
+
45
47
public int getRefreshTimeMinutes () {
46
48
return refreshTimeMinutes ;
47
49
}
@@ -57,8 +59,10 @@ public Boolean isRefreshEnabled() {
57
59
public String getExcludedVersionsStr () {
58
60
return excludedVersionsStr ;
59
61
}
60
- public String getIncludedVersionsStr () { return includedVersionsStr ; }
61
62
63
+ public String getIncludedVersionsStr () {
64
+ return includedVersionsStr ;
65
+ }
62
66
63
67
private List <String > getExcludedVersions () {
64
68
if (excludedVersionsStr == null ) {
@@ -117,78 +121,61 @@ public static FilePath getGlobalLibrariesCacheDir() {
117
121
}
118
122
119
123
@ Extension public static class DescriptorImpl extends Descriptor <LibraryCachingConfiguration > {
120
- public FormValidation doClearCache (@ QueryParameter String name , @ QueryParameter boolean forceDelete ) throws InterruptedException {
124
+ public FormValidation doClearCache (@ QueryParameter String name , @ QueryParameter String cachedLibraryRef , @ QueryParameter boolean forceDelete ) throws InterruptedException {
121
125
Jenkins .get ().checkPermission (Jenkins .ADMINISTER );
122
-
126
+ String cacheDirName = null ;
123
127
try {
124
128
if (LibraryCachingConfiguration .getGlobalLibrariesCacheDir ().exists ()) {
125
- for (FilePath libraryNamePath : LibraryCachingConfiguration .getGlobalLibrariesCacheDir ().list ("*-name.txt" )) {
126
- // Libraries configured in distinct locations may have the same name. Since only admins are allowed here, this is not a huge issue, but it is probably unexpected.
127
- String cacheName ;
128
- try (InputStream stream = libraryNamePath .read ()) {
129
- cacheName = IOUtils .toString (stream , StandardCharsets .UTF_8 );
130
- }
131
- if (cacheName .equals (name )) {
132
- FilePath libraryCachePath = LibraryCachingConfiguration .getGlobalLibrariesCacheDir ()
133
- .child (libraryNamePath .getName ().replace ("-name.txt" , "" ));
134
- if (forceDelete ) {
135
- LOGGER .log (Level .FINER , "Force deleting cache for {0}" , name );
136
- libraryCachePath .deleteRecursive ();
137
- libraryNamePath .delete ();
138
- } else {
139
- LOGGER .log (Level .FINER , "Safe deleting cache for {0}" , name );
140
- ReentrantReadWriteLock retrieveLock = LibraryAdder .getReadWriteLockFor (libraryCachePath .getName ());
141
- if (retrieveLock .writeLock ().tryLock (10 , TimeUnit .SECONDS )) {
142
- try {
143
- libraryCachePath .deleteRecursive ();
144
- libraryNamePath .delete ();
145
- } finally {
146
- retrieveLock .writeLock ().unlock ();
129
+ outer : for (FilePath libraryCache : LibraryCachingConfiguration .getGlobalLibrariesCacheDir ().listDirectories ()) {
130
+ for (FilePath libraryNamePath : libraryCache .list ("*-name.txt" )) {
131
+ if (libraryNamePath .readToString ().startsWith (name + "@" )) {
132
+ FilePath libraryCachePath = libraryNamePath .getParent ();
133
+ if (libraryCachePath != null ) {
134
+ FilePath versionCachePath = new FilePath (libraryCachePath , libraryNamePath .getName ().replace ("-name.txt" , "" ));
135
+ LOGGER .log (Level .FINER , "Safe deleting cache for {0}" , name );
136
+ ReentrantReadWriteLock retrieveLock = LibraryAdder .getReadWriteLockFor (libraryCachePath .getName ());
137
+ if (forceDelete || retrieveLock .writeLock ().tryLock (10 , TimeUnit .SECONDS )) {
138
+ if (forceDelete ) {
139
+ LOGGER .log (Level .FINER , "Force deleting cache for {0}" , name );
140
+ } else {
141
+ LOGGER .log (Level .FINER , "Safe deleting cache for {0}" , name );
142
+ }
143
+ try {
144
+ if (StringUtils .isNotEmpty (cachedLibraryRef )) {
145
+ if (libraryNamePath .readToString ().equals (name + "@" + cachedLibraryRef )) {
146
+ cacheDirName = name + "@" + cachedLibraryRef ;
147
+ libraryNamePath .delete ();
148
+ versionCachePath .deleteRecursive ();
149
+ break outer ;
150
+ }
151
+ } else {
152
+ cacheDirName = name ;
153
+ libraryCachePath .deleteRecursive ();
154
+ break outer ;
155
+ }
156
+ } finally {
157
+ if (!forceDelete ) {
158
+ retrieveLock .writeLock ().unlock ();
159
+ }
160
+ }
161
+ } else {
162
+ return FormValidation .error ("The cache dir could not be deleted because it is currently being used by another thread. Please try again." );
147
163
}
148
- } else {
149
- return FormValidation .error ("The cache dir could not be deleted because it is currently being used by another thread. Please try again." );
150
164
}
151
165
}
152
166
}
153
167
}
154
168
}
155
169
} catch (IOException ex ) {
156
- return FormValidation .error (ex , "The cache dir was not deleted successfully" );
170
+ return FormValidation .error (ex , String . format ( "The cache dir %s was not deleted successfully" , cacheDirName ) );
157
171
}
158
- return FormValidation .ok ("The cache dir was deleted successfully." );
159
- }
160
- }
161
-
162
- /**
163
- * Method can be called from an external source to delete cache of a particular version of the shared library
164
- * Example: delete cache from through pipeline script once the build is successful
165
- * @param jenkinsLibrary Name of the shared library
166
- * @param version Name of the version to delete the cache
167
- * @throws IOException
168
- * @throws InterruptedException
169
- */
170
- public static void deleteLibraryCacheForBranch (String jenkinsLibrary , String version ) throws IOException , InterruptedException {
171
- if (LibraryCachingConfiguration .getGlobalLibrariesCacheDir ().exists ()) {
172
- for (FilePath libraryNamePath : LibraryCachingConfiguration .getGlobalLibrariesCacheDir ().list ("*-name.txt" )) {
173
- String cacheName ;
174
- try (InputStream stream = libraryNamePath .read ()) {
175
- cacheName = IOUtils .toString (stream , StandardCharsets .UTF_8 );
176
-
177
- if (cacheName .equals (jenkinsLibrary )) {
178
- FilePath libraryCachePath = LibraryCachingConfiguration .getGlobalLibrariesCacheDir ()
179
- .child (libraryNamePath .getName ().replace ("-name.txt" , "" ));
180
- ReentrantReadWriteLock retrieveLock = LibraryAdder .getReadWriteLockFor (libraryCachePath .getName ());
181
- if (retrieveLock .writeLock ().tryLock (10 , TimeUnit .SECONDS )) {
182
- try {
183
- libraryCachePath .deleteRecursive ();
184
- libraryNamePath .delete ();
185
- } finally {
186
- retrieveLock .writeLock ().unlock ();
187
- }
188
- }
189
- }
190
- }
172
+
173
+ if (cacheDirName == null ) {
174
+ return FormValidation .ok (String .format ("The version %s was not found for library %s." , cachedLibraryRef , name ));
175
+ } else {
176
+ return FormValidation .ok (String .format ("The cache dir %s was deleted successfully." , cacheDirName ));
191
177
}
192
178
}
179
+
193
180
}
194
- }
181
+ }
0 commit comments