Skip to content

Commit 19c067a

Browse files
authored
improve scheduler logging to only show relevant passes (#5)
1 parent 09e899d commit 19c067a

2 files changed

Lines changed: 77 additions & 12 deletions

File tree

src/main/java/it/aboutbits/springboot/emailservice/lib/application/CleanupAttachmentFiles.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
import it.aboutbits.springboot.emailservice.lib.AttachmentCleanerCallback;
55
import it.aboutbits.springboot.emailservice.lib.exception.AttachmentException;
6-
import lombok.AllArgsConstructor;
6+
import lombok.RequiredArgsConstructor;
77
import lombok.extern.log4j.Log4j2;
88
import org.springframework.scheduling.annotation.Scheduled;
99

10+
import java.time.Duration;
1011
import java.util.List;
1112

12-
@AllArgsConstructor
13+
@RequiredArgsConstructor
1314
@Log4j2
1415
public class CleanupAttachmentFiles {
1516
private static final String JOB_DESCRIPTION = "Cleanup attachments of sent Emails.";
@@ -18,9 +19,13 @@ public class CleanupAttachmentFiles {
1819
private final ManageEmail manageEmail;
1920
private final List<AttachmentCleanerCallback> callbacks;
2021

22+
private long lastInfoLogMillis = System.currentTimeMillis();
23+
private long silentRuns = 0;
24+
private boolean firstRun = true;
25+
2126
@Scheduled(initialDelayString = "${aboutbits.emailservice.scheduling.interval:30000}", fixedDelayString = "${aboutbits.emailservice.scheduling.interval:30000}")
2227
void cleanupAttachments() {
23-
log.info("Start: " + JOB_DESCRIPTION);
28+
logStartOfPass();
2429

2530
var emailsToCleanup = queryEmail.readyToCleanup();
2631

@@ -35,8 +40,7 @@ void cleanupAttachments() {
3540
}
3641
}
3742

38-
log.info("Finished: " + JOB_DESCRIPTION);
39-
log.info("Cleaned: {}, Errors: {}", countCleaned, countError);
43+
logEndOfPass(countCleaned, countError);
4044

4145
for (var callback : callbacks) {
4246
callback.report(new AttachmentCleanerCallback.Report(
@@ -47,5 +51,32 @@ void cleanupAttachments() {
4751
}
4852
}
4953

54+
private void logStartOfPass() {
55+
if (firstRun) {
56+
log.info(JOB_DESCRIPTION + " | Job enabled.");
57+
firstRun = false;
58+
}
59+
log.debug(JOB_DESCRIPTION + " | Start");
60+
}
5061

62+
private void logEndOfPass(int countCleaned, int countError) {
63+
log.debug(JOB_DESCRIPTION + " | Finished");
64+
if (countCleaned > 0 || countError > 0) {
65+
lastInfoLogMillis = System.currentTimeMillis();
66+
silentRuns = 0;
67+
log.info(JOB_DESCRIPTION + " | Cleaned: {}, Errors: {}", countCleaned, countError);
68+
} else {
69+
log.debug(JOB_DESCRIPTION + " | Cleaned: {}, Errors: {}", countCleaned, countError);
70+
silentRuns++;
71+
}
72+
73+
if (lastInfoLogMillis + Duration.ofHours(1).toMillis() < System.currentTimeMillis() && !log.isDebugEnabled()) {
74+
log.info(
75+
JOB_DESCRIPTION + " | Ran silently {} times. Enable debug logging to see all hidden passes.",
76+
silentRuns
77+
);
78+
lastInfoLogMillis = System.currentTimeMillis();
79+
silentRuns = 0;
80+
}
81+
}
5182
}

src/main/java/it/aboutbits/springboot/emailservice/lib/application/SendScheduledEmails.java

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33

44
import it.aboutbits.springboot.emailservice.lib.EmailSchedulerCallback;
5-
import lombok.AllArgsConstructor;
5+
import lombok.RequiredArgsConstructor;
66
import lombok.extern.log4j.Log4j2;
77
import org.springframework.scheduling.annotation.Scheduled;
88

9+
import java.time.Duration;
910
import java.util.List;
1011

11-
@AllArgsConstructor
12+
@RequiredArgsConstructor
1213
@Log4j2
1314
public 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

Comments
 (0)