@@ -31,7 +31,6 @@ public final class LibraryCachingConfiguration extends AbstractDescribableImpl<L
31
31
private String excludedVersionsStr ;
32
32
private String includedVersionsStr ;
33
33
34
-
35
34
private static final String VERSIONS_SEPARATOR = " " ;
36
35
public static final String GLOBAL_LIBRARIES_DIR = "global-libraries-cache" ;
37
36
public static final String LAST_READ_FILE = "last_read" ;
@@ -40,10 +39,9 @@ public final class LibraryCachingConfiguration extends AbstractDescribableImpl<L
40
39
this .refreshTimeMinutes = refreshTimeMinutes ;
41
40
this .excludedVersionsStr = excludedVersionsStr ;
42
41
this .includedVersionsStr = includedVersionsStr ;
43
-
44
-
45
42
}
46
43
44
+
47
45
public int getRefreshTimeMinutes () {
48
46
return refreshTimeMinutes ;
49
47
}
@@ -59,10 +57,8 @@ public Boolean isRefreshEnabled() {
59
57
public String getExcludedVersionsStr () {
60
58
return excludedVersionsStr ;
61
59
}
60
+ public String getIncludedVersionsStr () { return includedVersionsStr ; }
62
61
63
- public String getIncludedVersionsStr () {
64
- return includedVersionsStr ;
65
- }
66
62
67
63
private List <String > getExcludedVersions () {
68
64
if (excludedVersionsStr == null ) {
@@ -121,61 +117,78 @@ public static FilePath getGlobalLibrariesCacheDir() {
121
117
}
122
118
123
119
@ Extension public static class DescriptorImpl extends Descriptor <LibraryCachingConfiguration > {
124
- public FormValidation doClearCache (@ QueryParameter String name , @ QueryParameter String cachedLibraryRef , @ QueryParameter boolean forceDelete ) throws InterruptedException {
120
+ public FormValidation doClearCache (@ QueryParameter String name , @ QueryParameter boolean forceDelete ) throws InterruptedException {
125
121
Jenkins .get ().checkPermission (Jenkins .ADMINISTER );
126
- String cacheDirName = null ;
122
+
127
123
try {
128
124
if (LibraryCachingConfiguration .getGlobalLibrariesCacheDir ().exists ()) {
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." );
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 ();
163
147
}
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." );
164
150
}
165
151
}
166
152
}
167
153
}
168
154
}
169
155
} catch (IOException ex ) {
170
- return FormValidation .error (ex , String .format ("The cache dir %s was not deleted successfully" , cacheDirName ));
171
- }
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 ));
156
+ return FormValidation .error (ex , "The cache dir was not deleted successfully" );
177
157
}
158
+ return FormValidation .ok ("The cache dir was deleted successfully." );
178
159
}
160
+ }
179
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
+ }
191
+ }
192
+ }
180
193
}
181
- }
194
+ }
0 commit comments