@@ -79,14 +79,45 @@ export class Caching {
7979 return ;
8080 }
8181
82- // Check disk space before creating tar archive
82+ // Check disk space before creating tar archive and clean up if needed
83+ let diskUsagePercent = 0 ;
8384 try {
84- const diskCheckOutput = await CloudRunnerSystem . Run ( `df -h . 2>/dev/null || df -h /data 2>/dev/null || true` ) ;
85+ const diskCheckOutput = await CloudRunnerSystem . Run ( `df . 2>/dev/null || df /data 2>/dev/null || true` ) ;
8586 CloudRunnerLogger . log ( `Disk space before tar: ${ diskCheckOutput } ` ) ;
87+ // Parse disk usage percentage (e.g., "72G 72G 196M 100%")
88+ const usageMatch = diskCheckOutput . match ( / ( \d + ) % / ) ;
89+ if ( usageMatch ) {
90+ diskUsagePercent = parseInt ( usageMatch [ 1 ] , 10 ) ;
91+ }
8692 } catch ( error ) {
8793 // Ignore disk check errors
8894 }
8995
96+ // If disk usage is high (>90%), proactively clean up old cache files
97+ if ( diskUsagePercent > 90 ) {
98+ CloudRunnerLogger . log (
99+ `Disk usage is ${ diskUsagePercent } % - cleaning up old cache files before tar operation` ,
100+ ) ;
101+ try {
102+ const cacheParent = path . dirname ( cacheFolder ) ;
103+ if ( await fileExists ( cacheParent ) ) {
104+ // Remove cache files older than 6 hours (more aggressive than 1 day)
105+ await CloudRunnerSystem . Run (
106+ `find ${ cacheParent } -name "*.tar*" -type f -mmin +360 -delete 2>/dev/null || true` ,
107+ ) ;
108+ // Also try to remove old cache directories
109+ await CloudRunnerSystem . Run (
110+ `find ${ cacheParent } -type d -empty -delete 2>/dev/null || true` ,
111+ ) ;
112+ CloudRunnerLogger . log ( `Cleanup completed. Checking disk space again...` ) ;
113+ const diskCheckAfter = await CloudRunnerSystem . Run ( `df . 2>/dev/null || df /data 2>/dev/null || true` ) ;
114+ CloudRunnerLogger . log ( `Disk space after cleanup: ${ diskCheckAfter } ` ) ;
115+ }
116+ } catch ( cleanupError ) {
117+ CloudRunnerLogger . log ( `Proactive cleanup failed: ${ cleanupError } ` ) ;
118+ }
119+ }
120+
90121 // Clean up any existing incomplete tar files
91122 try {
92123 await CloudRunnerSystem . Run ( `rm -f ${ cacheArtifactName } .tar${ compressionSuffix } 2>/dev/null || true` ) ;
@@ -102,24 +133,60 @@ export class Caching {
102133 // Check if error is due to disk space
103134 const errorMessage = error ?. message || error ?. toString ( ) || '' ;
104135 if ( errorMessage . includes ( 'No space left' ) || errorMessage . includes ( 'Wrote only' ) ) {
105- CloudRunnerLogger . log ( `Disk space error detected. Attempting cleanup...` ) ;
106- // Try to clean up old cache files
136+ CloudRunnerLogger . log ( `Disk space error detected. Attempting aggressive cleanup...` ) ;
137+ // Try to clean up old cache files more aggressively
107138 try {
108139 const cacheParent = path . dirname ( cacheFolder ) ;
109140 if ( await fileExists ( cacheParent ) ) {
110- // Find and remove old cache entries (keep only the most recent)
141+ // Remove cache files older than 1 hour (very aggressive)
142+ await CloudRunnerSystem . Run (
143+ `find ${ cacheParent } -name "*.tar*" -type f -mmin +60 -delete 2>/dev/null || true` ,
144+ ) ;
145+ // Remove empty cache directories
111146 await CloudRunnerSystem . Run (
112- `find ${ cacheParent } -name "*.tar*" -type f -mtime +1 -delete 2>/dev/null || true` ,
147+ `find ${ cacheParent } -type d -empty -delete 2>/dev/null || true` ,
148+ ) ;
149+ // Also try to clean up the entire cache folder if it's getting too large
150+ const cacheRoot = path . resolve ( cacheParent , '..' ) ;
151+ if ( await fileExists ( cacheRoot ) ) {
152+ // Remove cache entries older than 30 minutes
153+ await CloudRunnerSystem . Run (
154+ `find ${ cacheRoot } -name "*.tar*" -type f -mmin +30 -delete 2>/dev/null || true` ,
155+ ) ;
156+ }
157+ CloudRunnerLogger . log ( `Aggressive cleanup completed. Retrying tar operation...` ) ;
158+ // Retry the tar operation once after cleanup
159+ let retrySucceeded = false ;
160+ try {
161+ await CloudRunnerSystem . Run (
162+ `tar -cf ${ cacheArtifactName } .tar${ compressionSuffix } "${ path . basename ( sourceFolder ) } "` ,
163+ ) ;
164+ // If retry succeeds, mark it - we'll continue normally without throwing
165+ retrySucceeded = true ;
166+ } catch ( retryError : any ) {
167+ throw new Error (
168+ `Failed to create cache archive after cleanup. Original error: ${ errorMessage } . Retry error: ${ retryError ?. message || retryError } ` ,
169+ ) ;
170+ }
171+ // If retry succeeded, don't throw the original error - let execution continue after catch block
172+ if ( ! retrySucceeded ) {
173+ throw error ;
174+ }
175+ // If we get here, retry succeeded - execution will continue after the catch block
176+ } else {
177+ throw new Error (
178+ `Failed to create cache archive due to insufficient disk space. Error: ${ errorMessage } . Cleanup not possible - cache folder missing.` ,
113179 ) ;
114180 }
115- } catch ( cleanupError ) {
181+ } catch ( cleanupError : any ) {
116182 CloudRunnerLogger . log ( `Cleanup attempt failed: ${ cleanupError } ` ) ;
183+ throw new Error (
184+ `Failed to create cache archive due to insufficient disk space. Error: ${ errorMessage } . Cleanup failed: ${ cleanupError ?. message || cleanupError } ` ,
185+ ) ;
117186 }
118- throw new Error (
119- `Failed to create cache archive due to insufficient disk space. Error: ${ errorMessage } . Please free up disk space and retry.` ,
120- ) ;
187+ } else {
188+ throw error ;
121189 }
122- throw error ;
123190 }
124191 await CloudRunnerSystem . Run ( `du ${ cacheArtifactName } .tar${ compressionSuffix } ` ) ;
125192 assert ( await fileExists ( `${ cacheArtifactName } .tar${ compressionSuffix } ` ) , 'cache archive exists' ) ;
0 commit comments