9
9
import java .io .IOException ;
10
10
import java .net .URISyntaxException ;
11
11
import java .nio .charset .StandardCharsets ;
12
+ import java .nio .file .DirectoryStream ;
13
+ import java .nio .file .Files ;
12
14
import java .nio .file .Path ;
13
15
import java .nio .file .Paths ;
16
+ import java .time .Duration ;
14
17
import java .time .ZonedDateTime ;
15
18
import java .util .ArrayList ;
16
19
import java .util .List ;
27
30
import org .slf4j .Logger ;
28
31
import org .slf4j .LoggerFactory ;
29
32
import org .springframework .beans .factory .annotation .Value ;
33
+ import org .springframework .boot .context .event .ApplicationReadyEvent ;
30
34
import org .springframework .context .annotation .Profile ;
35
+ import org .springframework .context .event .EventListener ;
36
+ import org .springframework .scheduling .annotation .Async ;
31
37
import org .springframework .stereotype .Service ;
32
38
33
39
import com .github .dockerjava .api .command .CreateContainerResponse ;
@@ -71,6 +77,8 @@ public class BuildJobExecutionService {
71
77
@ Value ("${artemis.version-control.default-branch:main}" )
72
78
private String defaultBranch ;
73
79
80
+ private static final Duration TEMP_DIR_RETENTION_PERIOD = Duration .ofMinutes (5 );
81
+
74
82
public BuildJobExecutionService (BuildJobContainerService buildJobContainerService , BuildJobGitService buildJobGitService , BuildAgentDockerService buildAgentDockerService ,
75
83
BuildLogsMap buildLogsMap ) {
76
84
this .buildJobContainerService = buildJobContainerService ;
@@ -79,6 +87,38 @@ public BuildJobExecutionService(BuildJobContainerService buildJobContainerServic
79
87
this .buildLogsMap = buildLogsMap ;
80
88
}
81
89
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
+
82
122
/**
83
123
* Orchestrates the execution of a build job in a Docker container. This method handles the preparation and configuration of the container,
84
124
* 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
512
552
}
513
553
buildJobGitService .deleteLocalRepository (repository );
514
554
}
555
+ // Do not throw an exception if deletion fails. If an exception occurs, clean up will happen in the next server start.
515
556
catch (EntityNotFoundException e ) {
516
557
msg = "Error while checking out repository" ;
517
558
buildLogsMap .appendBuildLogEntry (buildJobId , msg );
518
- throw new LocalCIException ( msg , e );
559
+ log . error ( "Error while deleting repository with URI {} and Path {}" , repositoryUri , repositoryPath , e );
519
560
}
520
561
catch (IOException e ) {
521
562
msg = "Error while deleting repository" ;
522
563
buildLogsMap .appendBuildLogEntry (buildJobId , msg );
523
- throw new LocalCIException ( msg , e );
564
+ log . error ( "Error while deleting repository with URI {} and Path {}" , repositoryUri , repositoryPath , e );
524
565
}
525
566
}
526
567
0 commit comments