From 29b8caa047b3b1468eb8b21e76f5df8b5c2f6ddb Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 25 Jan 2024 08:11:32 -0800 Subject: Initialization grace period (fix #5258) --- src/main/java/org/traccar/Main.java | 32 ++++++---------- .../java/org/traccar/schedule/ScheduleManager.java | 11 +++--- .../java/org/traccar/schedule/TaskClearStatus.java | 43 ++++++++++++++++++++++ .../java/org/traccar/schedule/TaskHealthCheck.java | 24 ++++++++---- 4 files changed, 78 insertions(+), 32 deletions(-) create mode 100644 src/main/java/org/traccar/schedule/TaskClearStatus.java (limited to 'src/main/java') diff --git a/src/main/java/org/traccar/Main.java b/src/main/java/org/traccar/Main.java index e34fbb72a..33fcf654f 100644 --- a/src/main/java/org/traccar/Main.java +++ b/src/main/java/org/traccar/Main.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2022 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2024 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. @@ -20,10 +20,8 @@ import com.google.inject.Injector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.broadcast.BroadcastService; -import org.traccar.helper.model.DeviceUtil; import org.traccar.schedule.ScheduleManager; import org.traccar.storage.DatabaseModule; -import org.traccar.storage.Storage; import org.traccar.web.WebModule; import org.traccar.web.WebServer; @@ -33,10 +31,9 @@ import java.lang.management.MemoryMXBean; import java.lang.management.OperatingSystemMXBean; import java.lang.management.RuntimeMXBean; import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; -import java.util.Objects; -import java.util.stream.Collectors; -import java.util.stream.Stream; public final class Main { @@ -70,8 +67,7 @@ public final class Main { + " heap: " + memoryBean.getHeapMemoryUsage().getMax() / (1024 * 1024) + "mb" + " non-heap: " + memoryBean.getNonHeapMemoryUsage().getMax() / (1024 * 1024) + "mb"); - LOGGER.info("Character encoding: " - + System.getProperty("file.encoding") + " charset: " + Charset.defaultCharset()); + LOGGER.info("Character encoding: " + Charset.defaultCharset().displayName()); } catch (Exception error) { LOGGER.warn("Failed to get system info"); @@ -122,18 +118,14 @@ public final class Main { LOGGER.info("Version: " + Main.class.getPackage().getImplementationVersion()); LOGGER.info("Starting server..."); - if (injector.getInstance(BroadcastService.class).singleInstance()) { - DeviceUtil.resetStatus(injector.getInstance(Storage.class)); - } - - var services = Stream.of( - ServerManager.class, WebServer.class, ScheduleManager.class, BroadcastService.class) - .map(injector::getInstance) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - for (var service : services) { - service.start(); + var services = new ArrayList(); + for (var clazz : List.of( + ScheduleManager.class, ServerManager.class, WebServer.class, BroadcastService.class)) { + var service = injector.getInstance(clazz); + if (service != null) { + service.start(); + services.add(service); + } } Thread.setDefaultUncaughtExceptionHandler((t, e) -> LOGGER.error("Thread exception", e)); diff --git a/src/main/java/org/traccar/schedule/ScheduleManager.java b/src/main/java/org/traccar/schedule/ScheduleManager.java index 3756d955b..742428fd8 100644 --- a/src/main/java/org/traccar/schedule/ScheduleManager.java +++ b/src/main/java/org/traccar/schedule/ScheduleManager.java @@ -20,9 +20,9 @@ import org.traccar.LifecycleObject; import jakarta.inject.Inject; import jakarta.inject.Singleton; -import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.stream.Stream; @Singleton public class ScheduleManager implements LifecycleObject { @@ -38,14 +38,15 @@ public class ScheduleManager implements LifecycleObject { @Override public void start() { executor = Executors.newSingleThreadScheduledExecutor(); - var tasks = List.of( + Stream.of( + TaskHealthCheck.class, + TaskClearStatus.class, TaskExpirations.class, TaskDeleteTemporary.class, TaskReports.class, TaskDeviceInactivityCheck.class, - TaskWebSocketKeepalive.class, - TaskHealthCheck.class); - tasks.forEach(task -> injector.getInstance(task).schedule(executor)); + TaskWebSocketKeepalive.class) + .forEachOrdered(task -> injector.getInstance(task).schedule(executor)); } @Override diff --git a/src/main/java/org/traccar/schedule/TaskClearStatus.java b/src/main/java/org/traccar/schedule/TaskClearStatus.java new file mode 100644 index 000000000..78fecc0ea --- /dev/null +++ b/src/main/java/org/traccar/schedule/TaskClearStatus.java @@ -0,0 +1,43 @@ +/* + * Copyright 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.schedule; + +import jakarta.inject.Inject; +import org.traccar.broadcast.BroadcastService; +import org.traccar.helper.model.DeviceUtil; +import org.traccar.storage.Storage; +import org.traccar.storage.StorageException; + +import java.util.concurrent.ScheduledExecutorService; + +public class TaskClearStatus implements ScheduleTask { + + @Inject + public TaskClearStatus(BroadcastService broadcastService, Storage storage) throws StorageException { + if (broadcastService.singleInstance()) { + DeviceUtil.resetStatus(storage); + } + } + + @Override + public void schedule(ScheduledExecutorService executor) { + } + + @Override + public void run() { + } + +} diff --git a/src/main/java/org/traccar/schedule/TaskHealthCheck.java b/src/main/java/org/traccar/schedule/TaskHealthCheck.java index abdc5af48..a60935f18 100644 --- a/src/main/java/org/traccar/schedule/TaskHealthCheck.java +++ b/src/main/java/org/traccar/schedule/TaskHealthCheck.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 - 2022 Anton Tananaev (anton@traccar.org) + * Copyright 2020 - 2024 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. @@ -34,6 +34,8 @@ public class TaskHealthCheck implements ScheduleTask { private final Config config; private final Client client; + private final long gracePeriod = System.currentTimeMillis() + TimeUnit.HOURS.toMillis(1); + private SystemD systemD; private boolean enabled; @@ -77,14 +79,22 @@ public class TaskHealthCheck implements ScheduleTask { @Override public void run() { LOGGER.debug("Health check running"); - int status = client.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); + if (System.currentTimeMillis() > gracePeriod) { + int status = client.target(getUrl()).request().get().getStatus(); + if (status == 200) { + notifyWatchdog(); + } else { + LOGGER.warn("Health check failed with status {}", status); } } else { - LOGGER.warn("Health check failed with status {}", status); + notifyWatchdog(); + } + } + + private void notifyWatchdog() { + int result = systemD.sd_notify(0, "WATCHDOG=1"); + if (result < 0) { + LOGGER.warn("Health check notify error {}", result); } } -- cgit v1.2.3