From c0f6a7968e4c2908ed703aeca8406d24610ae6c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Alcarraz?= Date: Thu, 8 Feb 2024 11:19:37 -0500 Subject: [PATCH] Let the max depth for deletion be configurable. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andrés Alcarraz #580 About the rotation of q2 logs --- .../java/org/jpos/util/DailyLogListener.java | 9 +++- .../org/jpos/util/DailyLogListenerTest.java | 47 +++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/jpos/src/main/java/org/jpos/util/DailyLogListener.java b/jpos/src/main/java/org/jpos/util/DailyLogListener.java index 415f9cb315..3c8fd70fc1 100644 --- a/jpos/src/main/java/org/jpos/util/DailyLogListener.java +++ b/jpos/src/main/java/org/jpos/util/DailyLogListener.java @@ -20,6 +20,7 @@ import org.jpos.core.Configuration; import org.jpos.core.ConfigurationException; +import org.jpos.core.annotation.Config; import java.io.*; import java.nio.file.Files; @@ -56,6 +57,10 @@ public class DailyLogListener extends RotateLogListener{ private static final int DEF_BUFFER_SIZE = 128*1024;//128 KB private static final String[] DEF_COMPRESSED_SUFFIX= {"",".gz",".zip"}; private static final Map COMPRESSION_FORMATS = new HashMap(3); + + @Config("max-depth-deletion") + private int maxDepthDeletion = DEF_MAXDEPTH; + static { COMPRESSION_FORMATS.put("none", NONE); COMPRESSION_FORMATS.put("gzip", GZIP); @@ -93,7 +98,7 @@ public void setConfiguration(Configuration cfg) throws ConfigurationException { setCompressionBufferSize(cfg.getInt("compression-buffer-size", DEF_BUFFER_SIZE)); - deleteRegex = cfg.get("delete-regex", defaultDeleteRegex()); + deleteRegex = cfg.get("delete-regex", defaultDeleteRegex()); setLastDate(fmt.format(new Date())); @@ -178,7 +183,7 @@ public void deleteOldLogs() throws IOException { long currentSystemTime = System.currentTimeMillis(); try { - Files.find(logBasePath, DEF_MAXDEPTH, + Files.find(logBasePath, maxDepthDeletion, (path, attributes) -> path.getFileName().toString().matches(deleteRegex) && attributes.isRegularFile() diff --git a/jpos/src/test/java/org/jpos/util/DailyLogListenerTest.java b/jpos/src/test/java/org/jpos/util/DailyLogListenerTest.java index b4ac150d80..2b921fb689 100644 --- a/jpos/src/test/java/org/jpos/util/DailyLogListenerTest.java +++ b/jpos/src/test/java/org/jpos/util/DailyLogListenerTest.java @@ -20,17 +20,24 @@ import static org.apache.commons.lang3.JavaVersion.JAVA_14; import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; import static org.jpos.util.LogFileTestUtils.getStringFromCompressedFile; import static org.jpos.util.LogFileTestUtils.getStringFromFile; import static org.junit.jupiter.api.Assertions.*; +import static org.hamcrest.io.FileMatchers.anExistingFile; +import static org.hamcrest.MatcherAssert.*; import java.io.*; import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.attribute.FileTime; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.Properties; import java.util.zip.Deflater; @@ -40,6 +47,7 @@ import org.jpos.core.ConfigurationException; import org.jpos.core.SimpleConfiguration; import org.jpos.core.SubConfiguration; +import org.jpos.q2.QFactory; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -427,6 +435,45 @@ public void testMaxAgeFeatureWhenThereIsNonLogFiles() throws Exception { listener.destroy(); } + @Test + public void testDeleteOldLogsWithCustomMaxDepth() throws ConfigurationException, IOException, IllegalAccessException { + Path parent = logRotationTestDirectory.getDirectory().resolve("parent"); + Files.createDirectory(parent); + Path child = parent.resolve("child"); + Files.createFile(child); + assertThat("file should have been created", child.toFile(), is(anExistingFile())); + Files.setLastModifiedTime(child, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough + DailyLogListener listener = new DailyLogListener(); + SimpleConfiguration cfg = new SimpleConfiguration(); + cfg.put("prefix", child.toAbsolutePath().toString()); + cfg.put("max-depth-deletion", "2"); + cfg.put("delete-regex", "^child$"); + cfg.put("maxage", "1000"); + QFactory.autoconfigure(listener, cfg); + listener.setConfiguration(cfg); + listener.deleteOldLogs(); + assertThat("file should have been deleted", child.toFile(), is(not(anExistingFile()))); + + } + @Test + public void testDeleteOldLogsWithoutCustomMaxDepth() throws ConfigurationException, IOException, IllegalAccessException { + Path parent = logRotationTestDirectory.getDirectory().resolve("parent"); + Files.createDirectory(parent); + Path child = parent.resolve("child"); + Files.createFile(child); + assertThat("file should have been created", child.toFile(), is(anExistingFile())); + Files.setLastModifiedTime(child, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough + DailyLogListener listener = new DailyLogListener(); + SimpleConfiguration cfg = new SimpleConfiguration(); + cfg.put("prefix", child.toAbsolutePath().toString()); + cfg.put("delete-regex", "^child$"); + cfg.put("maxage", "1000"); + QFactory.autoconfigure(listener, cfg); + listener.setConfiguration(cfg); + listener.deleteOldLogs(); + assertThat("file should have been deleted", child.toFile(), is(not(anExistingFile()))); + + } @Test @Disabled("This feature doesn't work in Windows so we reverted the patch c94ff02f2") public void testLogRotateAbortsWhenCreatingNewFileFails() throws Exception {