@@ -107,12 +107,11 @@ public class SupportAction implements RootAction, StaplerProxy {
107
107
*/
108
108
private final Logger logger = Logger .getLogger (SupportAction .class .getName ());
109
109
110
- private static final String SUPPORT_BUNDLE_FILE_NAME = "support-bundle.zip" ;
111
110
private static final String SUPPORT_BUNDLE_CREATION_FOLDER = Paths .get (System .getProperty ("java.io.tmpdir" ))
112
111
.resolve ("support-bundle" )
113
112
.toString ();
114
113
115
- private static final Map <UUID , SupportBundleAsyncGenerator > generatorMap = new ConcurrentHashMap <>();
114
+ private static final Map <UUID , SupportBundleAsyncGenerator > generatorByTaskId = new ConcurrentHashMap <>();
116
115
117
116
@ Override
118
117
@ Restricted (NoExternalUse .class )
@@ -378,7 +377,7 @@ public HttpRedirect doGenerateAllBundlesAsync(StaplerRequest2 req, StaplerRespon
378
377
UUID taskId = UUID .randomUUID ();
379
378
SupportBundleAsyncGenerator supportBundleAsyncGenerator = new SupportBundleAsyncGenerator ();
380
379
supportBundleAsyncGenerator .init (taskId , components );
381
- generatorMap .put (taskId , supportBundleAsyncGenerator );
380
+ generatorByTaskId .put (taskId , supportBundleAsyncGenerator );
382
381
383
382
return new HttpRedirect ("progressPage?taskId=" + taskId );
384
383
}
@@ -506,8 +505,8 @@ public boolean isSelected() {
506
505
}
507
506
}
508
507
509
- public ProgressiveRendering getGenerateSupportBundle (@ QueryParameter String taskId ) throws Exception {
510
- return generatorMap .get (UUID .fromString (taskId ));
508
+ public ProgressiveRendering getGeneratorByTaskId (@ QueryParameter String taskId ) throws Exception {
509
+ return generatorByTaskId .get (UUID .fromString (taskId ));
511
510
}
512
511
513
512
public static class SupportBundleAsyncGenerator extends ProgressiveRendering {
@@ -517,6 +516,7 @@ public static class SupportBundleAsyncGenerator extends ProgressiveRendering {
517
516
private String pathToBundle ;
518
517
private List <Component > components ;
519
518
private boolean supportBundleGenerationInProgress = false ;
519
+ private String supportBundleName ;
520
520
521
521
public SupportBundleAsyncGenerator init (UUID taskId , List <Component > components ) {
522
522
this .taskId = taskId ;
@@ -526,36 +526,40 @@ public SupportBundleAsyncGenerator init(UUID taskId, List<Component> components)
526
526
527
527
@ Override
528
528
protected void compute () throws Exception {
529
- if (!supportBundleGenerationInProgress ) {
530
- this .supportBundleGenerationInProgress = true ;
531
- logger .info ("Generating support bundle... task id " + taskId );
532
- File outputDir = new File (SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId );
533
- if (!outputDir .exists ()) {
534
- if (!outputDir .mkdirs ()) {
535
- throw new IOException ("Failed to create directory: " + outputDir .getAbsolutePath ());
536
- }
529
+ if (supportBundleGenerationInProgress ) {
530
+ return ;
531
+ }
532
+ this .supportBundleName = BundleFileName .generate ();
533
+ this .supportBundleGenerationInProgress = true ;
534
+ // Thread.sleep(6000);
535
+ logger .fine ("Generating support bundle... task id " + taskId );
536
+ File outputDir = new File (SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId );
537
+ if (!outputDir .exists ()) {
538
+ if (!outputDir .mkdirs ()) {
539
+ throw new IOException ("Failed to create directory: " + outputDir .getAbsolutePath ());
537
540
}
541
+ }
538
542
539
- try (FileOutputStream fileOutputStream =
540
- new FileOutputStream (new File (outputDir , SUPPORT_BUNDLE_FILE_NAME ))) {
541
- SupportPlugin .setRequesterAuthentication (Jenkins .getAuthentication2 ());
542
- try {
543
- try (ACLContext ignored = ACL .as2 (ACL .SYSTEM2 )) {
544
- SupportPlugin .writeBundle (fileOutputStream , components );
545
- } catch (IOException e ) {
546
- logger .log (Level .FINE , e .getMessage (), e );
547
- }
548
- } finally {
549
- SupportPlugin .clearRequesterAuthentication ();
543
+ try (FileOutputStream fileOutputStream =
544
+ new FileOutputStream (new File (outputDir , supportBundleName ))) {
545
+ SupportPlugin .setRequesterAuthentication (Jenkins .getAuthentication2 ());
546
+ try {
547
+ try (ACLContext ignored = ACL .as2 (ACL .SYSTEM2 )) {
548
+ SupportPlugin .writeBundle (fileOutputStream , components );
549
+ } catch (IOException e ) {
550
+ logger .log (Level .WARNING , e .getMessage (), e );
550
551
}
551
552
} finally {
552
- logger . fine ( "Response completed" );
553
+ SupportPlugin . clearRequesterAuthentication ( );
553
554
}
554
-
555
- pathToBundle = outputDir .getAbsolutePath () + "/" + SUPPORT_BUNDLE_FILE_NAME ;
556
- isCompleted = true ;
557
- progress (1 );
555
+ } finally {
556
+ logger .fine ("Response completed" );
558
557
}
558
+
559
+ pathToBundle = outputDir .getAbsolutePath () + "/" + supportBundleName ;
560
+ isCompleted = true ;
561
+ progress (1 );
562
+
559
563
}
560
564
561
565
@ NonNull
@@ -564,21 +568,25 @@ protected JSON data() {
564
568
JSONObject json = new JSONObject ();
565
569
json .put ("isCompleted" , isCompleted );
566
570
json .put ("pathToBundle" , pathToBundle );
567
- json .put ("status" , 1 );
568
571
json .put ("taskId" , String .valueOf (taskId ));
569
572
return json ;
570
573
}
574
+
575
+ public String getSupportBundleName () {
576
+ return supportBundleName ;
577
+ }
571
578
}
572
579
573
580
public void doDownloadBundle (@ QueryParameter ("taskId" ) String taskId , StaplerResponse2 rsp ) throws IOException {
574
- File bundleFile = new File (SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId + "/" + SUPPORT_BUNDLE_FILE_NAME );
581
+ String supportBundleName = generatorByTaskId .get (UUID .fromString (taskId )).getSupportBundleName ();
582
+ File bundleFile = new File (SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId + "/" + supportBundleName );
575
583
if (!bundleFile .exists ()) {
576
584
rsp .sendError (HttpServletResponse .SC_NOT_FOUND , "Support bundle file not found" );
577
585
return ;
578
586
}
579
587
580
588
rsp .setContentType ("application/zip" );
581
- rsp .addHeader ("Content-Disposition" , "attachment; filename=" + SUPPORT_BUNDLE_FILE_NAME );
589
+ rsp .addHeader ("Content-Disposition" , "attachment; filename=" + supportBundleName );
582
590
try (ServletOutputStream outputStream = rsp .getOutputStream ();
583
591
FileInputStream inputStream = new FileInputStream (bundleFile )) {
584
592
IOUtils .copy (inputStream , outputStream );
@@ -592,7 +600,7 @@ public void doDownloadBundle(@QueryParameter("taskId") String taskId, StaplerRes
592
600
593
601
try {
594
602
FileUtils .deleteDirectory (outputDir );
595
- generatorMap .remove (taskId );
603
+ generatorByTaskId .remove (taskId );
596
604
logger .fine (() -> "Cleaned up temporary directory " + outputDir );
597
605
598
606
} catch (IOException e ) {
0 commit comments