Update scheduler internals + add hourly scheduler
This commit is contained in:
parent
943750109c
commit
5d51ae33b0
2 changed files with 59 additions and 5 deletions
|
@ -42,6 +42,8 @@ public final class BuildConfiguration {
|
|||
// Verbose logging
|
||||
public static final boolean provideVerboseLoggingForSpellingHelpers = false;
|
||||
public static final boolean provideVerboseLoggingForSpellingCompletions = false;
|
||||
public static final boolean announceSchedulingActionsInLogIfPlayers = true;
|
||||
public static final boolean announceSchedulingActionsInLogIfAlone = true;
|
||||
|
||||
// Command configuration
|
||||
// -> /msg & /reply
|
||||
|
|
|
@ -18,7 +18,9 @@
|
|||
*/
|
||||
package de.pickshadow.plugin;
|
||||
|
||||
import de.pickshadow.plugin.classes.Configuration;
|
||||
import de.pickshadow.plugin.classes.logger.LoggerInstanceImpl;
|
||||
import de.pickshadow.plugin.utils.Miscellaneous;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.util.UUID;
|
||||
|
@ -26,18 +28,68 @@ import java.util.UUID;
|
|||
public class Scheduler {
|
||||
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() {
|
||||
logger = ObjHolder.logger.getInstance(getClass());
|
||||
logger.verb("Scheduling tasks");
|
||||
Bukkit.getScheduler().runTaskLater(ObjHolder.main, this::runMinuteTasks, 1200L);
|
||||
logger.verb("Initializing scheduler");
|
||||
Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runMinuteTasks(execsMinutely), 1200L);
|
||||
Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runHourTasks(execsHourly), 72000L);
|
||||
}
|
||||
|
||||
public void runMinuteTasks() {
|
||||
logger.diag("Running minutely tasks");
|
||||
public void runMinuteTasks(int execs) {
|
||||
// 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
|
||||
for (UUID uuid : ObjHolder.playerDataLoader.getLoadedUUIDs()) ObjHolder.playerDataLoader.savePlayerData(uuid);
|
||||
|
||||
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue