use ChronoUnit instead of TimeUnit

This commit is contained in:
HorizonCode 2022-07-04 09:46:18 +02:00
parent 626bb3e25a
commit c6eb4c8fdb
3 changed files with 14 additions and 7 deletions

View File

@ -11,6 +11,7 @@ import org.tomlj.TomlArray;
import java.io.File; import java.io.File;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.temporal.ChronoUnit;
import java.util.Date; import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -63,8 +64,10 @@ public class TaskBuilder {
File outputFile = new File(backupDir, fileName); File outputFile = new File(backupDir, fileName);
if (doVAC) { if (doVAC) {
TimeUnit unit = ChronoUnit unit =
TimeUnit.valueOf(getTaskConfig().getStringOrDefault("vacuum.unit", TimeUnit.DAYS.name())); ChronoUnit.valueOf(
getTaskConfig().getStringOrDefault("vacuum.unit", ChronoUnit.DAYS.name()));
int value = getTaskConfig().getIntOrDefault("vacuum.time", 5); int value = getTaskConfig().getIntOrDefault("vacuum.time", 5);
System.out.printf("Adding VacuumTask with lifetime of %d %s%n", value, unit.name()); System.out.printf("Adding VacuumTask with lifetime of %d %s%n", value, unit.name());

View File

@ -4,18 +4,19 @@ import lombok.Getter;
import net.horizoncode.sysbackup.tasks.Task; import net.horizoncode.sysbackup.tasks.Task;
import java.io.File; import java.io.File;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Getter @Getter
public class VacuumTask extends Task { public class VacuumTask extends Task {
private final File backupDir; private final File backupDir;
private final TimeUnit unit; private final ChronoUnit unit;
private final int value; private final int value;
public VacuumTask(File backupDir, TimeUnit unit, int value) { public VacuumTask(File backupDir, ChronoUnit unit, int value) {
this.backupDir = backupDir; this.backupDir = backupDir;
this.unit = unit; this.unit = unit;
this.value = value; this.value = value;
@ -25,7 +26,10 @@ public class VacuumTask extends Task {
public void start() { public void start() {
if (backupDir.listFiles() != null) { if (backupDir.listFiles() != null) {
Arrays.stream(Objects.requireNonNull(backupDir.listFiles())) Arrays.stream(Objects.requireNonNull(backupDir.listFiles()))
.filter(file -> file.lastModified() + unit.toMillis(value) <= System.currentTimeMillis()) .filter(
file ->
file.lastModified() + Duration.of(value, unit).toMillis()
<= System.currentTimeMillis())
.forEachOrdered(File::deleteOnExit); .forEachOrdered(File::deleteOnExit);
} }

View File

@ -5,7 +5,7 @@ outputFile = "{date} - {taskName}"
[vacuum] [vacuum]
enabled = true enabled = true
time = 5 time = 5
unit = "DAYS" # See java.util.concurrent.TimeUnit unit = "DAYS" # See java.time.temporal.ChronoUnit
[mysql] [mysql]
enabled = true enabled = true