BackupHelper cleanup

2.x-1.16
szymon 2021-06-19 15:39:00 +02:00
parent 725d4098be
commit fcc1dca958
1 changed files with 16 additions and 67 deletions

View File

@ -34,9 +34,7 @@ import java.time.LocalDateTime;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
public class BackupHelper { public class BackupHelper {
public static Runnable create(BackupContext ctx) { public static Runnable create(BackupContext ctx) {
@ -67,43 +65,7 @@ public class BackupHelper {
try { try {
ctx.getServer().save(false, true, true); ctx.getServer().save(false, true, true);
} catch (Exception e) { } catch (Exception e) {
Statics.LOGGER.sendErrorAL(ctx,"An exception occurred when trying to save the world!" Statics.LOGGER.sendErrorAL(ctx,"An exception occurred when trying to save the world!");
);
/*
MutableText text = Statics.LOGGER.getPrefixText()
.append(new LiteralText("In order for backup to be up-to-date call ").formatted(Formatting.WHITE))
.append(
new LiteralText("[/save-all flush]")
.styled(
style -> style
.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/save-all flush"))
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new LiteralText("Click!")))
.withColor(Formatting.BLUE)
)
)
.append(new LiteralText(" and then re-run the backup.").formatted(Formatting.WHITE));
ctx.getCommandSource().sendFeedback(text, false);
text = Statics.LOGGER.getPrefixText()
.append(new LiteralText("This is known issue (See ").formatted(Formatting.WHITE))
.append(
new LiteralText("https://github.com/Szum123321/textile_backup/issues/42")
.styled(
style -> style
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://github.com/Szum123321/textile_backup/issues/42"))
.withColor(Formatting.BLUE)
)
)
.append(new LiteralText(")").formatted(Formatting.WHITE));
ctx.getCommandSource().sendFeedback(text, false);
if(ctx.startedByPlayer())
Statics.LOGGER.sendError(ctx, "If you have access to server console please take a look at it.");
Statics.LOGGER.error("Please let me know about this situation, include below error, mod's config, additional mods, where is the server running etc.", e);
*/
} }
} }
@ -118,8 +80,7 @@ public class BackupHelper {
if(ctx.getInitiator().equals(ActionInitiator.Player) && ctx.getCommandSource().getEntity() != null) if(ctx.getInitiator().equals(ActionInitiator.Player) && ctx.getCommandSource().getEntity() != null)
uuid = ctx.getCommandSource().getEntity().getUuid(); uuid = ctx.getCommandSource().getEntity().getUuid();
else else uuid = Util.NIL_UUID;
uuid = Util.NIL_UUID;
ctx.getServer().getPlayerManager().broadcastChatMessage( ctx.getServer().getPlayerManager().broadcastChatMessage(
message, message,
@ -130,51 +91,39 @@ public class BackupHelper {
public static int executeFileLimit(ServerCommandSource ctx, String worldName) { public static int executeFileLimit(ServerCommandSource ctx, String worldName) {
File root = Utilities.getBackupRootPath(worldName); File root = Utilities.getBackupRootPath(worldName);
AtomicInteger deletedFiles = new AtomicInteger(); int deletedFiles = 0;
if (root.isDirectory() && root.exists() && root.listFiles() != null) { if (root.isDirectory() && root.exists() && root.listFiles() != null) {
if (Statics.CONFIG.maxAge > 0) { // delete files older that configured if (Statics.CONFIG.maxAge > 0) { // delete files older that configured
final LocalDateTime now = LocalDateTime.now(); final LocalDateTime now = LocalDateTime.now();
Arrays.stream(root.listFiles()) deletedFiles += Arrays.stream(root.listFiles())
.filter(Utilities::isValidBackup)// We check if we can get file's creation date so that the next line won't throw an exception .filter(Utilities::isValidBackup)// We check if we can get file's creation date so that the next line won't throw an exception
.filter(f -> now.toEpochSecond(ZoneOffset.UTC) - Utilities.getFileCreationTime(f).get().toEpochSecond(ZoneOffset.UTC) > Statics.CONFIG.maxAge) .filter(f -> now.toEpochSecond(ZoneOffset.UTC) - Utilities.getFileCreationTime(f).get().toEpochSecond(ZoneOffset.UTC) > Statics.CONFIG.maxAge)
.forEach(f -> { .map(f -> deleteFile(f, ctx))
if(deleteFile(f, ctx)) .filter(b -> b).count(); //a bit awkward
deletedFiles.getAndIncrement();
});
} }
if (Statics.CONFIG.backupsToKeep > 0 && root.listFiles().length > Statics.CONFIG.backupsToKeep) { if (Statics.CONFIG.backupsToKeep > 0 && root.listFiles().length > Statics.CONFIG.backupsToKeep) {
int i = root.listFiles().length; deletedFiles += Arrays.stream(root.listFiles())
Iterator<File> it = Arrays.stream(root.listFiles())
.filter(Utilities::isValidBackup) .filter(Utilities::isValidBackup)
.sorted(Comparator.comparing(f -> Utilities.getFileCreationTime(f).get())) .sorted(Comparator.comparing(f -> Utilities.getFileCreationTime((File) f).get()).reversed())
.iterator(); .skip(Statics.CONFIG.backupsToKeep)
.map(f -> deleteFile(f, ctx))
while(i > Statics.CONFIG.backupsToKeep && it.hasNext()) { .filter(b -> b).count();
if(deleteFile(it.next(), ctx))
deletedFiles.getAndIncrement();
i--;
}
} }
if (Statics.CONFIG.maxSize > 0 && FileUtils.sizeOfDirectory(root) / 1024 > Statics.CONFIG.maxSize) { if (Statics.CONFIG.maxSize > 0 && FileUtils.sizeOfDirectory(root) / 1024 > Statics.CONFIG.maxSize) {
Iterator<File> it = Arrays.stream(root.listFiles()) deletedFiles += Arrays.stream(root.listFiles())
.filter(Utilities::isValidBackup) .filter(Utilities::isValidBackup)
.sorted(Comparator.comparing(f -> Utilities.getFileCreationTime(f).get())) .sorted(Comparator.comparing(f -> Utilities.getFileCreationTime(f).get()))
.iterator(); .takeWhile(f -> FileUtils.sizeOfDirectory(root) / 1024 > Statics.CONFIG.maxSize)
.map(f -> deleteFile(f, ctx))
while(FileUtils.sizeOfDirectory(root) / 1024 > Statics.CONFIG.maxSize && it.hasNext()) { .filter(b -> b).count();
if(deleteFile(it.next(), ctx))
deletedFiles.getAndIncrement();
}
} }
} }
return deletedFiles.get(); return deletedFiles;
} }
private static boolean deleteFile(File f, ServerCommandSource ctx) { private static boolean deleteFile(File f, ServerCommandSource ctx) {