diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/cron/CleanUpOldLogs.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/cron/CleanUpOldLogs.java new file mode 100644 index 0000000000..c1edafc48c --- /dev/null +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/cron/CleanUpOldLogs.java @@ -0,0 +1,45 @@ +package com.appsmith.server.cron; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.stream.Stream; + +@Slf4j +@Component +public class CleanUpOldLogs { + + private static final int CUTOFF_AGE = 7 * 24 * 60 * 60 * 1000; // 7 days + + @Scheduled(cron = "0 0 0 * * SAT") + public void cleanUpOldLogs() throws IOException { + log.info("Cleaning up old logs"); + + final long cutoffTime = System.currentTimeMillis() - CUTOFF_AGE; + + try (Stream walk = Files.walk(Paths.get("/appsmith-stacks/logs"))) { + walk.filter(path -> { + try { + return Files.isRegularFile(path) + && path.getFileName().toString().contains(".log") + && Files.getLastModifiedTime(path).toMillis() < cutoffTime; + } catch (IOException e) { + log.error("Failed to get last modified time for file {}", path, e); + } + return false; + }) + .forEach(path -> { + try { + Files.delete(path); + } catch (IOException e) { + log.error("Failed to delete old log file {}", path, e); + } + }); + } + } +} diff --git a/deploy/docker/fs/etc/cron.d/cleanup-logs b/deploy/docker/fs/etc/cron.d/cleanup-logs deleted file mode 100644 index f90c2b79e2..0000000000 --- a/deploy/docker/fs/etc/cron.d/cleanup-logs +++ /dev/null @@ -1 +0,0 @@ -0 0 * * SUN root find /appsmith-stacks/logs/backend /appsmith-stacks/logs/rts/ /appsmith-stacks/logs/editor/ -name "*.log*" -type f -mtime +7 -delete