99import org .springframework .context .event .EventListener ;
1010import org .springframework .stereotype .Component ;
1111
12- import java .io .File ;
1312import java .io .IOException ;
1413import java .nio .file .Files ;
1514import java .nio .file .Path ;
@@ -22,6 +21,7 @@ public class AppShutdownHandler {
2221 private FileLoggerService fileLoggerService ;
2322 private FileReportExporter reportExporter ;
2423 private appConfig appConfig ;
24+ private Boolean elevatedFlag ;
2525
2626 @ Autowired
2727 public AppShutdownHandler (FileLoggerService fileLoggerService , FileReportExporter reportExporter , appConfig config ) {
@@ -48,18 +48,24 @@ public void onShutdown() {
4848 } catch (Exception e ) {
4949 System .err .println ("Failed to delete temp directory: " + e .getMessage ());
5050 }
51-
5251 return ;
5352 }
5453
55- try {
56- System .out .println ("AppShutdownHandler - Application is shutting down! Flushing logs and exporting report..." );
57- fileLoggerService .flushLogToMainFile ();
58- System .out .println ("AppShutdownHandler - Shutdown tasks completed successfully." );
59- } catch (Exception e ) {
60- System .err .println ("AppShutdownHandler - Error during shutdown tasks: " + e .getMessage ());
61- e .printStackTrace ();
54+ if (appConfig .isRelaunch () || appConfig .getLogsDir () == null || appConfig .getLogsDir ().isBlank ()) {
55+ System .out .println ("Skipping flush — elevated admin relaunch or before Spring Injection" );
56+ return ;
57+ } else {
58+ try {
59+ System .out .println ("AppShutdownHandler - Application is shutting down! Flushing logs and exporting report..." );
60+ fileLoggerService .flushLogToMainFile ();
61+ System .out .println ("AppShutdownHandler - Shutdown tasks completed successfully." );
62+ } catch (Exception e ) {
63+ System .err .println ("AppShutdownHandler - Error during shutdown tasks: " + e .getMessage ());
64+ e .printStackTrace ();
65+ }
6266 }
67+
68+
6369 }
6470 // Method for correct deletion of temp files when noLogs is selected
6571 private void deleteDirectoryRecursively (Path path ) throws IOException {
0 commit comments