Skip to content

Commit 817e37b

Browse files
authored
Merge pull request #52 from yma96/1.1.x
Add cleanupBCWorkspace when generation error
2 parents db204c2 + 205522d commit 817e37b

File tree

1 file changed

+49
-27
lines changed

1 file changed

+49
-27
lines changed

src/main/java/org/commonjava/indy/service/archive/controller/ArchiveController.java

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,17 @@ public void generate( HistoricalContentDTO content )
175175
try
176176
{
177177
doGenerate( content );
178+
recordCompleted( buildConfigId );
178179
}
179180
catch ( Exception e )
180181
{
181-
// If error happens on generation, remove the status to make sure coming generation
182+
// If error happens on generation, remove the status to make sure following generation
182183
removeStatus( buildConfigId );
184+
cleanupBCWorkspace( buildConfigId );
183185
logger.error( "Generation failed for buildConfigId {}", buildConfigId, e );
184186
}
185187
finally
186188
{
187-
recordCompleted( buildConfigId );
188189
buildConfigLocks.remove( buildConfigId );
189190
logger.info( "lock released, buildConfigId {}", buildConfigId );
190191
}
@@ -195,25 +196,27 @@ public void generate( HistoricalContentDTO content )
195196
}
196197
catch ( TimeoutException e )
197198
{
198-
// If timeout happens on generation, cancel and remove the status to make sure coming generation
199+
// If timeout happens on generation, cancel and remove the status to make sure following generation
199200
future.cancel( true );
200201
removeStatus( buildConfigId );
202+
cleanupBCWorkspace( buildConfigId );
201203
logger.error( "Generation timeout for buildConfigId {}", buildConfigId, e );
202204
}
203205
catch ( InterruptedException | ExecutionException e )
204206
{
205-
// If future task level error happens on generation, cancel and remove the status to make sure coming generation
207+
// If future task level error happens on generation, cancel and remove the status to make sure following generation
206208
future.cancel( true );
207209
removeStatus( buildConfigId );
210+
cleanupBCWorkspace( buildConfigId );
208211
logger.error( "Generation future task level failed for buildConfigId {}", buildConfigId, e );
209212
}
210213
}
211214
}
212215

213216
protected Boolean doGenerate( HistoricalContentDTO content )
214217
{
215-
logger.info( "Handle generate event: {}, build config id: {}", EVENT_GENERATE_ARCHIVE,
216-
content.getBuildConfigId() );
218+
String buildConfigId = content.getBuildConfigId();
219+
logger.info( "Handle generate event: {}, build config id: {}", EVENT_GENERATE_ARCHIVE, buildConfigId );
217220

218221
Map<String, HistoricalEntryDTO> entryDTOs = reader.readEntries( content );
219222
Map<String, String> downloadPaths = new HashMap<>();
@@ -227,26 +230,24 @@ protected Boolean doGenerate( HistoricalContentDTO content )
227230
}
228231
catch ( final InterruptedException e )
229232
{
230-
logger.error( "Artifacts downloading is interrupted, build config id: " + content.getBuildConfigId(), e );
233+
logger.error( "Artifacts downloading is interrupted, build config id: " + buildConfigId, e );
231234
return false;
232235
}
233236
catch ( final ExecutionException e )
234237
{
235-
logger.error( "Artifacts download execution manager failed, build config id: " + content.getBuildConfigId(),
236-
e );
238+
logger.error( "Artifacts download execution manager failed, build config id: " + buildConfigId, e );
237239
return false;
238240
}
239241
catch ( final IOException e )
240242
{
241-
logger.error( "Failed to generate historical archive from content, build config id: "
242-
+ content.getBuildConfigId(), e );
243+
logger.error( "Failed to generate historical archive from content, build config id: " + buildConfigId, e );
243244
return false;
244245
}
245246

246247
boolean created = false;
247248
if ( archive.isPresent() && archive.get().exists() )
248249
{
249-
created = renderArchive( archive.get(), content.getBuildConfigId() );
250+
created = renderArchive( archive.get(), buildConfigId );
250251
}
251252

