Skip to content

Commit

Permalink
Introduce new printing queue threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
sebr72 committed Aug 29, 2024
1 parent d50c8c8 commit b4d4c4b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
42 changes: 23 additions & 19 deletions core/src/main/java/org/mapfish/print/metrics/ApplicationStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,50 @@
import org.springframework.beans.factory.annotation.Value;

class ApplicationStatus extends HealthCheck {
@Value("${expectedMaxTime.sinceLastPrint.InSeconds}")
@Value("${healthStatus.expectedMaxTime.sinceLastPrint.InSeconds}")
private int secondsInFloatingWindow;

@Value("${healthStatus.unhealthyThreshold.maxNbrPrintJobQueued}")
private int maxNbrPrintJobQueued;

@Autowired private JobQueue jobQueue;
@Autowired private ThreadPoolJobManager jobManager;

private long previousNumberOfWaitingJobs = 0L;

/**
* When a Result is returned it can be healthy or unhealthy. In both cases it is associated to a
* Http 200 status. When an exception is thrown it means the server is no longer working at all,
* it is associated to a Http 500 status.
*/
@Override
protected Result check() throws Exception {
long waitingJobsCount = jobQueue.getWaitingJobsCount();
if (waitingJobsCount == 0) {
previousNumberOfWaitingJobs = waitingJobsCount;
return Result.healthy("No print job is waiting in the queue.");
}

String health = "Number of print jobs waiting is " + waitingJobsCount;
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 (hasJobExecutedRecently()) {
if (waitingJobsCount > previousNumberOfWaitingJobs) {
previousNumberOfWaitingJobs = waitingJobsCount;
return Result.unhealthy("No print job was ever processed by this server." + health);
} else if (hasThisServerPrintedRecently()) {
if (waitingJobsCount > maxNbrPrintJobQueued) {
return Result.unhealthy(
"Number of print jobs queued is increasing. But this server is processing them. "
+ health);
"Number of print jobs queued is above threshold: " + maxNbrPrintJobQueued + health);
} else {
previousNumberOfWaitingJobs = waitingJobsCount;
return Result.healthy(
"Print jobs are being dequeued. Number of print jobs waiting is " + waitingJobsCount);
return Result.healthy("This server instance is printing." + health);
}
} else {
previousNumberOfWaitingJobs = waitingJobsCount;
throw new RuntimeException(
"No print job was processed by this server, in the last (seconds): "
+ secondsInFloatingWindow);
throw notificationForBrokenServer();
}
}

private boolean hasJobExecutedRecently() {
private RuntimeException notificationForBrokenServer() {
return new RuntimeException(
"None of the print job queued was processed by this server, in the last (seconds): "
+ secondsInFloatingWindow);
}

private boolean hasThisServerPrintedRecently() {
final Instant lastExecutedJobTime = jobManager.getLastExecutedJobTimestamp().toInstant();
final Instant beginningOfTimeWindow = getBeginningOfTimeWindow();
return lastExecutedJobTime.isAfter(beginningOfTimeWindow);
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/resources/mapfish-spring.properties
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ httpRequest.fetchRetry.maxNumber=3
httpRequest.fetchRetry.intervalMillis=100

# Amount of time in the past where we check if a print job was executed by this server
expectedMaxTime.sinceLastPrint.InSeconds=300
healthStatus.expectedMaxTime.sinceLastPrint.InSeconds=300

# Maximum number of Print Jobs queued before raising it i
healthStatus.unhealthyThreshold.maxNbrPrintJobQueued=4
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,34 @@ public void testCheck_Failed_NoPrintJobs() throws Exception {
fail("Expected exception not thrown");
} catch (RuntimeException e) {
assertEquals(
"No print job was processed by this server, in the last (seconds): 300", e.getMessage());
"None of the print job queued was processed by this server, in the last (seconds): 300",
e.getMessage());
} catch (Exception e) {
fail("Incorrect Exception thrown: " + e);
}
}

@Test
public void testCheck_Success_PrintJobs() throws Exception {
when(jobQueue.getWaitingJobsCount()).thenReturn(10L, 9L);
when(jobQueue.getWaitingJobsCount()).thenReturn(5L, 4L);
when(jobManager.getLastExecutedJobTimestamp()).thenReturn(new Date());

applicationStatus.check();
HealthCheck.Result result = applicationStatus.check();

assertTrue(result.isHealthy());
assertTrue(result.getMessage().contains("Print jobs are being dequeued"));
assertTrue(result.getMessage().contains("This server instance is printing."));
}

@Test
public void testCheck_Fail_PrintJobsButIncreasing() throws Exception {
when(jobQueue.getWaitingJobsCount()).thenReturn(0L, 1L);
public void testCheck_Fail_TooManyJobsAreQueued() throws Exception {
when(jobQueue.getWaitingJobsCount()).thenReturn(4L, 5L);
when(jobManager.getLastExecutedJobTimestamp()).thenReturn(new Date());

applicationStatus.check();
HealthCheck.Result result = applicationStatus.check();

assertFalse(result.isHealthy());
assertTrue(result.getMessage().contains("Number of print jobs queued is increasing."));
assertTrue(result.getMessage().contains("Number of print jobs queued is above threshold: "));
}
}

0 comments on commit b4d4c4b

Please sign in to comment.