This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch mvnd-1.x
in repository https://gitbox.apache.org/repos/asf/maven-mvnd.git

commit 40ce62d664cc7047ec7f8a2cf39980bebaac5a37
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Sun Nov 15 11:20:30 2026 +0100

    Keep the logs of running daemons when purging
    
    Fixes #1318
    
    (cherry picked from commit 3ab3a7515b72db6875feeaed40bc934380111379)
---
 .../org/mvndaemon/mvnd/client/DefaultClient.java   |  92 ++------------
 .../java/org/mvndaemon/mvnd/client/LogPurger.java  | 139 +++++++++++++++++++++
 .../org/mvndaemon/mvnd/client/LogPurgerTest.java   |  49 ++++++++
 3 files changed, 198 insertions(+), 82 deletions(-)

diff --git 
a/client/src/main/java-mvnd/org/mvndaemon/mvnd/client/DefaultClient.java 
b/client/src/main/java-mvnd/org/mvndaemon/mvnd/client/DefaultClient.java
index 8e820b13..b2d4216a 100644
--- a/client/src/main/java-mvnd/org/mvndaemon/mvnd/client/DefaultClient.java
+++ b/client/src/main/java-mvnd/org/mvndaemon/mvnd/client/DefaultClient.java
@@ -18,18 +18,12 @@
  */
 package org.mvndaemon.mvnd.client;
 
-import java.io.IOException;
-import java.io.PrintWriter;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.nio.file.StandardOpenOption;
-import java.nio.file.attribute.FileTime;
-import java.time.Duration;
 import java.time.Instant;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
-import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -38,6 +32,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
@@ -56,7 +51,6 @@ import org.mvndaemon.mvnd.common.Message;
 import org.mvndaemon.mvnd.common.Message.BuildException;
 import org.mvndaemon.mvnd.common.Message.BuildFinished;
 import org.mvndaemon.mvnd.common.OsUtils;
-import org.mvndaemon.mvnd.common.TimeUtils;
 import org.mvndaemon.mvnd.common.logging.ClientOutput;
 import org.mvndaemon.mvnd.common.logging.TerminalOutput;
 import org.slf4j.Logger;
@@ -64,8 +58,6 @@ import org.slf4j.LoggerFactory;
 import org.slf4j.bridge.SLF4JBridgeHandler;
 import org.slf4j.impl.MvndLoggerFactory;
 
