diff options
author | Anton Tananaev <anton@traccar.org> | 2022-04-14 18:10:56 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2022-04-14 18:10:56 -0700 |
commit | f5dcafaf60fd41c5a2cd587e9ecf40e67d235d59 (patch) | |
tree | 991bde6884672a1abb03befe0d8c52e822b0acfc /src/main/java/org/traccar/api | |
parent | 437a7651096ecdd8e9cabeeca760b9af89b10458 (diff) | |
download | trackermap-server-f5dcafaf60fd41c5a2cd587e9ecf40e67d235d59.tar.gz trackermap-server-f5dcafaf60fd41c5a2cd587e9ecf40e67d235d59.tar.bz2 trackermap-server-f5dcafaf60fd41c5a2cd587e9ecf40e67d235d59.zip |
Use schedule manager for health check
Diffstat (limited to 'src/main/java/org/traccar/api')
-rw-r--r-- | src/main/java/org/traccar/api/HealthCheckService.java | 47 |
1 files changed, 21 insertions, 26 deletions
diff --git a/src/main/java/org/traccar/api/HealthCheckService.java b/src/main/java/org/traccar/api/HealthCheckService.java index 0182cc358..8a17c8798 100644 --- a/src/main/java/org/traccar/api/HealthCheckService.java +++ b/src/main/java/org/traccar/api/HealthCheckService.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 Anton Tananaev (anton@traccar.org) + * Copyright 2020 - 2022 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,9 +22,10 @@ import org.slf4j.LoggerFactory; import org.traccar.Context; import org.traccar.config.Keys; -import java.util.TimerTask; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; -public class HealthCheckService { +public class HealthCheckService implements Runnable { private static final Logger LOGGER = LoggerFactory.getLogger(HealthCheckService.class); @@ -52,36 +53,30 @@ public class HealthCheckService { } } - public boolean isEnabled() { - return enabled; - } - - public long getPeriod() { - return period; - } - private String getUrl() { String address = Context.getConfig().getString(Keys.WEB_ADDRESS, "localhost"); int port = Context.getConfig().getInteger(Keys.WEB_PORT); return "http://" + address + ":" + port + "/api/server"; } - public TimerTask createTask() { - return new TimerTask() { - @Override - public void run() { - LOGGER.debug("Health check running"); - int status = Context.getClient().target(getUrl()).request().get().getStatus(); - if (status == 200) { - int result = systemD.sd_notify(0, "WATCHDOG=1"); - if (result < 0) { - LOGGER.warn("Health check notify error {}", result); - } - } else { - LOGGER.warn("Health check failed with status {}", status); - } + public void schedule(ScheduledExecutorService executor) { + if (enabled) { + executor.scheduleAtFixedRate(this, period, period, TimeUnit.MILLISECONDS); + } + } + + @Override + public void run() { + LOGGER.debug("Health check running"); + int status = Context.getClient().target(getUrl()).request().get().getStatus(); + if (status == 200) { + int result = systemD.sd_notify(0, "WATCHDOG=1"); + if (result < 0) { + LOGGER.warn("Health check notify error {}", result); } - }; + } else { + LOGGER.warn("Health check failed with status {}", status); + } } interface SystemD extends Library { |