Separated BackupHelper into the factory and the cleanup (MakeBackupRunnableFactory & Cleanup)

2.x
Szum123321 2022-08-30 01:31:43 +02:00
parent b7da7dbc6f
commit b3a340deab
8 changed files with 103 additions and 71 deletions

View File

@ -39,8 +39,8 @@ import net.szum123321.textile_backup.config.ConfigHelper;
import net.szum123321.textile_backup.config.ConfigPOJO; import net.szum123321.textile_backup.config.ConfigPOJO;
import net.szum123321.textile_backup.core.ActionInitiator; import net.szum123321.textile_backup.core.ActionInitiator;
import net.szum123321.textile_backup.core.create.BackupContext; import net.szum123321.textile_backup.core.create.BackupContext;
import net.szum123321.textile_backup.core.create.BackupHelper;
import net.szum123321.textile_backup.core.create.BackupScheduler; import net.szum123321.textile_backup.core.create.BackupScheduler;
import net.szum123321.textile_backup.core.create.MakeBackupRunnableFactory;
public class TextileBackup implements ModInitializer { public class TextileBackup implements ModInitializer {
public static final String MOD_NAME = "Textile Backup"; public static final String MOD_NAME = "Textile Backup";
@ -67,7 +67,7 @@ public class TextileBackup implements ModInitializer {
Globals.INSTANCE.shutdownQueueExecutor(60000); Globals.INSTANCE.shutdownQueueExecutor(60000);
if (config.get().shutdownBackup && Globals.INSTANCE.globalShutdownBackupFlag.get()) { if (config.get().shutdownBackup && Globals.INSTANCE.globalShutdownBackupFlag.get()) {
BackupHelper.create( MakeBackupRunnableFactory.create(
BackupContext.Builder BackupContext.Builder
.newBackupContextBuilder() .newBackupContextBuilder()
.setServer(server) .setServer(server)

View File

@ -23,7 +23,7 @@ import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.szum123321.textile_backup.TextileBackup; import net.szum123321.textile_backup.TextileBackup;
import net.szum123321.textile_backup.TextileLogger; import net.szum123321.textile_backup.TextileLogger;
import net.szum123321.textile_backup.core.create.BackupHelper; import net.szum123321.textile_backup.core.Cleanup;
import net.szum123321.textile_backup.core.Utilities; import net.szum123321.textile_backup.core.Utilities;
public class CleanupCommand { public class CleanupCommand {
@ -38,7 +38,7 @@ public class CleanupCommand {
log.sendInfo( log.sendInfo(
source, source,
"Deleted: {} files.", "Deleted: {} files.",
BackupHelper.executeFileLimit(source, Utilities.getLevelName(source.getServer())) Cleanup.executeFileLimit(source, Utilities.getLevelName(source.getServer()))
); );
return 1; return 1;

View File

@ -26,7 +26,7 @@ import net.szum123321.textile_backup.Globals;
import net.szum123321.textile_backup.TextileBackup; import net.szum123321.textile_backup.TextileBackup;
import net.szum123321.textile_backup.TextileLogger; import net.szum123321.textile_backup.TextileLogger;
import net.szum123321.textile_backup.core.create.BackupContext; import net.szum123321.textile_backup.core.create.BackupContext;
import net.szum123321.textile_backup.core.create.BackupHelper; import net.szum123321.textile_backup.core.create.MakeBackupRunnableFactory;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -43,7 +43,7 @@ public class StartBackupCommand {
private static int execute(ServerCommandSource source, @Nullable String comment) { private static int execute(ServerCommandSource source, @Nullable String comment) {
try { try {
Globals.INSTANCE.getQueueExecutor().submit( Globals.INSTANCE.getQueueExecutor().submit(
BackupHelper.create( MakeBackupRunnableFactory.create(
BackupContext.Builder BackupContext.Builder
.newBackupContextBuilder() .newBackupContextBuilder()
.setCommandSource(source) .setCommandSource(source)

View File

@ -1,29 +1,29 @@
/* /*
A simple backup mod for Fabric * A simple backup mod for Fabric
Copyright (C) 2020 Szum123321 * Copyright (C) 2022 Szum123321
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
This program is free software: you can redistribute it and/or modify package net.szum123321.textile_backup.core;
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.szum123321.textile_backup.core.create;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.szum123321.textile_backup.Globals; import net.szum123321.textile_backup.Globals;
import net.szum123321.textile_backup.TextileBackup; import net.szum123321.textile_backup.TextileBackup;
import net.szum123321.textile_backup.TextileLogger; import net.szum123321.textile_backup.TextileLogger;
import net.szum123321.textile_backup.config.ConfigHelper; import net.szum123321.textile_backup.config.ConfigHelper;
import net.szum123321.textile_backup.core.Utilities;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
@ -35,55 +35,14 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Stream; import java.util.stream.Stream;
public class BackupHelper { public class Cleanup {
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME); private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
private final static ConfigHelper config = ConfigHelper.INSTANCE; private final static ConfigHelper config = ConfigHelper.INSTANCE;
public static Runnable create(BackupContext ctx) {
if(config.get().broadcastBackupStart) {
Utilities.notifyPlayers(ctx.server(),
"Warning! Server backup will begin shortly. You may experience some lag."
);
} else {
log.sendInfoAL(ctx, "Warning! Server backup will begin shortly. You may experience some lag.");
}
StringBuilder builder = new StringBuilder();
builder.append("Backup started ");
builder.append(ctx.initiator().getPrefix());
if(ctx.startedByPlayer())
builder.append(ctx.commandSource().getDisplayName().getString());
else
builder.append(ctx.initiator().getName());
builder.append(" on: ");
builder.append(Utilities.getDateTimeFormatter().format(LocalDateTime.now()));
log.info(builder.toString());
if (ctx.shouldSave()) {
log.sendInfoAL(ctx, "Saving server...");
ctx.server().getPlayerManager().saveAllPlayerData();
try {
ctx.server().save(false, true, true);
} catch (Exception e) {
log.sendErrorAL(ctx,"An exception occurred when trying to save the world!");
}
}
return new MakeBackupRunnable(ctx);
}
public static int executeFileLimit(ServerCommandSource ctx, String worldName) { public static int executeFileLimit(ServerCommandSource ctx, String worldName) {
Path root = Utilities.getBackupRootPath(worldName); Path root = Utilities.getBackupRootPath(worldName);
int deletedFiles = 0; int deletedFiles = 0;
if (Files.isDirectory(root) && Files.exists(root) && !isEmpty(root)) { if (Files.isDirectory(root) && Files.exists(root) && !isEmpty(root)) {
if (config.get().maxAge > 0) { // delete files older that configured if (config.get().maxAge > 0) { // delete files older that configured
final long now = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC); final long now = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);

View File

@ -44,7 +44,7 @@ public class BackupScheduler {
if(scheduled) { if(scheduled) {
if(nextBackup <= now) { if(nextBackup <= now) {
Globals.INSTANCE.getQueueExecutor().submit( Globals.INSTANCE.getQueueExecutor().submit(
BackupHelper.create( MakeBackupRunnableFactory.create(
BackupContext.Builder BackupContext.Builder
.newBackupContextBuilder() .newBackupContextBuilder()
.setServer(server) .setServer(server)
@ -63,7 +63,7 @@ public class BackupScheduler {
} else if(!config.get().doBackupsOnEmptyServer && server.getPlayerManager().getCurrentPlayerCount() == 0) { } else if(!config.get().doBackupsOnEmptyServer && server.getPlayerManager().getCurrentPlayerCount() == 0) {
if(scheduled && nextBackup <= now) { if(scheduled && nextBackup <= now) {
Globals.INSTANCE.getQueueExecutor().submit( Globals.INSTANCE.getQueueExecutor().submit(
BackupHelper.create( MakeBackupRunnableFactory.create(
BackupContext.Builder BackupContext.Builder
.newBackupContextBuilder() .newBackupContextBuilder()
.setServer(server) .setServer(server)

View File

@ -23,6 +23,7 @@ import net.szum123321.textile_backup.TextileBackup;
import net.szum123321.textile_backup.TextileLogger; import net.szum123321.textile_backup.TextileLogger;
import net.szum123321.textile_backup.config.ConfigHelper; import net.szum123321.textile_backup.config.ConfigHelper;
import net.szum123321.textile_backup.core.ActionInitiator; import net.szum123321.textile_backup.core.ActionInitiator;
import net.szum123321.textile_backup.core.Cleanup;
import net.szum123321.textile_backup.core.create.compressors.*; import net.szum123321.textile_backup.core.create.compressors.*;
import net.szum123321.textile_backup.core.Utilities; import net.szum123321.textile_backup.core.Utilities;
import net.szum123321.textile_backup.core.create.compressors.tar.AbstractTarArchiver; import net.szum123321.textile_backup.core.create.compressors.tar.AbstractTarArchiver;
@ -108,7 +109,7 @@ public class MakeBackupRunnable implements Runnable {
case TAR -> new AbstractTarArchiver().createArchive(world, outFile, context, coreCount); case TAR -> new AbstractTarArchiver().createArchive(world, outFile, context, coreCount);
} }
BackupHelper.executeFileLimit(context.commandSource(), Utilities.getLevelName(context.server())); Cleanup.executeFileLimit(context.commandSource(), Utilities.getLevelName(context.server()));
if(config.get().broadcastBackupDone) { if(config.get().broadcastBackupDone) {
Utilities.notifyPlayers( Utilities.notifyPlayers(

View File

@ -0,0 +1,72 @@
/*
* A simple backup mod for Fabric
* Copyright (C) 2022 Szum123321
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
package net.szum123321.textile_backup.core.create;
import net.szum123321.textile_backup.TextileBackup;
import net.szum123321.textile_backup.TextileLogger;
import net.szum123321.textile_backup.config.ConfigHelper;
import net.szum123321.textile_backup.core.Utilities;
import java.time.LocalDateTime;
public class MakeBackupRunnableFactory {
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
private final static ConfigHelper config = ConfigHelper.INSTANCE;
public static Runnable create(BackupContext ctx) {
if(config.get().broadcastBackupStart) {
Utilities.notifyPlayers(ctx.server(),
"Warning! Server backup will begin shortly. You may experience some lag."
);
} else {
log.sendInfoAL(ctx, "Warning! Server backup will begin shortly. You may experience some lag.");
}
StringBuilder builder = new StringBuilder();
builder.append("Backup started ");
builder.append(ctx.initiator().getPrefix());
if(ctx.startedByPlayer())
builder.append(ctx.commandSource().getDisplayName().getString());
else
builder.append(ctx.initiator().getName());
builder.append(" on: ");
builder.append(Utilities.getDateTimeFormatter().format(LocalDateTime.now()));
log.info(builder.toString());
if (ctx.shouldSave()) {
log.sendInfoAL(ctx, "Saving server...");
ctx.server().getPlayerManager().saveAllPlayerData();
try {
ctx.server().save(false, true, true);
} catch (Exception e) {
log.sendErrorAL(ctx,"An exception occurred when trying to save the world!");
}
}
return new MakeBackupRunnable(ctx);
}
}

View File

@ -27,7 +27,7 @@ import net.szum123321.textile_backup.core.ActionInitiator;
import net.szum123321.textile_backup.core.LivingServer; import net.szum123321.textile_backup.core.LivingServer;
import net.szum123321.textile_backup.core.Utilities; import net.szum123321.textile_backup.core.Utilities;
import net.szum123321.textile_backup.core.create.BackupContext; import net.szum123321.textile_backup.core.create.BackupContext;
import net.szum123321.textile_backup.core.create.BackupHelper; import net.szum123321.textile_backup.core.create.MakeBackupRunnableFactory;
import net.szum123321.textile_backup.core.restore.decompressors.GenericTarDecompressor; import net.szum123321.textile_backup.core.restore.decompressors.GenericTarDecompressor;
import net.szum123321.textile_backup.core.restore.decompressors.ZipDecompressor; import net.szum123321.textile_backup.core.restore.decompressors.ZipDecompressor;
@ -55,7 +55,7 @@ public class RestoreBackupRunnable implements Runnable {
awaitServerShutdown(); awaitServerShutdown();
if(config.get().backupOldWorlds) { if(config.get().backupOldWorlds) {
BackupHelper.create( MakeBackupRunnableFactory.create(
BackupContext.Builder BackupContext.Builder
.newBackupContextBuilder() .newBackupContextBuilder()
.setServer(ctx.server()) .setServer(ctx.server())