17
17
import java .util .List ;
18
18
import java .util .concurrent .TimeUnit ;
19
19
import java .util .concurrent .locks .ReentrantReadWriteLock ;
20
+ import java .util .logging .Level ;
21
+ import java .util .logging .Logger ;
22
+
20
23
import org .apache .commons .io .IOUtils ;
21
24
import org .apache .commons .lang .StringUtils ;
22
25
23
26
public final class LibraryCachingConfiguration extends AbstractDescribableImpl <LibraryCachingConfiguration > {
27
+
28
+ private static final Logger LOGGER = Logger .getLogger (LibraryCachingConfiguration .class .getName ());
29
+
24
30
private int refreshTimeMinutes ;
25
31
private String excludedVersionsStr ;
26
32
@@ -83,7 +89,7 @@ public static FilePath getGlobalLibrariesCacheDir() {
83
89
}
84
90
85
91
@ Extension public static class DescriptorImpl extends Descriptor <LibraryCachingConfiguration > {
86
- public FormValidation doClearCache (@ QueryParameter String name ) throws InterruptedException {
92
+ public FormValidation doClearCache (@ QueryParameter String name , @ QueryParameter boolean forceDelete ) throws InterruptedException {
87
93
Jenkins .get ().checkPermission (Jenkins .ADMINISTER );
88
94
89
95
try {
@@ -94,19 +100,26 @@ public FormValidation doClearCache(@QueryParameter String name) throws Interrupt
94
100
try (InputStream stream = libraryNamePath .read ()) {
95
101
cacheName = IOUtils .toString (stream , StandardCharsets .UTF_8 );
96
102
}
97
- if (libraryNamePath . readToString () .equals (name )) {
103
+ if (cacheName .equals (name )) {
98
104
FilePath libraryCachePath = LibraryCachingConfiguration .getGlobalLibrariesCacheDir ()
99
105
.child (libraryNamePath .getName ().replace ("-name.txt" , "" ));
100
- ReentrantReadWriteLock retrieveLock = LibraryAdder .getReadWriteLockFor (libraryCachePath .getName ());
101
- if (retrieveLock .writeLock ().tryLock (10 , TimeUnit .SECONDS )) {
102
- try {
103
- libraryCachePath .deleteRecursive ();
104
- libraryNamePath .delete ();
105
- } finally {
106
- retrieveLock .writeLock ().unlock ();
107
- }
106
+ if (forceDelete ) {
107
+ LOGGER .log (Level .FINER , "Force deleting cache for {0}" , name );
108
+ libraryCachePath .deleteRecursive ();
109
+ libraryNamePath .delete ();
108
110
} else {
109
- return FormValidation .error ("The cache dir is currently used by another thread, so deletion was not possibly. Please try again" );
111
+ LOGGER .log (Level .FINER , "Safe deleting cache for {0}" , name );
112
+ ReentrantReadWriteLock retrieveLock = LibraryAdder .getReadWriteLockFor (libraryCachePath .getName ());
113
+ if (retrieveLock .writeLock ().tryLock (10 , TimeUnit .SECONDS )) {
114
+ try {
115
+ libraryCachePath .deleteRecursive ();
116
+ libraryNamePath .delete ();
117
+ } finally {
118
+ retrieveLock .writeLock ().unlock ();
119
+ }
120
+ } else {
121
+ return FormValidation .error ("The cache dir could not be deleted because it is currently being used by another thread. Please try again." );
122
+ }
110
123
}
111
124
}
112
125
}
0 commit comments