-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate server health ensuring print job are executed.
- Loading branch information
Showing
4 changed files
with
63 additions
and
8 deletions.
There are no files selected for viewing
47 changes: 42 additions & 5 deletions
47
core/src/main/java/org/mapfish/print/metrics/ApplicationStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,59 @@ | ||
package org.mapfish.print.metrics; | ||
|
||
import com.codahale.metrics.health.HealthCheck; | ||
import java.time.Instant; | ||
import java.time.temporal.TemporalAmount; | ||
import java.util.Date; | ||
import org.mapfish.print.servlet.job.JobQueue; | ||
import org.mapfish.print.servlet.job.impl.ThreadPoolJobManager; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
|
||
class ApplicationStatus extends HealthCheck { | ||
public static final int MAX_WAITING_JOBS_TILL_UNHEALTHY = 5; | ||
@Value("${expectedMaxTime.sinceLastPrint.InSeconds}") | ||
private int SECONDS_IN_FLOATING_WINDOW; | ||
|
||
private final TemporalAmount TIME_WINDOW_SIZE = | ||
java.time.Duration.ofSeconds(SECONDS_IN_FLOATING_WINDOW); | ||
@Autowired private JobQueue jobQueue; | ||
@Autowired private ThreadPoolJobManager jobManager; | ||
|
||
private long previousNumberOfWaitingJobs = 0L; | ||
|
||
@Override | ||
protected Result check() throws Exception { | ||
long waitingJobsCount = jobQueue.getWaitingJobsCount(); | ||
String health = "Number of jobs waiting is " + waitingJobsCount; | ||
if (waitingJobsCount == 0) { | ||
previousNumberOfWaitingJobs = waitingJobsCount; | ||
return Result.healthy("No print job is waiting in the queue."); | ||
} | ||
|
||
if (waitingJobsCount >= MAX_WAITING_JOBS_TILL_UNHEALTHY) { | ||
return Result.unhealthy(health + ". It is too high."); | ||
String health = "Number of print jobs waiting is " + waitingJobsCount; | ||
|
||
if (jobManager.getLastExecutedJobTimestamp() == null) { | ||
return Result.unhealthy("No print job was ever processed by this server. " + health); | ||
} else if (jobManager | ||
.getLastExecutedJobTimestamp() | ||
.toInstant() | ||
.isAfter(getBeginningOfTimeWindow())) { | ||
if (waitingJobsCount > previousNumberOfWaitingJobs) { | ||
previousNumberOfWaitingJobs = waitingJobsCount; | ||
return Result.unhealthy( | ||
"More print jobs are being queued. But this server is processing them. " + health); | ||
} else { | ||
previousNumberOfWaitingJobs = waitingJobsCount; | ||
return Result.healthy( | ||
"Print jobs are being dequeued. Number of print jobs waiting is " + waitingJobsCount); | ||
} | ||
} else { | ||
return Result.healthy(health); | ||
previousNumberOfWaitingJobs = waitingJobsCount; | ||
throw new RuntimeException( | ||
"No print job was processed by this server, in the last (seconds): " | ||
+ SECONDS_IN_FLOATING_WINDOW); | ||
} | ||
} | ||
|
||
private Instant getBeginningOfTimeWindow() { | ||
return new Date().toInstant().minus(TIME_WINDOW_SIZE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters