From 141696efeb3598b2a8c62aada6e6b45734e56f11 Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期一, 22 十二月 2025 16:59:45 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/task/DeviceLogScheduler.java |   97 ++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 89 insertions(+), 8 deletions(-)

diff --git a/src/main/java/com/zy/core/task/DeviceLogScheduler.java b/src/main/java/com/zy/core/task/DeviceLogScheduler.java
index a18a6bf..8f5ff54 100644
--- a/src/main/java/com/zy/core/task/DeviceLogScheduler.java
+++ b/src/main/java/com/zy/core/task/DeviceLogScheduler.java
@@ -14,10 +14,13 @@
 import org.springframework.stereotype.Component;
 
 import java.nio.charset.StandardCharsets;
+import java.nio.file.FileVisitResult;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.nio.file.StandardOpenOption;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
 import java.text.SimpleDateFormat;
 import java.util.Comparator;
 import java.util.Date;
@@ -26,6 +29,8 @@
 import java.util.List;
 import java.util.ArrayList;
 import java.util.Map;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.stream.Stream;
 import java.util.stream.Collectors;
 
 @Slf4j
@@ -36,10 +41,32 @@
     private String storageType;
     @Value("${deviceLogStorage.loggingPath}")
     private String loggingPath;
+    @Value("${deviceLogStorage.expireDays}")
+    private Integer expireDays;
     @Autowired
     private DeviceDataLogService deviceDataLogService;
     @Autowired
     private RedisUtil redisUtil;
+
+    private static final ReentrantLock FILE_OP_LOCK = new ReentrantLock();
+
+    @Scheduled(cron = "0/3 * * * * ? ")
+    public void delDeviceLog() {
+        if ("mysql".equals(storageType)) {
+            deviceDataLogService.clearLog(expireDays == null ? 1 : expireDays);
+        }else if ("file".equals(storageType)) {
+            if (!FILE_OP_LOCK.tryLock()) {
+                return;
+            }
+            try {
+                clearFileLog(expireDays == null ? 1 : expireDays);
+            } finally {
+                FILE_OP_LOCK.unlock();
+            }
+        }else {
+            log.error("鏈畾涔夌殑瀛樺偍绫诲瀷锛歿}", storageType);
+        }
+    }
 
     @Scheduled(cron = "0/3 * * * * ? ")
     public void execute() {
@@ -59,7 +86,14 @@
             if ("mysql".equals(storageType)) {
                 mysqlSave(keys, list);
             }else if ("file".equals(storageType)) {
-                fileSave(keys, list);
+                if (!FILE_OP_LOCK.tryLock()) {
+                    return;
+                }
+                try {
+                    fileSave(keys, list);
+                } finally {
+                    FILE_OP_LOCK.unlock();
+                }
             }else {
                 log.error("鏈畾涔夌殑瀛樺偍绫诲瀷锛歿}", storageType);
             }
@@ -106,7 +140,9 @@
                         if (size + line.length > max) {
                             index++;
                             current = dayDir.resolve(prefix + index + ".log");
-                            Files.createFile(current);
+                            if (!Files.exists(current)) {
+                                Files.createFile(current);
+                            }
                             size = 0;
                         }
                         Files.write(current, line, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
@@ -121,12 +157,15 @@
     }
 
     private int findStartIndex(Path baseDir, String prefix) throws Exception {
-        List<Path> matched = Files.list(baseDir)
-                .filter(p -> {
-                    String n = p.getFileName().toString();
-                    return n.startsWith(prefix) && n.endsWith(".log");
-                })
-                .collect(Collectors.toList());
+        List<Path> matched;
+        try (Stream<Path> stream = Files.list(baseDir)) {
+            matched = stream
+                    .filter(p -> {
+                        String n = p.getFileName().toString();
+                        return n.startsWith(prefix) && n.endsWith(".log");
+                    })
+                    .collect(Collectors.toList());
+        }
         int maxIdx = 0;
         for (Path p : matched) {
             String name = p.getFileName().toString();
@@ -151,4 +190,46 @@
         return candidate;
     }
 
+    private void clearFileLog(int days) {
+        try {
+            Path baseDir = Paths.get(loggingPath);
+            if (!Files.exists(baseDir)) {
+                return;
+            }
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+            long cutoff = System.currentTimeMillis() - (long) days * 24 * 60 * 60 * 1000;
+            List<Path> dirs;
+            try (Stream<Path> stream = Files.list(baseDir)) {
+                dirs = stream.filter(Files::isDirectory).collect(Collectors.toList());
+            }
+            for (Path dir : dirs) {
+                String name = dir.getFileName().toString();
+                if (name.length() == 8 && name.chars().allMatch(Character::isDigit)) {
+                    Date d = sdf.parse(name);
+                    if (d.getTime() < cutoff) {
+                        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
+                            @Override
+                            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
+                                try {
+                                    Files.deleteIfExists(file);
+                                } catch (Exception ignored) {}
+                                return FileVisitResult.CONTINUE;
+                            }
+
+                            @Override
+                            public FileVisitResult postVisitDirectory(Path dir, java.io.IOException exc) {
+                                try {
+                                    Files.deleteIfExists(dir);
+                                } catch (Exception ignored) {}
+                                return FileVisitResult.CONTINUE;
+                            }
+                        });
+                    }
+                }
+            }
+        } catch (Exception e) {
+            log.error("璁惧鏃ュ織鏂囦欢娓呯悊澶辫触", e);
+        }
+    }
+
 }

--
Gitblit v1.9.1