22
33
44import it .aboutbits .springboot .emailservice .lib .EmailSchedulerCallback ;
5- import lombok .AllArgsConstructor ;
5+ import lombok .RequiredArgsConstructor ;
66import lombok .extern .log4j .Log4j2 ;
77import org .springframework .scheduling .annotation .Scheduled ;
88
9+ import java .time .Duration ;
910import java .util .List ;
1011
11- @ AllArgsConstructor
12+ @ RequiredArgsConstructor
1213@ Log4j2
1314public class SendScheduledEmails {
1415 private static final String JOB_DESCRIPTION = "Sending open and failed email notifications." ;
@@ -17,9 +18,13 @@ public class SendScheduledEmails {
1718 private final ManageEmail manageEmail ;
1819 private final List <EmailSchedulerCallback > callbacks ;
1920
21+ private long lastInfoLogMillis = System .currentTimeMillis ();
22+ private long silentRuns = 0 ;
23+ private boolean firstRun = true ;
24+
2025 @ Scheduled (initialDelayString = "${aboutbits.emailservice.scheduling.interval:30000}" , fixedDelayString = "${aboutbits.emailservice.scheduling.interval:30000}" )
2126 void sendEmails () {
22- log . info ( "Start: " + JOB_DESCRIPTION );
27+ logStartOfPass ( );
2328
2429 var emailsToSend = queryEmail .readyToSend ();
2530
@@ -31,13 +36,13 @@ void sendEmails() {
3136 case ERROR -> countError ++;
3237 case SENT -> countSent ++;
3338 default -> log .warn (
34- "Email job produced an invalid notification result state: {}." ,
35- updatedEmail .getState ().name ());
39+ JOB_DESCRIPTION + " | Job produced an invalid notification result state: {}." ,
40+ updatedEmail .getState ().name ()
41+ );
3642 }
3743 }
3844
39- log .info ("Finished: " + JOB_DESCRIPTION );
40- log .info ("Sent: {}, Errors: {}" , countSent , countError );
45+ logEndOfPass (countSent , countError );
4146
4247 for (var callback : callbacks ) {
4348 callback .report (new EmailSchedulerCallback .Report (
@@ -47,4 +52,33 @@ void sendEmails() {
4752 ));
4853 }
4954 }
55+
56+ private void logStartOfPass () {
57+ if (firstRun ) {
58+ log .info (JOB_DESCRIPTION + " | Job enabled." );
59+ firstRun = false ;
60+ }
61+ log .debug (JOB_DESCRIPTION + " | Start" );
62+ }
63+
64+ private void logEndOfPass (int countSent , int countError ) {
65+ log .debug (JOB_DESCRIPTION + " | Finished" );
66+ if (countSent > 0 || countError > 0 ) {
67+ lastInfoLogMillis = System .currentTimeMillis ();
68+ silentRuns = 0 ;
69+ log .info (JOB_DESCRIPTION + " | Sent: {}, Errors: {}" , countSent , countError );
70+ } else {
71+ log .debug (JOB_DESCRIPTION + " | Sent: {}, Errors: {}" , countSent , countError );
72+ silentRuns ++;
73+ }
74+
75+ if (lastInfoLogMillis + Duration .ofHours (1 ).toMillis () < System .currentTimeMillis () && !log .isDebugEnabled ()) {
76+ log .info (
77+ JOB_DESCRIPTION + " | Ran silently {} times. Enable debug logging to see all hidden passes." ,
78+ silentRuns
79+ );
80+ lastInfoLogMillis = System .currentTimeMillis ();
81+ silentRuns = 0 ;
82+ }
83+ }
5084}
0 commit comments