@@ -90,15 +90,15 @@ public class MonitoredJobHealthCheck extends HealthCheck {
90
90
static final Duration DEFAULT_WARNING_DURATION = Duration .minutes (15 );
91
91
92
92
private final MonitoredJob job ;
93
- private final long expectedFrequency ;
93
+ private final long expectedFrequencyMilliseconds ;
94
94
private final Duration errorWarningDuration ;
95
95
private final String errorWarningDurationString ;
96
- private final long errorWarningMilliseconds ;
96
+ private final long errorWarningDurationMilliseconds ;
97
97
private final double thresholdFactor ;
98
- private final long lowerTimeBound ;
98
+ private final long lowerTimeBoundTimestampMillis ;
99
99
private final KiwiEnvironment kiwiEnvironment ;
100
- private final long warningThreshold ;
101
- private final String warningThresholdString ;
100
+ private final long warningThresholdDurationMilliseconds ;
101
+ private final String warningThresholdDurationString ;
102
102
103
103
@ Builder
104
104
private MonitoredJobHealthCheck (MonitoredJob job ,
@@ -109,38 +109,40 @@ private MonitoredJobHealthCheck(MonitoredJob job,
109
109
KiwiEnvironment environment ) {
110
110
111
111
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 ();
113
113
this .errorWarningDuration = isNull (errorWarningDuration ) ? DEFAULT_WARNING_DURATION : errorWarningDuration ;
114
114
this .errorWarningDurationString = formatDropwizardDurationWords (this .errorWarningDuration );
115
- this .errorWarningMilliseconds = this .errorWarningDuration .toMilliseconds ();
115
+ this .errorWarningDurationMilliseconds = this .errorWarningDuration .toMilliseconds ();
116
116
this .thresholdFactor = isNull (thresholdFactor ) ? DEFAULT_THRESHOLD_FACTOR : thresholdFactor ;
117
117
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 );
121
121
}
122
122
123
123
@ Override
124
124
protected Result check () {
125
125
try {
126
- var lastRun = job .lastSuccessMillis ();
126
+ var lastSuccess = job .lastSuccessMillis ();
127
127
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 )));
129
129
}
130
130
131
131
var now = kiwiEnvironment .currentTimeMillis ();
132
132
var lastFailure = job .lastFailureMillis ();
133
- if ((now - lastFailure ) < errorWarningMilliseconds ) {
133
+ var timeSinceLastFailure = now - lastFailure ;
134
+ if (timeSinceLastFailure < errorWarningDurationMilliseconds ) {
134
135
return buildUnhealthyResult (f ("An error has occurred at: {}, which is within the threshold of: {}" ,
135
136
instantToStringOrNever (lastFailure ), errorWarningDurationString ));
136
137
}
137
138
138
- if ((now - getTimeOrServerStart (lastRun )) > warningThreshold ) {
139
+ var timeSinceLastSuccess = now - getTimeOrServerStart (lastSuccess );
140
+ if (timeSinceLastSuccess > warningThresholdDurationMilliseconds ) {
139
141
return buildUnhealthyResult (f ("Last successful execution was: {}, which is older than the threshold of: {}" ,
140
- instantToStringOrNever (lastRun ), warningThresholdString ));
142
+ instantToStringOrNever (lastSuccess ), warningThresholdDurationString ));
141
143
}
142
144
143
- return buildHealthyResult (f ("Last successful execution was: {}" , instantToStringOrNever (lastRun )));
145
+ return buildHealthyResult (f ("Last successful execution was: {}" , instantToStringOrNever (lastSuccess )));
144
146
} catch (Exception e ) {
145
147
LOG .error ("Encountered Exception: " , e );
146
148
return handleException (e );
@@ -168,30 +170,32 @@ private ResultBuilder resultBuilderWith(String message, boolean healthy, Excepti
168
170
.withDetail ("lastJobExceptionInfo" , job .lastJobExceptionInfo ())
169
171
.withDetail ("lastSuccess" , job .lastSuccessMillis ())
170
172
.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 );
174
176
}
175
177
176
178
private static void checkValidHealthArgumentCombination (boolean healthy , Exception error ) {
177
179
checkArgument (!healthy || isNull (error ), "If healthy, error must be null!" );
178
180
}
179
181
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
+ );
183
187
}
184
188
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" ;
187
191
}
188
192
189
193
private Result buildUnhealthyResult (String message ) {
190
194
return resultBuilderWith (message , false ).build ();
191
195
}
192
196
193
- private long getTimeOrServerStart (long lastRunMs ) {
194
- return Math .max (lastRunMs , lowerTimeBound );
197
+ private long getTimeOrServerStart (long lastSuccessMillis ) {
198
+ return Math .max (lastSuccessMillis , lowerTimeBoundTimestampMillis );
195
199
}
196
200
197
201
private Result handleException (Exception e ) {
0 commit comments