diff options
author | Anton Tananaev <anton@traccar.org> | 2024-07-13 14:36:56 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2024-07-13 14:36:56 -0700 |
commit | 50e9eced4a045c6d1e71abc5203b73da9673abc0 (patch) | |
tree | 7c449b7268287e2eb25a76b2311394b2e32c2baa /src/main/java | |
parent | 2d1b4aad57da93d171170b2c63d9b6380bf59e44 (diff) | |
download | trackermap-server-50e9eced4a045c6d1e71abc5203b73da9673abc0.tar.gz trackermap-server-50e9eced4a045c6d1e71abc5203b73da9673abc0.tar.bz2 trackermap-server-50e9eced4a045c6d1e71abc5203b73da9673abc0.zip |
Some tasks on single server only
Diffstat (limited to 'src/main/java')
8 files changed, 54 insertions, 9 deletions
diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index ac2adca8a..dcd9aaa28 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -1937,8 +1937,8 @@ public final class Keys { "time,position,speed,course,accuracy,result"); /** - * Broadcast method. Available options are "multicast" and "redis". By default (if the value is not - * specified or does not matches available options) server disables broadcast. + * Broadcast method. Available options are "multicast" and "redis". By default, (if the value is not + * specified or does not match available options) server disables broadcast. */ public static final ConfigKey<String> BROADCAST_TYPE = new StringConfigKey( "broadcast.type", @@ -1965,4 +1965,11 @@ public final class Keys { "broadcast.port", List.of(KeyType.CONFIG)); + /** + * Flag to mark secondary servers. Some tasks, like scheduled reports, will be executed on the main server only. + */ + public static final ConfigKey<Boolean> BROADCAST_SECONDARY = new BooleanConfigKey( + "broadcast.secondary", + List.of(KeyType.CONFIG)); + } diff --git a/src/main/java/org/traccar/schedule/ScheduleManager.java b/src/main/java/org/traccar/schedule/ScheduleManager.java index 742428fd8..fd8171148 100644 --- a/src/main/java/org/traccar/schedule/ScheduleManager.java +++ b/src/main/java/org/traccar/schedule/ScheduleManager.java @@ -20,6 +20,9 @@ import org.traccar.LifecycleObject; import jakarta.inject.Inject; import jakarta.inject.Singleton; +import org.traccar.config.Config; +import org.traccar.config.Keys; + import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.stream.Stream; @@ -28,11 +31,13 @@ import java.util.stream.Stream; public class ScheduleManager implements LifecycleObject { private final Injector injector; + private final boolean secondary; private ScheduledExecutorService executor; @Inject - public ScheduleManager(Injector injector) { + public ScheduleManager(Injector injector, Config config) { this.injector = injector; + secondary = config.getBoolean(Keys.BROADCAST_SECONDARY); } @Override @@ -46,7 +51,12 @@ public class ScheduleManager implements LifecycleObject { TaskReports.class, TaskDeviceInactivityCheck.class, TaskWebSocketKeepalive.class) - .forEachOrdered(task -> injector.getInstance(task).schedule(executor)); + .forEachOrdered(taskClass -> { + var task = injector.getInstance(taskClass); + if (task.multipleInstances() || !secondary) { + task.schedule(executor); + } + }); } @Override diff --git a/src/main/java/org/traccar/schedule/ScheduleTask.java b/src/main/java/org/traccar/schedule/ScheduleTask.java index 1b537213b..a2a31ac4f 100644 --- a/src/main/java/org/traccar/schedule/ScheduleTask.java +++ b/src/main/java/org/traccar/schedule/ScheduleTask.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Anton Tananaev (anton@traccar.org) + * Copyright 2022 - 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. @@ -18,5 +18,10 @@ package org.traccar.schedule; import java.util.concurrent.ScheduledExecutorService; public interface ScheduleTask extends Runnable { + + default boolean multipleInstances() { + return true; + } + void schedule(ScheduledExecutorService executor); } diff --git a/src/main/java/org/traccar/schedule/SingleScheduleTask.java b/src/main/java/org/traccar/schedule/SingleScheduleTask.java new file mode 100644 index 000000000..a0f1deee4 --- /dev/null +++ b/src/main/java/org/traccar/schedule/SingleScheduleTask.java @@ -0,0 +1,23 @@ +/* + * 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; + +public abstract class SingleScheduleTask implements ScheduleTask { + @Override + public boolean multipleInstances() { + return false; + } +} diff --git a/src/main/java/org/traccar/schedule/TaskDeleteTemporary.java b/src/main/java/org/traccar/schedule/TaskDeleteTemporary.java index 0cead59fb..eb49b7e46 100644 --- a/src/main/java/org/traccar/schedule/TaskDeleteTemporary.java +++ b/src/main/java/org/traccar/schedule/TaskDeleteTemporary.java @@ -28,7 +28,7 @@ import java.util.Date; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -public class TaskDeleteTemporary implements ScheduleTask { +public class TaskDeleteTemporary extends SingleScheduleTask { private static final Logger LOGGER = LoggerFactory.getLogger(TaskDeleteTemporary.class); diff --git a/src/main/java/org/traccar/schedule/TaskDeviceInactivityCheck.java b/src/main/java/org/traccar/schedule/TaskDeviceInactivityCheck.java index 8e45568d5..9bc98d8d3 100644 --- a/src/main/java/org/traccar/schedule/TaskDeviceInactivityCheck.java +++ b/src/main/java/org/traccar/schedule/TaskDeviceInactivityCheck.java @@ -34,7 +34,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; -public class TaskDeviceInactivityCheck implements ScheduleTask { +public class TaskDeviceInactivityCheck extends SingleScheduleTask { private static final Logger LOGGER = LoggerFactory.getLogger(TaskDeviceInactivityCheck.class); diff --git a/src/main/java/org/traccar/schedule/TaskExpirations.java b/src/main/java/org/traccar/schedule/TaskExpirations.java index e16dcd86c..03be0c97f 100644 --- a/src/main/java/org/traccar/schedule/TaskExpirations.java +++ b/src/main/java/org/traccar/schedule/TaskExpirations.java @@ -36,7 +36,7 @@ import org.traccar.storage.query.Request; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -public class TaskExpirations implements ScheduleTask { +public class TaskExpirations extends SingleScheduleTask { private static final Logger LOGGER = LoggerFactory.getLogger(TaskExpirations.class); diff --git a/src/main/java/org/traccar/schedule/TaskReports.java b/src/main/java/org/traccar/schedule/TaskReports.java index c57158cb9..1b9b15749 100644 --- a/src/main/java/org/traccar/schedule/TaskReports.java +++ b/src/main/java/org/traccar/schedule/TaskReports.java @@ -50,7 +50,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; -public class TaskReports implements ScheduleTask { +public class TaskReports extends SingleScheduleTask { private static final Logger LOGGER = LoggerFactory.getLogger(TaskReports.class); |