#
Junjie
6 天以前 32004d6bb7db528c151cca5bd78c4e557171a9ee
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);
        }
    }
}