Task for network to update configs in database on boot

This commit is contained in:
Rsl1122 2019-01-12 13:18:26 +02:00
parent c6c24f8c0f
commit 8edc621761
4 changed files with 101 additions and 16 deletions

View File

@ -21,6 +21,7 @@ import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.TimeSettings;
import com.djrapitops.plan.system.tasks.bungee.BungeeTPSCountTimer;
import com.djrapitops.plan.system.tasks.bungee.PingCountTimerBungee;
import com.djrapitops.plan.system.tasks.proxy.NetworkConfigStoreTask;
import com.djrapitops.plan.system.tasks.proxy.NetworkPageRefreshTask;
import com.djrapitops.plugin.api.TimeAmount;
import com.djrapitops.plugin.task.RunnableFactory;
@ -41,6 +42,7 @@ public class BungeeTaskSystem extends TaskSystem {
private final PingCountTimerBungee pingCountTimer;
private final LogsFolderCleanTask logsFolderCleanTask;
private final PlayersPageRefreshTask playersPageRefreshTask;
private final NetworkConfigStoreTask networkConfigStoreTask;
@Inject
public BungeeTaskSystem(
@ -51,7 +53,9 @@ public class BungeeTaskSystem extends TaskSystem {
NetworkPageRefreshTask networkPageRefreshTask,
PingCountTimerBungee pingCountTimer,
LogsFolderCleanTask logsFolderCleanTask,
PlayersPageRefreshTask playersPageRefreshTask) {
PlayersPageRefreshTask playersPageRefreshTask,
NetworkConfigStoreTask networkConfigStoreTask
) {
super(runnableFactory, bungeeTPSCountTimer);
this.plugin = plugin;
this.config = config;
@ -60,6 +64,7 @@ public class BungeeTaskSystem extends TaskSystem {
this.pingCountTimer = pingCountTimer;
this.logsFolderCleanTask = logsFolderCleanTask;
this.playersPageRefreshTask = playersPageRefreshTask;
this.networkConfigStoreTask = networkConfigStoreTask;
}
@Override
@ -78,5 +83,9 @@ public class BungeeTaskSystem extends TaskSystem {
registerTask(playersPageRefreshTask)
.runTaskTimerAsynchronously(TimeAmount.toTicks(5L, TimeUnit.MINUTES), TimeAmount.toTicks(5L, TimeUnit.MINUTES));
// +40 ticks / 2 seconds so that update check task runs first.
long storeDelay = TimeAmount.toTicks(config.get(TimeSettings.CONFIG_UPDATE_INTERVAL), TimeUnit.MILLISECONDS) + 40;
registerTask("Config Store Task", networkConfigStoreTask).runTaskLaterAsynchronously(storeDelay);
}
}

View File

@ -25,13 +25,13 @@ import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.settings.config.*;
import com.djrapitops.plan.system.settings.paths.PluginSettings;
import com.djrapitops.plan.system.settings.paths.TimeSettings;
import com.djrapitops.plan.system.tasks.TaskSystem;
import com.djrapitops.plan.utilities.file.FileWatcher;
import com.djrapitops.plan.utilities.file.WatchedFile;
import com.djrapitops.plugin.api.TimeAmount;
import com.djrapitops.plugin.logging.console.PluginLogger;
import com.djrapitops.plugin.logging.error.ErrorHandler;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.RunnableFactory;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -60,7 +60,7 @@ public class NetworkSettingManager implements SubSystem {
private final PlanFiles files;
private final DBSystem dbSystem;
private final ServerInfo serverInfo;
private final TaskSystem taskSystem;
private final RunnableFactory runnableFactory;
private PlanConfig config;
private final PluginLogger logger;
private ErrorHandler errorHandler;
@ -75,7 +75,7 @@ public class NetworkSettingManager implements SubSystem {
PlanConfig config,
DBSystem dbSystem,
ServerInfo serverInfo,
TaskSystem taskSystem,
RunnableFactory runnableFactory,
PluginLogger logger,
ErrorHandler errorHandler
) {
@ -83,7 +83,7 @@ public class NetworkSettingManager implements SubSystem {
this.config = config;
this.dbSystem = dbSystem;
this.serverInfo = serverInfo;
this.taskSystem = taskSystem;
this.runnableFactory = runnableFactory;
this.logger = logger;
this.errorHandler = errorHandler;
@ -107,10 +107,16 @@ public class NetworkSettingManager implements SubSystem {
}
}
public static UUID getServerUUIDFromFilename(File file) {
String fileName = file.getName();
String uuidString = fileName.substring(0, fileName.length() - 4);
return UUID.fromString(uuidString);
}
private FileWatcher prepareFileWatcher() {
FileWatcher fileWatcher = new FileWatcher(serverSettingsFolder, errorHandler);
File[] files = serverSettingsFolder.listFiles((dir, name) -> name.endsWith(".yml"));
File[] files = getConfigFiles();
if (files != null) {
for (File file : files) {
addFileToWatchList(fileWatcher, file);
@ -120,11 +126,13 @@ public class NetworkSettingManager implements SubSystem {
return fileWatcher;
}
public File[] getConfigFiles() {
return serverSettingsFolder.listFiles((dir, name) -> name.endsWith(".yml"));
}
private void addFileToWatchList(FileWatcher fileWatcher, File file) {
try {
String fileName = file.getName();
String uuidString = fileName.substring(0, fileName.length() - 4);
UUID serverUUID = UUID.fromString(uuidString);
UUID serverUUID = getServerUUIDFromFilename(file);
fileWatcher.addToWatchlist(new WatchedFile(file, () -> updateConfigInDB(file, serverUUID)));
} catch (IndexOutOfBoundsException | IllegalArgumentException ignore) {
@ -134,10 +142,10 @@ public class NetworkSettingManager implements SubSystem {
private void scheduleDBCheckTask() {
long checkPeriod = TimeAmount.toTicks(config.get(TimeSettings.CONFIG_UPDATE_INTERVAL), TimeUnit.MILLISECONDS);
taskSystem.registerTask("Config Update DB Checker", new AbsRunnable() {
runnableFactory.create("Config Update DB Checker", new AbsRunnable() {
@Override
public void run() {
checkDBForNewConfigSettings();
updateConfigFromDBIfUpdated();
}
}).runTaskTimerAsynchronously(checkPeriod, checkPeriod);
}
@ -156,17 +164,18 @@ public class NetworkSettingManager implements SubSystem {
return new File(serverSettingsFolder, serverUUID + ".yml");
}
private void checkDBForNewConfigSettings() {
private void updateConfigFromDBIfUpdated() {
Database database = dbSystem.getDatabase();
List<UUID> serverUUIDs = database.fetch().getServerUUIDs();
// Remove the proxy server from the list
serverUUIDs.remove(serverInfo.getServerUUID());
for (UUID serverUUID : serverUUIDs) {
checkDBForNewConfigSettings(database, serverUUID);
updateConfigFromDBIfUpdated(database, serverUUID);
}
}
private void checkDBForNewConfigSettings(Database database, UUID serverUUID) {
private void updateConfigFromDBIfUpdated(Database database, UUID serverUUID) {
File configFile = getServerConfigFile(serverUUID);
long lastModified = configFile.exists() ? configFile.lastModified() : -1;
@ -185,7 +194,7 @@ public class NetworkSettingManager implements SubSystem {
}
}
private void updateConfigInDB(File file, UUID serverUUID) {
public void updateConfigInDB(File file, UUID serverUUID) {
if (!file.exists()) {
return;
}

View File

@ -0,0 +1,58 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.system.tasks.proxy;
import com.djrapitops.plan.system.settings.network.NetworkSettingManager;
import com.djrapitops.plugin.task.AbsRunnable;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.File;
import java.util.UUID;
/**
* Task on networks that stores server configs in /plugins/Plan/serverConfiguration in database on boot.
*
* @author Rsl1122
*/
@Singleton
public class NetworkConfigStoreTask extends AbsRunnable {
private final NetworkSettingManager networkSettingManager;
@Inject
public NetworkConfigStoreTask(
NetworkSettingManager networkSettingManager
) {
this.networkSettingManager = networkSettingManager;
}
@Override
public void run() {
updateDBConfigs();
cancel();
}
private void updateDBConfigs() {
File[] configFiles = networkSettingManager.getConfigFiles();
for (File configFile : configFiles) {
UUID serverUUID = NetworkSettingManager.getServerUUIDFromFilename(configFile);
networkSettingManager.updateConfigInDB(configFile, serverUUID);
}
}
}

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.system.tasks;
import com.djrapitops.plan.PlanVelocity;
import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.TimeSettings;
import com.djrapitops.plan.system.tasks.proxy.NetworkConfigStoreTask;
import com.djrapitops.plan.system.tasks.proxy.NetworkPageRefreshTask;
import com.djrapitops.plan.system.tasks.velocity.PingCountTimerVelocity;
import com.djrapitops.plan.system.tasks.velocity.VelocityTPSCountTimer;
@ -41,6 +42,7 @@ public class VelocityTaskSystem extends TaskSystem {
private final PingCountTimerVelocity pingCountTimer;
private final LogsFolderCleanTask logsFolderCleanTask;
private final PlayersPageRefreshTask playersPageRefreshTask;
private final NetworkConfigStoreTask networkConfigStoreTask;
@Inject
public VelocityTaskSystem(
@ -51,7 +53,9 @@ public class VelocityTaskSystem extends TaskSystem {
NetworkPageRefreshTask networkPageRefreshTask,
PingCountTimerVelocity pingCountTimer,
LogsFolderCleanTask logsFolderCleanTask,
PlayersPageRefreshTask playersPageRefreshTask) {
PlayersPageRefreshTask playersPageRefreshTask,
NetworkConfigStoreTask networkConfigStoreTask
) {
super(runnableFactory, velocityTPSCountTimer);
this.plugin = plugin;
this.config = config;
@ -60,6 +64,7 @@ public class VelocityTaskSystem extends TaskSystem {
this.pingCountTimer = pingCountTimer;
this.logsFolderCleanTask = logsFolderCleanTask;
this.playersPageRefreshTask = playersPageRefreshTask;
this.networkConfigStoreTask = networkConfigStoreTask;
}
@Override
@ -78,5 +83,9 @@ public class VelocityTaskSystem extends TaskSystem {
registerTask(playersPageRefreshTask)
.runTaskTimerAsynchronously(TimeAmount.toTicks(5L, TimeUnit.MINUTES), TimeAmount.toTicks(5L, TimeUnit.MINUTES));
// +40 ticks / 2 seconds so that update check task runs first.
long storeDelay = TimeAmount.toTicks(config.get(TimeSettings.CONFIG_UPDATE_INTERVAL), TimeUnit.MILLISECONDS) + 40;
registerTask("Config Store Task", networkConfigStoreTask).runTaskLaterAsynchronously(storeDelay);
}
}