diff options
author | Anton Tananaev <anton@traccar.org> | 2024-06-24 07:53:42 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2024-06-24 07:53:42 -0700 |
commit | 690f734e423a6ebe55b3bef5b7ac42cc734b664f (patch) | |
tree | 237a985fddeca7876dc717f8323d28eb377c60a0 /src/main/java/org/traccar/schedule | |
parent | e9b1392ddc72bd8d5adcde69012e90cb4004c930 (diff) | |
download | trackermap-server-690f734e423a6ebe55b3bef5b7ac42cc734b664f.tar.gz trackermap-server-690f734e423a6ebe55b3bef5b7ac42cc734b664f.tar.bz2 trackermap-server-690f734e423a6ebe55b3bef5b7ac42cc734b664f.zip |
Health check for stored messages
Diffstat (limited to 'src/main/java/org/traccar/schedule')
-rw-r--r-- | src/main/java/org/traccar/schedule/TaskHealthCheck.java | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/src/main/java/org/traccar/schedule/TaskHealthCheck.java b/src/main/java/org/traccar/schedule/TaskHealthCheck.java index a60935f18..b3387eca7 100644 --- a/src/main/java/org/traccar/schedule/TaskHealthCheck.java +++ b/src/main/java/org/traccar/schedule/TaskHealthCheck.java @@ -24,6 +24,8 @@ import org.traccar.config.Keys; import jakarta.inject.Inject; import jakarta.ws.rs.client.Client; +import org.traccar.database.StatisticsManager; + import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -33,6 +35,7 @@ public class TaskHealthCheck implements ScheduleTask { private final Config config; private final Client client; + private final StatisticsManager statisticsManager; private final long gracePeriod = System.currentTimeMillis() + TimeUnit.HOURS.toMillis(1); @@ -40,11 +43,16 @@ public class TaskHealthCheck implements ScheduleTask { private boolean enabled; private long period; + private double dropThreshold; + + private int messageLastTotal; + private int messageLastPeriod; @Inject - public TaskHealthCheck(Config config, Client client) { + public TaskHealthCheck(Config config, Client client, StatisticsManager statisticsManager) { this.config = config; this.client = client; + this.statisticsManager = statisticsManager; if (!config.getBoolean(Keys.WEB_DISABLE_HEALTH_CHECK) && System.getProperty("os.name").toLowerCase().startsWith("linux")) { try { @@ -55,6 +63,7 @@ public class TaskHealthCheck implements ScheduleTask { } if (period > 0) { LOGGER.info("Health check enabled with period {}", period); + dropThreshold = config.getDouble(Keys.WEB_HEALTH_CHECK_DROP_THRESHOLD); enabled = true; } } catch (UnsatisfiedLinkError e) { @@ -80,11 +89,28 @@ public class TaskHealthCheck implements ScheduleTask { public void run() { LOGGER.debug("Health check running"); if (System.currentTimeMillis() > gracePeriod) { + boolean success = true; + int status = client.target(getUrl()).request().get().getStatus(); - if (status == 200) { + if (status != 200) { + success = false; + LOGGER.warn("Web health check failed with status {}", status); + } + + if (dropThreshold > 0) { + int messageCurrentTotal = statisticsManager.messageStoredCount(); + int messageCurrentPeriod = messageCurrentTotal - messageLastTotal; + double drop = messageCurrentPeriod / (double) messageLastPeriod; + if (drop < dropThreshold) { + success = false; + LOGGER.warn("Message health check failed with drop {}", drop); + } + messageLastTotal = messageCurrentTotal; + messageLastPeriod = messageCurrentPeriod; + } + + if (success) { notifyWatchdog(); - } else { - LOGGER.warn("Health check failed with status {}", status); } } else { notifyWatchdog(); |