diff options
Diffstat (limited to 'src/main/java/org/traccar/api/HealthCheckService.java')
-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 { |