#886 Do not include players with lastlogin = 0 in autopurge or default purge

This commit is contained in:
ljacqu
2016-09-18 16:49:34 +02:00
parent 405bd563d8
commit ff9f50f63f
12 changed files with 97 additions and 54 deletions
@@ -219,6 +219,7 @@ public class CommandInitializer {
.description("Purge old data")
.detailedDescription("Purge old AuthMeReloaded data longer than the specified amount of days ago.")
.withArgument("days", "Number of days", false)
.withArgument("all", "Add 'all' at the end to also purge players with lastlogin = 0", true)
.permission(AdminPermission.PURGE)
.executableCommand(PurgeCommand.class)
.build();
@@ -10,7 +10,7 @@ import java.util.Calendar;
import java.util.List;
/**
* Command for purging the data of players which have not been since for a given number
* Command for purging the data of players which have not been online for a given number
* of days. Depending on the settings, this removes player data in third-party plugins as well.
*/
public class PurgeCommand implements ExecutableCommand {
@@ -41,12 +41,24 @@ public class PurgeCommand implements ExecutableCommand {
return;
}
// If second param is available, check that it is equal to "all"
boolean includeLastLoginZeroEntries = false;
if (arguments.size() >= 2) {
if ("all".equals(arguments.get(1))) {
includeLastLoginZeroEntries = true;
} else {
sender.sendMessage("Purge process aborted; use '/authme purge " + days + " all' "
+ "to include users with lastlogin = 0");
return;
}
}
// Create a calender instance to determine the date
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -days);
long until = calendar.getTimeInMillis();
// Run the purge
purgeService.runPurge(sender, until);
purgeService.runPurge(sender, until, includeLastLoginZeroEntries);
}
}
@@ -138,8 +138,8 @@ public class CacheDataSource implements DataSource {
}
@Override
public Set<String> getRecordsToPurge(long until) {
return source.getRecordsToPurge(until);
public Set<String> getRecordsToPurge(long until, boolean includeEntriesWithLastLoginZero) {
return source.getRecordsToPurge(until, includeEntriesWithLastLoginZero);
}
@Override
@@ -74,9 +74,11 @@ public interface DataSource extends Reloadable {
* Get all records in the database whose last login was before the given time.
*
* @param until The minimum last login
* @param includeEntriesWithLastLoginZero Whether entries with lastlogin = 0 should be included or not,
* see <a href="https://github.com/Xephi/AuthMeReloaded/issues/886">issue #886</a>
* @return The account names selected to purge
*/
Set<String> getRecordsToPurge(long until);
Set<String> getRecordsToPurge(long until, boolean includeEntriesWithLastLoginZero);
/**
* Purge the given players from the database.
@@ -15,7 +15,6 @@ import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -211,30 +210,8 @@ public class FlatFile implements DataSource {
}
@Override
public Set<String> getRecordsToPurge(long until) {
BufferedReader br = null;
Set<String> list = new HashSet<>();
try {
br = new BufferedReader(new FileReader(source));
String line;
while ((line = br.readLine()) != null) {
String[] args = line.split(":");
if (args.length >= 4) {
if (Long.parseLong(args[3]) >= until) {
list.add(args[0]);
continue;
}
}
}
} catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage());
return list;
} finally {
silentClose(br);
}
return list;
public Set<String> getRecordsToPurge(long until, boolean includeEntriesWithLastLoginZero) {
throw new UnsupportedOperationException("Flat file no longer supported");
}
@Override
@@ -572,10 +572,13 @@ public class MySQL implements DataSource {
}
@Override
public Set<String> getRecordsToPurge(long until) {
public Set<String> getRecordsToPurge(long until, boolean includeEntriesWithLastLoginZero) {
Set<String> list = new HashSet<>();
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;";
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + " < ?";
if (!includeEntriesWithLastLoginZero) {
select += " AND " + col.LAST_LOGIN + " <> 0";
}
try (Connection con = getConnection();
PreparedStatement selectPst = con.prepareStatement(select)) {
selectPst.setLong(1, until);
@@ -299,10 +299,13 @@ public class SQLite implements DataSource {
}
@Override
public Set<String> getRecordsToPurge(long until) {
public Set<String> getRecordsToPurge(long until, boolean includeEntriesWithLastLoginZero) {
Set<String> list = new HashSet<>();
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;";
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + " < ?";
if (!includeEntriesWithLastLoginZero) {
select += " AND " + col.LAST_LOGIN + " <> 0";
}
try (PreparedStatement selectPst = con.prepareStatement(select)) {
selectPst.setLong(1, until);
try (ResultSet rs = selectPst.executeQuery()) {
@@ -16,6 +16,9 @@ import java.util.Calendar;
import java.util.Collection;
import java.util.Set;
/**
* Initiates purge tasks.
*/
public class PurgeService {
@Inject
@@ -33,6 +36,7 @@ public class PurgeService {
@Inject
private PurgeExecutor purgeExecutor;
/** Keeps track of whether a purge task is currently running. */
private boolean isPurging = false;
PurgeService() {
@@ -55,7 +59,7 @@ public class PurgeService {
calendar.add(Calendar.DATE, -daysBeforePurge);
long until = calendar.getTimeInMillis();
runPurge(null, until);
runPurge(null, until, false);
}
/**
@@ -64,10 +68,11 @@ public class PurgeService {
*
* @param sender Sender running the command
* @param until The last login threshold in milliseconds
* @param includeEntriesWithLastLoginZero True to also purge players with lastlogin = 0, false otherwise
*/
public void runPurge(CommandSender sender, long until) {
public void runPurge(CommandSender sender, long until, boolean includeEntriesWithLastLoginZero) {
//todo: note this should may run async because it may executes a SQL-Query
Set<String> toPurge = dataSource.getRecordsToPurge(until);
Set<String> toPurge = dataSource.getRecordsToPurge(until, includeEntriesWithLastLoginZero);
if (CollectionUtils.isEmpty(toPurge)) {
logAndSendMessage(sender, "No players to purge");
return;