Update scheduler internals + add hourly scheduler

This commit is contained in:
JeremyStar™ 2024-04-30 21:45:36 +02:00
parent 943750109c
commit 5d51ae33b0
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
2 changed files with 59 additions and 5 deletions

View file

@ -42,6 +42,8 @@ public final class BuildConfiguration {
// Verbose logging // Verbose logging
public static final boolean provideVerboseLoggingForSpellingHelpers = false; public static final boolean provideVerboseLoggingForSpellingHelpers = false;
public static final boolean provideVerboseLoggingForSpellingCompletions = false; public static final boolean provideVerboseLoggingForSpellingCompletions = false;
public static final boolean announceSchedulingActionsInLogIfPlayers = true;
public static final boolean announceSchedulingActionsInLogIfAlone = true;
// Command configuration // Command configuration
// -> /msg & /reply // -> /msg & /reply

View file

@ -18,7 +18,9 @@
*/ */
package de.pickshadow.plugin; package de.pickshadow.plugin;
import de.pickshadow.plugin.classes.Configuration;
import de.pickshadow.plugin.classes.logger.LoggerInstanceImpl; import de.pickshadow.plugin.classes.logger.LoggerInstanceImpl;
import de.pickshadow.plugin.utils.Miscellaneous;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import java.util.UUID; import java.util.UUID;
@ -26,18 +28,68 @@ import java.util.UUID;
public class Scheduler { public class Scheduler {
LoggerInstanceImpl logger; LoggerInstanceImpl logger;
// Keeps track on how many times the scheduler has been run
// Normally this isn't required, this however prevents duplicate
// scheduler calls in case /pssp scheduler <timeframe> is used.
public int execsMinutely = 0;
public int execsHourly = 0;
public void initializeScheduler() { public void initializeScheduler() {
logger = ObjHolder.logger.getInstance(getClass()); logger = ObjHolder.logger.getInstance(getClass());
logger.verb("Scheduling tasks"); logger.verb("Initializing scheduler");
Bukkit.getScheduler().runTaskLater(ObjHolder.main, this::runMinuteTasks, 1200L); Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runMinuteTasks(execsMinutely), 1200L);
Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runHourTasks(execsHourly), 72000L);
} }
public void runMinuteTasks() { public void runMinuteTasks(int execs) {
logger.diag("Running minutely tasks"); // If execution count does not match return
if (execs != execsMinutely) return;
// Print announcement
printTaskAnnouncement("minutely");
// Increase execution count to prevent additional calls
execsMinutely++;
// Save player data // Save player data
for (UUID uuid : ObjHolder.playerDataLoader.getLoadedUUIDs()) ObjHolder.playerDataLoader.savePlayerData(uuid); for (UUID uuid : ObjHolder.playerDataLoader.getLoadedUUIDs()) ObjHolder.playerDataLoader.savePlayerData(uuid);
// Schedule // Schedule
Bukkit.getScheduler().runTaskLater(ObjHolder.main, this::runMinuteTasks, 1200L); Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runMinuteTasks(execsMinutely), 1200L);
}
public void runHourTasks(int execs) {
// If execution count does not match return
if (execs != execsHourly) return;
// Print announcement
printTaskAnnouncement("hourly");
// Increase execution count to prevent additional calls
execsHourly++;
// Reload configuration file
Configuration newConfig = ObjHolder.configLoader.loadConfig();
if (newConfig == null) {
logger.error("Unable to reload configuration file automatically");
} else {
ObjHolder.config = newConfig;
}
// Run garbage collection (old gen gc)
Miscellaneous.threadedGc();
// Schedule
Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runHourTasks(execsHourly), 72000L);
}
private void printTaskAnnouncement(String timeframe) {
if (Bukkit.getOnlinePlayers().isEmpty() && !BuildConfiguration.announceSchedulingActionsInLogIfAlone) return;
if (!Bukkit.getOnlinePlayers().isEmpty() && !BuildConfiguration.announceSchedulingActionsInLogIfPlayers) return;
logger.diag("Running " + timeframe + " tasks");
} }
} }