From d9f2cb87d84a9b413b7f15b2456c81c7e1c213f3 Mon Sep 17 00:00:00 2001 From: Stephan Krusche Date: Mon, 9 Sep 2024 17:07:20 +0200 Subject: [PATCH] Development: Improve cleanup schedule for build logs --- .../artemis/service/BuildLogEntryService.java | 29 +++++++++++++++++-- .../resources/config/application-artemis.yml | 2 +- .../service/BuildLogEntryServiceTest.java | 4 --- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/tum/in/www1/artemis/service/BuildLogEntryService.java b/src/main/java/de/tum/in/www1/artemis/service/BuildLogEntryService.java index 9b2916219cd0..73198cbb7d52 100644 --- a/src/main/java/de/tum/in/www1/artemis/service/BuildLogEntryService.java +++ b/src/main/java/de/tum/in/www1/artemis/service/BuildLogEntryService.java @@ -40,15 +40,18 @@ public class BuildLogEntryService { private final ProgrammingSubmissionRepository programmingSubmissionRepository; + private final ProfileService profileService; + @Value("${artemis.continuous-integration.build-log.file-expiry-days:30}") private int expiryDays; @Value("${artemis.build-logs-path:./build-logs}") private Path buildLogsPath; - public BuildLogEntryService(BuildLogEntryRepository buildLogEntryRepository, ProgrammingSubmissionRepository programmingSubmissionRepository) { + public BuildLogEntryService(BuildLogEntryRepository buildLogEntryRepository, ProgrammingSubmissionRepository programmingSubmissionRepository, ProfileService profileService) { this.buildLogEntryRepository = buildLogEntryRepository; this.programmingSubmissionRepository = programmingSubmissionRepository; + this.profileService = profileService; } /** @@ -331,10 +334,30 @@ public FileSystemResource retrieveBuildLogsFromFileForBuildJob(String buildJobId } /** - * Deletes all build log files that are older than {@link #expiryDays} days on a schedule + * Scheduled task that deletes old build log files from the continuous integration system. + *

+ * This method runs based on the cron schedule defined in the application properties, with + * a default value of 3:00 AM every day if no custom schedule is provided. + * The task will only execute if scheduling is active, which is checked via the {@code profileService}. + *

+ *

+ * The method iterates through the files in the configured build logs directory and deletes + * files that were last modified before the configured expiry period (in days). The expiration + * period is specified by the {@code expiryDays} variable, and files older than this period are deleted. + *

+ *

+ * In case of an error during file deletion, it logs the error and continues processing. + *

+ * + * @throws IOException if an I/O error occurs while accessing the build log files directory or + * deleting files. */ - @Scheduled(cron = "${artemis.continuous-integration.build-log.cleanup-schedule:0 0 3 1 * ?}") + @Scheduled(cron = "${artemis.continuous-integration.build-log.cleanup-schedule:0 0 3 * * ?}") public void deleteOldBuildLogsFiles() { + // only execute this if scheduling is active + if (!profileService.isSchedulingActive()) { + return; + } log.info("Deleting old build log files"); ZonedDateTime now = ZonedDateTime.now(); diff --git a/src/main/resources/config/application-artemis.yml b/src/main/resources/config/application-artemis.yml index 9b2c2e3f40f7..f1501cbceba2 100644 --- a/src/main/resources/config/application-artemis.yml +++ b/src/main/resources/config/application-artemis.yml @@ -90,7 +90,7 @@ artemis: notification-plugin: "ls1tum/artemis-notification-plugin:1.0.0" # Docker image for the generic notification plugin. This value is set in an CI variable in GitLab CI. build-log: file-expiry-days: 30 # The amount of days until build log files can be deleted - cleanup-schedule: 0 0 3 1 * ? # Cron expression for schedule to delete old build log files + cleanup-schedule: 0 0 3 * * ? # Cron expression for schedule to delete old build log files git: name: Artemis email: artemis@xcit.tum.de diff --git a/src/test/java/de/tum/in/www1/artemis/service/BuildLogEntryServiceTest.java b/src/test/java/de/tum/in/www1/artemis/service/BuildLogEntryServiceTest.java index ba2befae81f0..398572dc7613 100644 --- a/src/test/java/de/tum/in/www1/artemis/service/BuildLogEntryServiceTest.java +++ b/src/test/java/de/tum/in/www1/artemis/service/BuildLogEntryServiceTest.java @@ -350,10 +350,6 @@ void filterOutEmptyLogs() { assertThat(result).isEmpty(); } - private List convertToBuildLogs(List content) { - return convertToBuildLogs(content.stream()); - } - private List convertToBuildLogs(String... content) { return convertToBuildLogs(Arrays.stream(content)); }