made cleanup more readable
This commit is contained in:
parent
21cf46a56a
commit
c22eb7a3b2
@ -30,8 +30,8 @@ import java.nio.file.Files;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.stream.BaseStream;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class Cleanup {
|
public class Cleanup {
|
||||||
@ -47,11 +47,11 @@ public class Cleanup {
|
|||||||
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);
|
||||||
|
|
||||||
deletedFiles += RestoreableFile.applyOnFiles(root, 0,
|
deletedFiles += RestoreableFile.applyOnFiles(root, 0L,
|
||||||
e -> log.error("An exception occurred while trying to delete old files!", e),
|
e -> log.error("An exception occurred while trying to delete an old files!", e),
|
||||||
stream -> stream.filter(f -> now - f.getCreationTime().toEpochSecond(ZoneOffset.UTC) > config.get().maxAge)
|
stream -> stream.filter(f -> now - f.getCreationTime().toEpochSecond(ZoneOffset.UTC) > config.get().maxAge)
|
||||||
.mapToInt(f -> deleteFile(f.getFile(), ctx))
|
.filter(f -> deleteFile(f.getFile(), ctx))
|
||||||
.sum()
|
.count()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,26 +59,26 @@ public class Cleanup {
|
|||||||
final long maxSize = config.get().maxSize > 0 ? config.get().maxSize * 1024: Long.MAX_VALUE;
|
final long maxSize = config.get().maxSize > 0 ? config.get().maxSize * 1024: Long.MAX_VALUE;
|
||||||
|
|
||||||
long[] counts = count(root);
|
long[] counts = count(root);
|
||||||
|
long n = counts[0], size = counts[1];
|
||||||
|
|
||||||
AtomicInteger currentNo = new AtomicInteger((int) counts[0]);
|
var it = RestoreableFile.applyOnFiles(root, null,
|
||||||
AtomicLong currentSize = new AtomicLong(counts[1]);
|
e -> log.error("An exception occurred while trying to delete old files!", e), BaseStream::iterator);
|
||||||
|
|
||||||
deletedFiles += RestoreableFile.applyOnFiles(root, 0,
|
if(Objects.isNull(it)) return deletedFiles;
|
||||||
e -> log.error("An exception occurred while trying to delete old files!", e),
|
|
||||||
stream -> stream.sequential()
|
while(it.hasNext() && (n > noToKeep || size > maxSize)) {
|
||||||
.takeWhile(f -> (currentNo.get() > noToKeep) || (currentSize.get() > maxSize))
|
Path f = it.next().getFile();
|
||||||
.map(RestoreableFile::getFile)
|
long x;
|
||||||
.peek(f -> {
|
try {
|
||||||
try {
|
x = Files.size(f);
|
||||||
currentSize.addAndGet(-Files.size(f));
|
} catch (IOException e) { size = 0; continue; }
|
||||||
} catch (IOException e) {
|
|
||||||
currentSize.set(0);
|
if(!deleteFile(f, ctx)) continue;
|
||||||
return;
|
|
||||||
}
|
size -= x;
|
||||||
currentNo.decrementAndGet();
|
n--;
|
||||||
})
|
deletedFiles++;
|
||||||
.mapToInt(f -> deleteFile(f, ctx))
|
}
|
||||||
.sum());
|
|
||||||
|
|
||||||
return deletedFiles;
|
return deletedFiles;
|
||||||
}
|
}
|
||||||
@ -111,16 +111,16 @@ public class Cleanup {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//1 -> ok, 0 -> err
|
//1 -> ok, 0 -> err
|
||||||
private static int deleteFile(Path f, ServerCommandSource ctx) {
|
private static boolean deleteFile(Path f, ServerCommandSource ctx) {
|
||||||
if(Globals.INSTANCE.getLockedFile().filter(p -> p == f).isPresent()) return 0;
|
if(Globals.INSTANCE.getLockedFile().filter(p -> p == f).isPresent()) return false;
|
||||||
try {
|
try {
|
||||||
Files.delete(f);
|
Files.delete(f);
|
||||||
log.sendInfoAL(ctx, "Deleted: {}", f);
|
log.sendInfoAL(ctx, "Deleted: {}", f);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if(Utilities.wasSentByPlayer(ctx)) log.sendError(ctx, "Something went wrong while deleting: {}.", f);
|
if(Utilities.wasSentByPlayer(ctx)) log.sendError(ctx, "Something went wrong while deleting: {}.", f);
|
||||||
log.error("Something went wrong while deleting: {}.", f, e);
|
log.error("Something went wrong while deleting: {}.", f, e);
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -32,7 +32,6 @@ import net.szum123321.textile_backup.mixin.MinecraftServerSessionAccessor;
|
|||||||
import org.apache.commons.io.file.SimplePathVisitor;
|
import org.apache.commons.io.file.SimplePathVisitor;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.FileVisitResult;
|
import java.nio.file.FileVisitResult;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
Loading…
Reference in New Issue
Block a user