Skip to content

Commit

Permalink
Integrated code lifecycle: Improve clean up of temp folders in build …
Browse files Browse the repository at this point in the history
…agents (#9542)
  • Loading branch information
BBesrour authored Oct 22, 2024
1 parent 873fd8f commit c17b2c4
Showing 1 changed file with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -27,7 +30,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.github.dockerjava.api.command.CreateContainerResponse;
Expand Down Expand Up @@ -71,6 +77,8 @@ public class BuildJobExecutionService {
@Value("${artemis.version-control.default-branch:main}")
private String defaultBranch;

private static final Duration TEMP_DIR_RETENTION_PERIOD = Duration.ofMinutes(5);

public BuildJobExecutionService(BuildJobContainerService buildJobContainerService, BuildJobGitService buildJobGitService, BuildAgentDockerService buildAgentDockerService,
BuildLogsMap buildLogsMap) {
this.buildJobContainerService = buildJobContainerService;
Expand All @@ -79,6 +87,38 @@ public BuildJobExecutionService(BuildJobContainerService buildJobContainerServic
this.buildLogsMap = buildLogsMap;
}

/**
* This method is responsible for cleaning up temporary directories that were used for checking out repositories.
* It is triggered when the application is ready and runs asynchronously.
*/
@EventListener(ApplicationReadyEvent.class)
@Async
public void initAsync() {
final ZonedDateTime currentTime = ZonedDateTime.now();
cleanUpTempDirectoriesAsync(currentTime);
}

private void cleanUpTempDirectoriesAsync(ZonedDateTime currentTime) {
log.info("Cleaning up temporary directories in {}", CHECKED_OUT_REPOS_TEMP_DIR);
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Path.of(CHECKED_OUT_REPOS_TEMP_DIR))) {
for (Path path : directoryStream) {
try {
ZonedDateTime lastModifiedTime = ZonedDateTime.ofInstant(Files.getLastModifiedTime(path).toInstant(), currentTime.getZone());
if (Files.isDirectory(path) && lastModifiedTime.isBefore(currentTime.minus(TEMP_DIR_RETENTION_PERIOD))) {
FileUtils.deleteDirectory(path.toFile());
}
}
catch (IOException e) {
log.error("Could not delete temporary directory {}", path, e);
}
}
}
catch (IOException e) {
log.error("Could not delete temporary directories", e);
}
log.info("Clean up of temporary directories in {} completed.", CHECKED_OUT_REPOS_TEMP_DIR);
}

/**
* Orchestrates the execution of a build job in a Docker container. This method handles the preparation and configuration of the container,
* including cloning the necessary repositories, checking out the appropriate branches, and preparing the environment for the build.
Expand Down Expand Up @@ -512,15 +552,16 @@ private void deleteCloneRepo(VcsRepositoryUri repositoryUri, @Nullable String co
}
buildJobGitService.deleteLocalRepository(repository);
}
// Do not throw an exception if deletion fails. If an exception occurs, clean up will happen in the next server start.
catch (EntityNotFoundException e) {
msg = "Error while checking out repository";
buildLogsMap.appendBuildLogEntry(buildJobId, msg);
throw new LocalCIException(msg, e);
log.error("Error while deleting repository with URI {} and Path {}", repositoryUri, repositoryPath, e);
}
catch (IOException e) {
msg = "Error while deleting repository";
buildLogsMap.appendBuildLogEntry(buildJobId, msg);
throw new LocalCIException(msg, e);
log.error("Error while deleting repository with URI {} and Path {}", repositoryUri, repositoryPath, e);
}
}

Expand Down

0 comments on commit c17b2c4

Please sign in to comment.