-import static org.mvndaemon.mvnd.client.DaemonParameters.LOG_EXTENSION;
-
 public class DefaultClient implements Client {
 
     private static final Logger LOGGER = 
LoggerFactory.getLogger(DefaultClient.class);
@@ -338,7 +330,7 @@ public class DefaultClient implements Client {
                 return DefaultResult.success(argv);
             }
             if (Environment.PURGE.removeCommandLineOption(args) != null) {
-                String result = purgeLogs();
+                String result = purgeLogs(activeDaemons(registry));
                 output.accept(Message.out(result != null ? result : "Nothing 
to purge"));
                 return DefaultResult.success(argv);
             }
@@ -384,9 +376,10 @@ public class DefaultClient implements Client {
 
                 // We've sent the request, so it gives us a bit of time to 
purge the logs
                 AtomicReference<String> purgeMessage = new AtomicReference<>();
+                Set<String> activeDaemons = activeDaemons(registry);
                 Thread purgeLog = new Thread(
                         () -> {
-                            purgeMessage.set(purgeLogs());
+                            purgeMessage.set(purgeLogs(activeDaemons));
                         },
                         "Log purge");
                 purgeLog.setDaemon(true);
@@ -423,77 +416,12 @@ public class DefaultClient implements Client {
         }
     }
 
-    private String purgeLogs() {
-        Path storage = parameters.daemonStorage();
-        Duration purgeLogPeriod = parameters.purgeLogPeriod();
-        if (!Files.isDirectory(storage) || 
!TimeUtils.isPositive(purgeLogPeriod)) {
-            return null;
-        }
-        String date = DateTimeFormatter.ofPattern("yyyy-MM-dd")
-                .withZone(ZoneId.systemDefault())
-                .format(Instant.now());
-        Path log = storage.resolve("purge-" + date + ".log");
-        List<Path> deleted = new ArrayList<>();
-        List<Throwable> exceptions = new ArrayList<>();
-        FileTime limit = FileTime.from(Instant.now().minus(purgeLogPeriod));
-        try (Stream<Path> storagePath = Files.list(storage)) {
-            storagePath
-                    .filter(p -> 
p.getFileName().toString().endsWith(LOG_EXTENSION))
-                    .filter(p -> !log.equals(p))
-                    .filter(p -> {
-                        try {
-                            FileTime lmt = Files.getLastModifiedTime(p);
-                            return lmt.compareTo(limit) < 0;
-                        } catch (IOException e) {
-                            exceptions.add(e);
-                            return false;
-                        }
-                    })
-                    .forEach(p -> {
-                        try {
-                            Files.delete(p);
-                            deleted.add(p);
-                        } catch (IOException e) {
-                            exceptions.add(e);
-                        }
-                    });
-        } catch (Exception e) {
-            exceptions.add(e);
-        }
-        if (exceptions.isEmpty() && deleted.isEmpty()) {
-            return null;
-        }
-        String logMessage;
-        try (PrintWriter w = new PrintWriter(Files.newBufferedWriter(
-                log, StandardOpenOption.WRITE, StandardOpenOption.APPEND, 
StandardOpenOption.CREATE))) {
-            w.printf("Purge executed at %s%n", Instant.now().toString());
-            if (deleted.isEmpty()) {
-                w.printf("No files deleted.%n");
-            } else {
-                w.printf("Deleted files:%n");
-                for (Path p : deleted) {
-                    w.printf("    %s%n", p.toString());
-                }
-            }
-            if (!exceptions.isEmpty()) {
-                w.printf("%d exception(s) occurred during the purge", 
exceptions.size());
-                for (Throwable t : exceptions) {
-                    t.printStackTrace(w);
-                }
-            }
-            char[] buf = new char[80];
-            Arrays.fill(buf, '=');
-            w.printf("%s%n", new String(buf));
-            logMessage = "log available in " + log.toString();
-        } catch (IOException e) {
-            logMessage = "an exception occurred when writing log to " + 
log.toString() + ": " + e.toString();
-        }
-        if (exceptions.isEmpty()) {
-            return String.format("Purged %d log files (%s)", deleted.size(), 
logMessage);
-        } else {
-            return String.format(
-                    "Purged %d log files with %d exceptions (%s)", 
deleted.size(), exceptions.size(), logMessage);
-        }
+    private String purgeLogs(Set<String> activeDaemons) {
+        return LogPurger.purge(parameters.daemonStorage(), 
parameters.purgeLogPeriod(), activeDaemons);
+    }
+
+    private static Set<String> activeDaemons(DaemonRegistry registry) {
+        return 
registry.getAll().stream().map(DaemonInfo::getId).collect(Collectors.toSet());
     }
 
     private static class DefaultResult implements ExecutionResult {
diff --git a/client/src/main/java/org/mvndaemon/mvnd/client/LogPurger.java 
b/client/src/main/java/org/mvndaemon/mvnd/client/LogPurger.java
new file mode 100644
index 00000000..b472643e
--- /dev/null
+++ b/client/src/main/java/org/mvndaemon/mvnd/client/LogPurger.java
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.mvndaemon.mvnd.client;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+import java.nio.file.attribute.FileTime;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Stream;
+
+import org.mvndaemon.mvnd.common.TimeUtils;
+
+import static org.mvndaemon.mvnd.client.DaemonParameters.LOG_EXTENSION;
+
+/**
+ * Deletes old daemon logs. Kept apart from {@code DefaultClient}, which lives 
in a separate source root that the
+ * unit tests cannot see.
+ */
+final class LogPurger {
+
+    private LogPurger() {}
+
+    /**
+     * Deletes the old logs in {@code storage}, except those of the daemons in 
{@code activeDaemons}, whose log
+     * files are still open.
+     *
+     * @return a message describing what was purged, or {@code null} if there 
was nothing to report
+     */
+    static String purge(Path storage, Duration purgeLogPeriod, Set<String> 
activeDaemons) {
+        if (!Files.isDirectory(storage) || 
!TimeUtils.isPositive(purgeLogPeriod)) {
+            return null;
+        }
+        String date = DateTimeFormatter.ofPattern("yyyy-MM-dd")
+                .withZone(ZoneId.systemDefault())
+                .format(Instant.now());
+        Path log = storage.resolve("purge-" + date + ".log");
+        List<Path> deleted = new ArrayList<>();
+        List<Throwable> exceptions = new ArrayList<>();
+        FileTime limit = FileTime.from(Instant.now().minus(purgeLogPeriod));
+        try (Stream<Path> storagePath = Files.list(storage)) {
+            storagePath
+                    .filter(p -> 
p.getFileName().toString().endsWith(LOG_EXTENSION))
+                    .filter(p -> !log.equals(p))
+                    .filter(p -> !isActiveDaemonLog(p, activeDaemons))
+                    .filter(p -> {
+                        try {
+                            FileTime lmt = Files.getLastModifiedTime(p);
+                            return lmt.compareTo(limit) < 0;
+                        } catch (IOException e) {
+                            exceptions.add(e);
+                            return false;
+                        }
+                    })
+                    .forEach(p -> {
+                        try {
+                            Files.delete(p);
+                            deleted.add(p);
+                        } catch (IOException e) {
+                            exceptions.add(e);
+                        }
+                    });
+        } catch (Exception e) {
+            exceptions.add(e);
+        }
+        if (exceptions.isEmpty() && deleted.isEmpty()) {
+            return null;
+        }
+        String logMessage;
+        try (PrintWriter w = new PrintWriter(Files.newBufferedWriter(
+                log, StandardOpenOption.WRITE, StandardOpenOption.APPEND, 
StandardOpenOption.CREATE))) {
+            w.printf("Purge executed at %s%n", Instant.now().toString());
+            if (deleted.isEmpty()) {
+                w.printf("No files deleted.%n");
+            } else {
+                w.printf("Deleted files:%n");
+                for (Path p : deleted) {
+                    w.printf("    %s%n", p.toString());
+                }
+            }
+            if (!exceptions.isEmpty()) {
+                w.printf("%d exception(s) occurred during the purge", 
exceptions.size());
+                for (Throwable t : exceptions) {
+                    t.printStackTrace(w);
+                }
+            }
+            char[] buf = new char[80];
+            Arrays.fill(buf, '=');
+            w.printf("%s%n", new String(buf));
+            logMessage = "log available in " + log.toString();
+        } catch (IOException e) {
+            logMessage = "an exception occurred when writing log to " + 
log.toString() + ": " + e.toString();
+        }
+        if (exceptions.isEmpty()) {
+            return String.format("Purged %d log files (%s)", deleted.size(), 
logMessage);
+        } else {
+            return String.format(
+                    "Purged %d log files with %d exceptions (%s)", 
deleted.size(), exceptions.size(), logMessage);
+        }
+    }
+
+    private static boolean isActiveDaemonLog(Path p, Set<String> 
activeDaemons) {
+        String name = p.getFileName().toString();
+        if (!name.startsWith("daemon-")) {
+            return false;
+        }
+        // daemon-<id>.log and daemon-<id>.out.log
+        String id = name.substring("daemon-".length(), name.length() - 
LOG_EXTENSION.length());
+        if (id.endsWith(".out")) {
+            id = id.substring(0, id.length() - ".out".length());
+        }
+        return activeDaemons.contains(id);
+    }
+}
diff --git a/client/src/test/java/org/mvndaemon/mvnd/client/LogPurgerTest.java 
b/client/src/test/java/org/mvndaemon/mvnd/client/LogPurgerTest.java
new file mode 100644
index 00000000..7e2d6d98
--- /dev/null
+++ b/client/src/test/java/org/mvndaemon/mvnd/client/LogPurgerTest.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.mvndaemon.mvnd.client;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.FileTime;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Arrays;
+import java.util.Collections;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class LogPurgerTest {
+
+    @Test
+    void purgeKeepsLogsOfActiveDaemons(@TempDir Path dir) throws Exception {
+        FileTime old = FileTime.from(Instant.now().minusSeconds(30L * 24 * 
3600));
+        for (String name : Arrays.asList("daemon-abc.log", 
"daemon-abc.out.log", "daemon-gone.log")) {
+            Path p = Files.createFile(dir.resolve(name));
+            Files.setLastModifiedTime(p, old);
+        }
+        LogPurger.purge(dir, Duration.ofDays(7), Collections.singleton("abc"));
+        assertTrue(Files.exists(dir.resolve("daemon-abc.log")));
+        assertTrue(Files.exists(dir.resolve("daemon-abc.out.log")));
+        assertFalse(Files.exists(dir.resolve("daemon-gone.log")));
+    }
+}

Reply via email to