252253
return created;
@@ -269,7 +270,7 @@ public void deleteArchive( final String buildConfigId )
269270
File zip = new File( archiveDir, buildConfigId + ARCHIVE_SUFFIX );
270271
if ( zip.exists() )
271272
{
272-
zip.delete();
273+
Files.delete( zip.toPath() );
273274
}
274275
logger.info( "Historical archive for build config id: {} is deleted.", buildConfigId );
275276
}
@@ -292,14 +293,14 @@ public void deleteArchiveWithChecksum( final String buildConfigId, final String
292293
// only delete the zip once checksum is matched
293294
if ( storedChecksum.equals( checksum ) )
294295
{
295-
zip.delete();
296+
Files.delete( zip.toPath() );
296297
logger.info( "Historical archive for build config id: {} is deleted, checksum {}.", buildConfigId,
297298
storedChecksum );
298299
}
299300
else
300301
{
301-
logger.info( "Don't delete the {} zip, transferred checksum {}, but stored checksum {}.",
302-
buildConfigId, checksum, storedChecksum );
302+
logger.info( "Don't delete the {} zip, transferred checksum {}, but stored checksum {}.", buildConfigId,
303+
checksum, storedChecksum );
303304
}
304305
}
305306
}
@@ -314,7 +315,27 @@ public void cleanup()
314315
artifact.delete();
315316
}
316317
dir.delete();
317-
logger.info( "Temporary workplace cleanup is finished." );
318+
logger.info( "Temporary workplace {} cleanup is finished.", contentDir );
319+
}
320+
321+
public void cleanupBCWorkspace( String buildConfigId )
322+
{
323+
String path = String.format( "%s/%s", contentDir, buildConfigId );
324+
try
325+
{
326+
File dir = new File( path );
327+
List<File> artifacts = walkAllFiles( path );
328+
for ( File artifact : artifacts )
329+
{
330+
artifact.delete();
331+
}
332+
dir.delete();
333+
logger.info( "Temporary workplace {} cleanup is finished.", path );
334+
}
335+
catch ( IOException e )
336+
{
337+
logger.error( "Failed to walk files on path {}", path, e );
338+
}
318339
}
319340

320341
public boolean statusExists( final String buildConfigId )
@@ -340,15 +361,16 @@ private void downloadArtifacts( final Map<String, HistoricalEntryDTO> entryDTOs,
340361
BasicCookieStore cookieStore = new BasicCookieStore();
341362
ExecutorCompletionService<Boolean> executor = new ExecutorCompletionService<>( downloadExecutor );
342363

343-
String contentBuildDir = String.format( "%s/%s", contentDir, content.getBuildConfigId() );
344-
File dir = new File( contentBuildDir );
345-
dir.delete();
364+
String buildConfigId = content.getBuildConfigId();
365+
String contentBuildDir = String.format( "%s/%s", contentDir, buildConfigId );
366+
// cleanup the build config content dir before download
367+
cleanupBCWorkspace( buildConfigId );
346368

347-
HistoricalContentDTO originalTracked = unpackHistoricalArchive( contentBuildDir, content.getBuildConfigId() );
369+
HistoricalContentDTO originalTracked = unpackHistoricalArchive( contentBuildDir, buildConfigId );
348370
Map<String, List<String>> originalChecksumsMap = new HashMap<>();
349371
if ( originalTracked != null )
350372
{
351-
logger.trace( "originalChecksumsMap generated for {}", content.getBuildConfigId() );
373+
logger.trace( "originalChecksumsMap generated for {}", buildConfigId );
352374
Map<String, HistoricalEntryDTO> originalEntries = reader.readEntries( originalTracked );
353375
originalEntries.forEach( ( key, entry ) -> originalChecksumsMap.put( key, new ArrayList<>(
354376
Arrays.asList( entry.getSha1(), entry.getSha256(), entry.getMd5() ) ) ) );
@@ -384,21 +406,22 @@ private void downloadArtifacts( final Map<String, HistoricalEntryDTO> entryDTOs,
384406
private Optional<File> generateArchive( final List<String> paths, final HistoricalContentDTO content )
385407
throws IOException
386408
{
387-
String contentBuildDir = String.format( "%s/%s", contentDir, content.getBuildConfigId() );
409+
String buildConfigId = content.getBuildConfigId();
410+
String contentBuildDir = String.format( "%s/%s", contentDir, buildConfigId );
388411
File dir = new File( contentBuildDir );
389412
if ( !dir.exists() )
390413
{
391414
return Optional.empty();
392415
}
393416

394-
final File part = new File( archiveDir, content.getBuildConfigId() + PART_ARCHIVE_SUFFIX );
417+
final File part = new File( archiveDir, buildConfigId + PART_ARCHIVE_SUFFIX );
395418
part.getParentFile().mkdirs();
396419

397420
logger.info( "Writing archive to: '{}'", part.getAbsolutePath() );
398421
ZipOutputStream zip = new ZipOutputStream( new FileOutputStream( part ) );
399422

400423
// adding tracked file
401-
paths.add( content.getBuildConfigId() );
424+
paths.add( buildConfigId );
402425
byte[] buffer = new byte[1024];
403426
for ( String path : paths )
404427
{
@@ -448,8 +471,7 @@ private boolean renderArchive( File part, final String buildConfigId )
448471
}
449472
catch ( final SecurityException | IOException e )
450473
{
451-
e.printStackTrace();
452-
logger.error( "Failed to delete the obsolete archive file {}", target.getPath() );
474+
logger.error( "Failed to delete the obsolete archive file {}", target.getPath(), e );
453475
return false;
454476
}
455477
target.getParentFile().mkdirs();

0 commit comments

Comments
 (0)