Merge pull request #149 from AuthMe/806-cleanup-task

Implement periodic cleanup task
This commit is contained in:
Gabriele C
2016-08-08 17:24:42 +02:00
committed by GitHub
6 changed files with 152 additions and 1 deletions
@@ -45,6 +45,7 @@ import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.settings.properties.SettingsFieldRetriever;
import fr.xephi.authme.settings.propertymap.PropertyMap;
import fr.xephi.authme.task.CleanupTask;
import fr.xephi.authme.task.purge.PurgeService;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.FileUtils;
@@ -80,6 +81,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import static fr.xephi.authme.settings.properties.EmailSettings.RECALL_PLAYERS;
import static fr.xephi.authme.util.BukkitService.TICKS_PER_MINUTE;
/**
* The AuthMe main class.
@@ -271,6 +273,11 @@ public class AuthMe extends JavaPlugin {
// Purge on start if enabled
PurgeService purgeService = injector.getSingleton(PurgeService.class);
purgeService.runAutoPurge();
// Schedule clean up task
final int cleanupInterval = 5 * TICKS_PER_MINUTE;
CleanupTask cleanupTask = injector.getSingleton(CleanupTask.class);
cleanupTask.runTaskTimerAsynchronously(this, cleanupInterval, cleanupInterval);
}
protected void instantiateServices(Injector injector) {
+20 -1
View File
@@ -1,10 +1,13 @@
package fr.xephi.authme.cache;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.initialization.HasCleanup;
import fr.xephi.authme.initialization.SettingsDependent;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings;
import javax.inject.Inject;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -12,7 +15,7 @@ import java.util.concurrent.ConcurrentHashMap;
* Manages sessions, allowing players to be automatically logged in if they join again
* within a configurable amount of time.
*/
public class SessionManager implements SettingsDependent {
public class SessionManager implements SettingsDependent, HasCleanup {
private static final int MINUTE_IN_MILLIS = 60_000;
// Player -> expiration of session in milliseconds
@@ -72,6 +75,22 @@ public class SessionManager implements SettingsDependent {
// With this reload, the sessions feature has just been disabled, so clear all stored sessions
if (oldEnabled && !enabled) {
sessions.clear();
ConsoleLogger.fine("Sessions disabled: cleared all sessions");
}
}
@Override
public void performCleanup() {
if (!enabled) {
return;
}
final long currentTime = System.currentTimeMillis();
Iterator<Map.Entry<String, Long>> iterator = sessions.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Long> entry = iterator.next();
if (entry.getValue() < currentTime) {
iterator.remove();
}
}
}
}
@@ -0,0 +1,14 @@
package fr.xephi.authme.initialization;
/**
* Common interface for types which have data that becomes outdated
* and that can be cleaned up periodically.
*/
public interface HasCleanup {
/**
* Performs the cleanup action.
*/
void performCleanup();
}
@@ -0,0 +1,26 @@
package fr.xephi.authme.task;
import ch.jalu.injector.Injector;
import fr.xephi.authme.initialization.HasCleanup;
import org.bukkit.scheduler.BukkitRunnable;
import javax.inject.Inject;
/**
* Task run periodically to invoke the cleanup task on services.
*/
public class CleanupTask extends BukkitRunnable {
@Inject
private Injector injector;
CleanupTask() {
}
@Override
public void run() {
for (HasCleanup service : injector.retrieveAllOfType(HasCleanup.class)) {
service.performCleanup();
}
}
}