Skip to content

Commit 88221f0

Browse files
authored
Code cleanup: renaming fields and vars in MonitoredJobHealthCheck (#597)
* Rename private fields and local vars in MonitoredJobHealthCheck to be more consistent in terms of what is a timestamp, what is a duration, what the unit is, etc. It's not perfect, but it's better. * Change calculateWarningThreshold to a static method and add arguments for clarity. * Rename several method parameters for clarity.
1 parent d1feb8b commit 88221f0

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

src/main/java/org/kiwiproject/dropwizard/util/health/MonitoredJobHealthCheck.java

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ public class MonitoredJobHealthCheck extends HealthCheck {
9090
static final Duration DEFAULT_WARNING_DURATION = Duration.minutes(15);
9191

9292
private final MonitoredJob job;
93-
private final long expectedFrequency;
93+
private final long expectedFrequencyMilliseconds;
9494
private final Duration errorWarningDuration;
9595
private final String errorWarningDurationString;
96-
private final long errorWarningMilliseconds;
96+
private final long errorWarningDurationMilliseconds;
9797
private final double thresholdFactor;
98-
private final long lowerTimeBound;
98+
private final long lowerTimeBoundTimestampMillis;
9999
private final KiwiEnvironment kiwiEnvironment;
100-
private final long warningThreshold;
101-
private final String warningThresholdString;
100+
private final long warningThresholdDurationMilliseconds;
101+
private final String warningThresholdDurationString;
102102

103103
@Builder
104104
private MonitoredJobHealthCheck(MonitoredJob job,
@@ -109,38 +109,40 @@ private MonitoredJobHealthCheck(MonitoredJob job,
109109
KiwiEnvironment environment) {
110110

111111
this.job = requireNotNull(job, "job is required");
112-
this.expectedFrequency = requireNotNull(expectedFrequency, "expectedFrequency is required").toMilliseconds();
112+
this.expectedFrequencyMilliseconds = requireNotNull(expectedFrequency, "expectedFrequency is required").toMilliseconds();
113113
this.errorWarningDuration = isNull(errorWarningDuration) ? DEFAULT_WARNING_DURATION : errorWarningDuration;
114114
this.errorWarningDurationString = formatDropwizardDurationWords(this.errorWarningDuration);
115-
this.errorWarningMilliseconds = this.errorWarningDuration.toMilliseconds();
115+
this.errorWarningDurationMilliseconds = this.errorWarningDuration.toMilliseconds();
116116
this.thresholdFactor = isNull(thresholdFactor) ? DEFAULT_THRESHOLD_FACTOR : thresholdFactor;
117117
this.kiwiEnvironment = isNull(environment) ? new DefaultEnvironment() : environment;
118-
this.lowerTimeBound = isNull(lowerTimeBound) ? kiwiEnvironment.currentTimeMillis() : lowerTimeBound;
119-
this.warningThreshold = calculateWarningThreshold();
120-
this.warningThresholdString = formatMillisecondDurationWords(this.warningThreshold);
118+
this.lowerTimeBoundTimestampMillis = isNull(lowerTimeBound) ? kiwiEnvironment.currentTimeMillis() : lowerTimeBound;
119+
this.warningThresholdDurationMilliseconds = calculateWarningThreshold(expectedFrequencyMilliseconds, this.thresholdFactor);
120+
this.warningThresholdDurationString = formatMillisecondDurationWords(this.warningThresholdDurationMilliseconds);
121121
}
122122

123123
@Override
124124
protected Result check() {
125125
try {
126-
var lastRun = job.lastSuccessMillis();
126+
var lastSuccess = job.lastSuccessMillis();
127127
if (!job.isActive()) {
128-
return buildHealthyResult(f("Job is inactive. (last run: {})", instantToStringOrNever(lastRun)));
128+
return buildHealthyResult(f("Job is inactive. (last run: {})", instantToStringOrNever(lastSuccess)));
129129
}
130130

131131
var now = kiwiEnvironment.currentTimeMillis();
132132
var lastFailure = job.lastFailureMillis();
133-
if ((now - lastFailure) < errorWarningMilliseconds) {
133+
var timeSinceLastFailure = now - lastFailure;
134+
if (timeSinceLastFailure < errorWarningDurationMilliseconds) {
134135
return buildUnhealthyResult(f("An error has occurred at: {}, which is within the threshold of: {}",
135136
instantToStringOrNever(lastFailure), errorWarningDurationString));
136137
}
137138

138-
if ((now - getTimeOrServerStart(lastRun)) > warningThreshold) {
139+
var timeSinceLastSuccess = now - getTimeOrServerStart(lastSuccess);
140+
if (timeSinceLastSuccess > warningThresholdDurationMilliseconds) {
139141
return buildUnhealthyResult(f("Last successful execution was: {}, which is older than the threshold of: {}",
140-
instantToStringOrNever(lastRun), warningThresholdString));
142+
instantToStringOrNever(lastSuccess), warningThresholdDurationString));
141143
}
142144

143-
return buildHealthyResult(f("Last successful execution was: {}", instantToStringOrNever(lastRun)));
145+
return buildHealthyResult(f("Last successful execution was: {}", instantToStringOrNever(lastSuccess)));
144146
} catch (Exception e) {
145147
LOG.error("Encountered Exception: ", e);
146148
return handleException(e);
@@ -168,30 +170,32 @@ private ResultBuilder resultBuilderWith(String message, boolean healthy, Excepti
168170
.withDetail("lastJobExceptionInfo", job.lastJobExceptionInfo())
169171
.withDetail("lastSuccess", job.lastSuccessMillis())
170172
.withDetail("lastExecutionTimeMs", job.lastExecutionTimeMillis())
171-
.withDetail("expectedFrequencyMs", expectedFrequency)
172-
.withDetail("warningThresholdMs", warningThreshold)
173-
.withDetail("errorWarningDurationMs", errorWarningMilliseconds);
173+
.withDetail("expectedFrequencyMs", expectedFrequencyMilliseconds)
174+
.withDetail("warningThresholdMs", warningThresholdDurationMilliseconds)
175+
.withDetail("errorWarningDurationMs", errorWarningDurationMilliseconds);
174176
}
175177

176178
private static void checkValidHealthArgumentCombination(boolean healthy, Exception error) {
177179
checkArgument(!healthy || isNull(error), "If healthy, error must be null!");
178180
}
179181

180-
private long calculateWarningThreshold() {
181-
return Math.max((long) (expectedFrequency * thresholdFactor),
182-
MINIMUM_WARNING_THRESHOLD.toMilliseconds());
182+
private static long calculateWarningThreshold(long expectedFrequencyMilliseconds, double thresholdFactor) {
183+
return (long) Math.max(
184+
expectedFrequencyMilliseconds * thresholdFactor,
185+
MINIMUM_WARNING_THRESHOLD.toMilliseconds()
186+
);
183187
}
184188

185-
private static String instantToStringOrNever(long epochMs) {
186-
return epochMs != 0 ? Instant.ofEpochMilli(epochMs).toString() : "never";
189+
private static String instantToStringOrNever(long epochMillis) {
190+
return epochMillis != 0 ? Instant.ofEpochMilli(epochMillis).toString() : "never";
187191
}
188192

189193
private Result buildUnhealthyResult(String message) {
190194
return resultBuilderWith(message, false).build();
191195
}
192196

193-
private long getTimeOrServerStart(long lastRunMs) {
194-
return Math.max(lastRunMs, lowerTimeBound);
197+
private long getTimeOrServerStart(long lastSuccessMillis) {
198+
return Math.max(lastSuccessMillis, lowerTimeBoundTimestampMillis);
195199
}
196200

197201
private Result handleException(Exception e) {

src/test/java/org/kiwiproject/dropwizard/util/health/MonitoredJobHealthCheckTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void shouldDefaultVariousFields() {
6262
assertThat(healthCheck.getErrorWarningDuration()).isEqualTo(MonitoredJobHealthCheck.DEFAULT_WARNING_DURATION);
6363
assertThat(healthCheck.getThresholdFactor()).isEqualTo(MonitoredJobHealthCheck.DEFAULT_THRESHOLD_FACTOR);
6464
assertThat(healthCheck.getKiwiEnvironment()).isNotNull();
65-
assertThat(healthCheck.getLowerTimeBound()).isPositive();
65+
assertThat(healthCheck.getLowerTimeBoundTimestampMillis()).isPositive();
6666
}
6767
}
6868

0 commit comments

Comments
 (0)