Skip to content

Commit

Permalink
adjust deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
BBesrour committed Sep 10, 2024
1 parent ee5ca31 commit 15c8450
Showing 1 changed file with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -401,23 +401,62 @@ public void deleteOldBuildLogsFiles() {
if (!profileService.isSchedulingActive()) {
return;
}

log.info("Deleting old build log files");
ZonedDateTime now = ZonedDateTime.now();

try (DirectoryStream<Path> stream = Files.newDirectoryStream(buildLogsPath)) {
for (Path file : stream) {
ZonedDateTime lastModified = ZonedDateTime.ofInstant(Files.getLastModifiedTime(file).toInstant(), now.getZone());
if (lastModified.isBefore(now.minusDays(expiryDays))) {
Files.deleteIfExists(file);
log.info("Deleted old build log file {}", file);
}
}
try {
deleteRecursively(buildLogsPath);
}
catch (IOException e) {
log.error("Error occurred while trying to delete old build log files", e);
}
}

private void deleteRecursively(Path path) throws IOException {
if (!Files.isDirectory(path)) {
deleteFileIfExpired(path);
return;
}

try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
for (Path subPath : stream) {
deleteRecursively(subPath);
}
}
catch (IOException e) {
log.error("Error occurred while processing directory: {}", path, e);
}

if (!path.equals(buildLogsPath)) {
deleteDirectoryIfEmpty(path);
}
}

private void deleteFileIfExpired(Path file) throws IOException {
ZonedDateTime now = ZonedDateTime.now();

ZonedDateTime lastModified = ZonedDateTime.ofInstant(Files.getLastModifiedTime(file).toInstant(), now.getZone());
if (Files.isRegularFile(file) && lastModified.isBefore(now.minusDays(expiryDays))) {
Files.deleteIfExists(file);
log.info("Deleted old build log file {}", file);
}

}

private void deleteDirectoryIfEmpty(Path directory) {
if (Files.isDirectory(directory)) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory)) {
if (!stream.iterator().hasNext()) {
Files.deleteIfExists(directory);
log.info("Deleted empty directory {}", directory);
}
}
catch (IOException e) {
log.error("Error occurred while trying to delete empty directory {}", directory, e);
}
}
}

/**
* Checks if the log file for a specific build job exists in the file system.
*
Expand Down

0 comments on commit 15c8450

Please sign in to comment.