3434import java .io .File ;
3535import java .io .IOException ;
3636import java .io .InputStream ;
37+ import java .net .MalformedURLException ;
3738import java .net .URL ;
3839import java .util .ArrayList ;
3940import java .util .Base64 ;
133134 if (cfg instanceof LibraryResolver .ResolvedLibraryConfiguration ) {
134135 source = ((LibraryResolver .ResolvedLibraryConfiguration ) cfg ).getSource ();
135136 }
136- librariesAdded .put (name , new LibraryRecord (name , version , kindTrusted , changelog , cfg .getCachingConfiguration (), source ));
137- retrievers .put (name , cfg .getRetriever ());
137+ LibraryRetriever retriever = cfg .getRetriever ();
138+ String libraryPath = null ;
139+ if (retriever instanceof SCMBasedRetriever ) {
140+ libraryPath = ((SCMBasedRetriever ) retriever ).getLibraryPath ();
141+ }
142+ librariesAdded .put (name , new LibraryRecord (name , version , kindTrusted , changelog , cfg .getCachingConfiguration (), source , libraryPath ));
143+ retrievers .put (name , retriever );
138144 }
139145 }
140146 for (String name : librariesAdded .keySet ()) {
147153 build .addAction (new LibrariesAction (new ArrayList <>(librariesAdded .values ())));
148154 // Now actually try to retrieve the libraries.
149155 for (LibraryRecord record : librariesAdded .values ()) {
150- listener .getLogger ().println ("Loading library " + record .name + "@" + record . version );
156+ listener .getLogger ().println ("Loading library " + record .getLogString () );
151157 for (URL u : retrieve (record , retrievers .get (record .name ), listener , build , execution )) {
152158 additions .add (new Addition (u , record .trusted ));
153159 }
166172
167173 private enum CacheStatus {
168174 VALID ,
175+ EMPTY ,
169176 DOES_NOT_EXIST ,
170177 EXPIRED ;
171178 }
172-
179+
173180 private static CacheStatus getCacheStatus (@ NonNull LibraryCachingConfiguration cachingConfiguration , @ NonNull final FilePath versionCacheDir )
174181 throws IOException , InterruptedException
175182 {
@@ -178,6 +185,9 @@ private static CacheStatus getCacheStatus(@NonNull LibraryCachingConfiguration c
178185
179186 if (versionCacheDir .exists ()) {
180187 if ((versionCacheDir .lastModified () + cachingMilliseconds ) > System .currentTimeMillis ()) {
188+ if (getUrlsForLibDir (versionCacheDir ).isEmpty ()) {
189+ return CacheStatus .EMPTY ;
190+ }
181191 return CacheStatus .VALID ;
182192 } else {
183193 return CacheStatus .EXPIRED ;
@@ -187,6 +197,9 @@ private static CacheStatus getCacheStatus(@NonNull LibraryCachingConfiguration c
187197 }
188198 } else {
189199 if (versionCacheDir .exists ()) {
200+ if (getUrlsForLibDir (versionCacheDir ).isEmpty ()) {
201+ return CacheStatus .EMPTY ;
202+ }
190203 return CacheStatus .VALID ;
191204 } else {
192205 return CacheStatus .DOES_NOT_EXIST ;
@@ -199,6 +212,7 @@ static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriev
199212 String name = record .name ;
200213 String version = record .version ;
201214 boolean changelog = record .changelog ;
215+ String libraryLogString = record .getLogString ();
202216 LibraryCachingConfiguration cachingConfiguration = record .cachingConfiguration ;
203217 FilePath libDir = new FilePath (execution .getOwner ().getRootDir ()).child ("libs/" + record .getDirectoryName ());
204218 Boolean shouldCache = cachingConfiguration != null ;
@@ -207,7 +221,7 @@ static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriev
207221 final FilePath lastReadFile = new FilePath (versionCacheDir , LibraryCachingConfiguration .LAST_READ_FILE );
208222
209223 if (shouldCache && cachingConfiguration .isExcluded (version )) {
210- listener .getLogger ().println ("Library " + name + "@" + version + " is excluded from caching." );
224+ listener .getLogger ().println ("Library " + libraryLogString + " is excluded from caching." );
211225 shouldCache = false ;
212226 }
213227
@@ -218,38 +232,51 @@ static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriev
218232 retrieveLock .readLock ().lockInterruptibly ();
219233 try {
220234 CacheStatus cacheStatus = getCacheStatus (cachingConfiguration , versionCacheDir );
221- if (cacheStatus == CacheStatus .DOES_NOT_EXIST || cacheStatus == CacheStatus .EXPIRED ) {
235+ if (cacheStatus == CacheStatus .DOES_NOT_EXIST || cacheStatus == CacheStatus .EXPIRED || cacheStatus == CacheStatus . EMPTY ) {
222236 retrieveLock .readLock ().unlock ();
223237 retrieveLock .writeLock ().lockInterruptibly ();
224238 try {
225239 boolean retrieve = false ;
226240 switch (getCacheStatus (cachingConfiguration , versionCacheDir )) {
227241 case VALID :
228- listener .getLogger ().println ("Library " + name + "@" + version + " is cached. Copying from home." );
229- break ;
242+ listener .getLogger ().println ("Library " + libraryLogString + " is cached. Copying from home." );
243+ break ;
244+ case EMPTY :
245+ listener .getLogger ().println ("Library " + libraryLogString + " should have been cached but is empty, re-caching." );
246+ deleteCacheDirIfExists (versionCacheDir );
247+ retrieve = true ;
248+ break ;
230249 case DOES_NOT_EXIST :
231250 retrieve = true ;
232251 break ;
233252 case EXPIRED :
234253 long cachingMinutes = cachingConfiguration .getRefreshTimeMinutes ();
235- listener .getLogger ().println ("Library " + name + "@" + version + " is due for a refresh after " + cachingMinutes + " minutes, clearing." );
236- if (versionCacheDir .exists ()) {
237- versionCacheDir .deleteRecursive ();
238- versionCacheDir .withSuffix ("-name.txt" ).delete ();
239- }
240- retrieve = true ;
241- break ;
254+ listener .getLogger ().println ("Library " + libraryLogString + " is due for a refresh after " + cachingMinutes + " minutes, clearing." );
255+ deleteCacheDirIfExists (versionCacheDir );
256+ retrieve = true ;
257+ break ;
242258 }
243259
244260 if (retrieve ) {
245- listener .getLogger ().println ("Caching library " + name + "@" + version );
261+ listener .getLogger ().println ("Caching library " + libraryLogString );
246262 versionCacheDir .mkdirs ();
247263 // try to retrieve the library and delete the versionCacheDir if it fails
248264 try {
249265 retriever .retrieve (name , version , changelog , versionCacheDir , run , listener );
266+ if (getUrlsForLibDir (versionCacheDir ).isEmpty ()) {
267+ // Get job name and build number from run
268+ String jobName = run .getParent ().getFullName ();
269+ String message = "Library " + libraryLogString + " is empty after retrieval in job " + jobName + ". Cleaning up cache directory." ;
270+ listener .getLogger ().println (message );
271+ // Log a warning at controller level as well
272+ LOGGER .log (Level .WARNING , message );
273+ deleteCacheDirIfExists (versionCacheDir );
274+ throw new AbortException ("Library " + libraryLogString + " is empty." );
275+ }
276+ listener .getLogger ().println ("Library " + libraryLogString + " successfully cached." );
250277 } catch (Exception e ) {
251- listener .getLogger ().println ("Failed to cache library " + name + "@" + version + ". Error message: " + e .getMessage () + ". Cleaning up cache directory." );
252- versionCacheDir . deleteRecursive ( );
278+ listener .getLogger ().println ("Failed to cache library " + libraryLogString + ". Error message: " + e .getMessage () + ". Cleaning up cache directory." );
279+ deleteCacheDirIfExists ( versionCacheDir );
253280 throw e ;
254281 }
255282 }
@@ -258,7 +285,7 @@ static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriev
258285 retrieveLock .writeLock ().unlock ();
259286 }
260287 } else {
261- listener .getLogger ().println ("Library " + name + "@" + version + " is cached. Copying from home." );
288+ listener .getLogger ().println ("Library " + libraryLogString + " is cached. Copying from home." );
262289 }
263290
264291 lastReadFile .touch (System .currentTimeMillis ());
@@ -289,6 +316,25 @@ static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriev
289316 }
290317 }
291318 }
319+ List <URL > urls = getUrlsForLibDir (libDir , record );
320+ if (urls .isEmpty ()) {
321+ throw new AbortException ("Library " + name + " expected to contain at least one of src or vars directories" );
322+ }
323+ return urls ;
324+ }
325+
326+ private static void deleteCacheDirIfExists (FilePath versionCacheDir ) throws IOException , InterruptedException {
327+ if (versionCacheDir .exists ()) {
328+ versionCacheDir .deleteRecursive ();
329+ versionCacheDir .withSuffix ("-name.txt" ).delete ();
330+ }
331+ }
332+
333+ private static List <URL > getUrlsForLibDir (FilePath libDir ) throws MalformedURLException , IOException , InterruptedException {
334+ return getUrlsForLibDir (libDir , null );
335+ }
336+
337+ private static List <URL > getUrlsForLibDir (FilePath libDir , LibraryRecord record ) throws MalformedURLException , IOException , InterruptedException {
292338 List <URL > urls = new ArrayList <>();
293339 FilePath srcDir = libDir .child ("src" );
294340 if (srcDir .isDirectory ()) {
@@ -297,13 +343,12 @@ static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriev
297343 FilePath varsDir = libDir .child ("vars" );
298344 if (varsDir .isDirectory ()) {
299345 urls .add (varsDir .toURI ().toURL ());
300- for (FilePath var : varsDir .list ("*.groovy" )) {
301- record .variables .add (var .getBaseName ());
346+ if (record != null ) {
347+ for (FilePath var : varsDir .list ("*.groovy" )) {
348+ record .variables .add (var .getBaseName ());
349+ }
302350 }
303351 }
304- if (urls .isEmpty ()) {
305- throw new AbortException ("Library " + name + " expected to contain at least one of src or vars directories" );
306- }
307352 return urls ;
308353 }
309354
0 commit comments