From c6eb4c8fdb01dffa441c58c0ddef9e8ca90aea7c Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Mon, 4 Jul 2022 09:46:18 +0200 Subject: [PATCH] use ChronoUnit instead of TimeUnit --- .../net/horizoncode/sysbackup/tasks/TaskBuilder.java | 7 +++++-- .../horizoncode/sysbackup/tasks/impl/VacuumTask.java | 12 ++++++++---- src/main/resources/exampletask.toml | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/horizoncode/sysbackup/tasks/TaskBuilder.java b/src/main/java/net/horizoncode/sysbackup/tasks/TaskBuilder.java index ae4e965..0e4af32 100644 --- a/src/main/java/net/horizoncode/sysbackup/tasks/TaskBuilder.java +++ b/src/main/java/net/horizoncode/sysbackup/tasks/TaskBuilder.java @@ -11,6 +11,7 @@ import org.tomlj.TomlArray; import java.io.File; import java.text.SimpleDateFormat; +import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; @@ -63,8 +64,10 @@ public class TaskBuilder { File outputFile = new File(backupDir, fileName); if (doVAC) { - TimeUnit unit = - TimeUnit.valueOf(getTaskConfig().getStringOrDefault("vacuum.unit", TimeUnit.DAYS.name())); + ChronoUnit unit = + ChronoUnit.valueOf( + getTaskConfig().getStringOrDefault("vacuum.unit", ChronoUnit.DAYS.name())); + int value = getTaskConfig().getIntOrDefault("vacuum.time", 5); System.out.printf("Adding VacuumTask with lifetime of %d %s%n", value, unit.name()); diff --git a/src/main/java/net/horizoncode/sysbackup/tasks/impl/VacuumTask.java b/src/main/java/net/horizoncode/sysbackup/tasks/impl/VacuumTask.java index 5505a64..43224d8 100644 --- a/src/main/java/net/horizoncode/sysbackup/tasks/impl/VacuumTask.java +++ b/src/main/java/net/horizoncode/sysbackup/tasks/impl/VacuumTask.java @@ -4,18 +4,19 @@ import lombok.Getter; import net.horizoncode.sysbackup.tasks.Task; import java.io.File; +import java.time.Duration; +import java.time.temporal.ChronoUnit; import java.util.Arrays; import java.util.Objects; -import java.util.concurrent.TimeUnit; @Getter public class VacuumTask extends Task { private final File backupDir; - private final TimeUnit unit; + private final ChronoUnit unit; private final int value; - public VacuumTask(File backupDir, TimeUnit unit, int value) { + public VacuumTask(File backupDir, ChronoUnit unit, int value) { this.backupDir = backupDir; this.unit = unit; this.value = value; @@ -25,7 +26,10 @@ public class VacuumTask extends Task { public void start() { if (backupDir.listFiles() != null) { 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); } diff --git a/src/main/resources/exampletask.toml b/src/main/resources/exampletask.toml index 1a2365d..cbcdbff 100644 --- a/src/main/resources/exampletask.toml +++ b/src/main/resources/exampletask.toml @@ -5,7 +5,7 @@ outputFile = "{date} - {taskName}" [vacuum] enabled = true time = 5 -unit = "DAYS" # See java.util.concurrent.TimeUnit +unit = "DAYS" # See java.time.temporal.ChronoUnit [mysql] enabled = true