Skip to content

Commit c17b2c4

Browse files
authored
Integrated code lifecycle: Improve clean up of temp folders in build agents (#9542)
1 parent 873fd8f commit c17b2c4

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

src/main/java/de/tum/cit/aet/artemis/buildagent/service/BuildJobExecutionService.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
import java.io.IOException;
1010
import java.net.URISyntaxException;
1111
import java.nio.charset.StandardCharsets;
12+
import java.nio.file.DirectoryStream;
13+
import java.nio.file.Files;
1214
import java.nio.file.Path;
1315
import java.nio.file.Paths;
16+
import java.time.Duration;
1417
import java.time.ZonedDateTime;
1518
import java.util.ArrayList;
1619
import java.util.List;
@@ -27,7 +30,10 @@
2730
import org.slf4j.Logger;
2831
import org.slf4j.LoggerFactory;
2932
import org.springframework.beans.factory.annotation.Value;
33+
import org.springframework.boot.context.event.ApplicationReadyEvent;
3034
import org.springframework.context.annotation.Profile;
35+
import org.springframework.context.event.EventListener;
36+
import org.springframework.scheduling.annotation.Async;
3137
import org.springframework.stereotype.Service;
3238

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

80+
private static final Duration TEMP_DIR_RETENTION_PERIOD = Duration.ofMinutes(5);
81+
7482
public BuildJobExecutionService(BuildJobContainerService buildJobContainerService, BuildJobGitService buildJobGitService, BuildAgentDockerService buildAgentDockerService,
7583
BuildLogsMap buildLogsMap) {
7684
this.buildJobContainerService = buildJobContainerService;
@@ -79,6 +87,38 @@ public BuildJobExecutionService(BuildJobContainerService buildJobContainerServic
7987
this.buildLogsMap = buildLogsMap;
8088
}
8189

90+
/**
91+
* This method is responsible for cleaning up temporary directories that were used for checking out repositories.
92+
* It is triggered when the application is ready and runs asynchronously.
93+
*/
94+
@EventListener(ApplicationReadyEvent.class)
95+
@Async
96+
public void initAsync() {
97+
final ZonedDateTime currentTime = ZonedDateTime.now();
98+
cleanUpTempDirectoriesAsync(currentTime);
99+
}
100+
101+
private void cleanUpTempDirectoriesAsync(ZonedDateTime currentTime) {
102+
log.info("Cleaning up temporary directories in {}", CHECKED_OUT_REPOS_TEMP_DIR);
103+
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Path.of(CHECKED_OUT_REPOS_TEMP_DIR))) {
104+
for (Path path : directoryStream) {
105+
try {
106+
ZonedDateTime lastModifiedTime = ZonedDateTime.ofInstant(Files.getLastModifiedTime(path).toInstant(), currentTime.getZone());
107+
if (Files.isDirectory(path) && lastModifiedTime.isBefore(currentTime.minus(TEMP_DIR_RETENTION_PERIOD))) {
108+
FileUtils.deleteDirectory(path.toFile());
109+
}
110+
}
111+
catch (IOException e) {
112+
log.error("Could not delete temporary directory {}", path, e);
113+
}
114+
}
115+
}
116+
catch (IOException e) {
117+
log.error("Could not delete temporary directories", e);
118+
}
119+
log.info("Clean up of temporary directories in {} completed.", CHECKED_OUT_REPOS_TEMP_DIR);
120+
}
121+
82122
/**
83123
* Orchestrates the execution of a build job in a Docker container. This method handles the preparation and configuration of the container,
84124
* including cloning the necessary repositories, checking out the appropriate branches, and preparing the environment for the build.
@@ -512,15 +552,16 @@ private void deleteCloneRepo(VcsRepositoryUri repositoryUri, @Nullable String co
512552
}
513553
buildJobGitService.deleteLocalRepository(repository);
514554
}
555+
// Do not throw an exception if deletion fails. If an exception occurs, clean up will happen in the next server start.
515556
catch (EntityNotFoundException e) {
516557
msg = "Error while checking out repository";
517558
buildLogsMap.appendBuildLogEntry(buildJobId, msg);
518-
throw new LocalCIException(msg, e);
559+
log.error("Error while deleting repository with URI {} and Path {}", repositoryUri, repositoryPath, e);
519560
}
520561
catch (IOException e) {
521562
msg = "Error while deleting repository";
522563
buildLogsMap.appendBuildLogEntry(buildJobId, msg);
523-
throw new LocalCIException(msg, e);
564+
log.error("Error while deleting repository with URI {} and Path {}", repositoryUri, repositoryPath, e);
524565
}
525566
}
526567

0 commit comments

Comments
 (0)