@@ -20,6 +20,7 @@ package pipeline
20
20
import (
21
21
"context"
22
22
"fmt"
23
+ "os"
23
24
"path/filepath"
24
25
"regexp"
25
26
"time"
@@ -178,20 +179,24 @@ func (cs *CleanupService) runCleanup(ctx context.Context) {
178
179
continue
179
180
}
180
181
182
+ logger := cs .logger .Warn ().With (log.Fields {
183
+ "shard" : log .String (cs .shard .Name ),
184
+ "directory" : log .String (dirName ),
185
+ })
186
+
181
187
// Check if directory should be deleted
182
- if shouldDelete , err := cs .shouldDeleteDirectory (ctx , dirName , cutoffTime ); err != nil {
183
- cs . logger . Warn (). With (log. Fields {
184
- "shard" : log . String ( cs . shard . Name ),
185
- " directory" : log . String ( dirName ),
186
- }). Logf ( "error checking directory: %v" , err )
188
+ shouldDelete , err := cs .shouldDeleteDirectory (ctx , dirName , cutoffTime )
189
+ switch {
190
+ case err != nil :
191
+ logger . Error (). Logf ( "error checking directory: %v" , err )
192
+
187
193
errorCount ++
188
194
cleanupErrorsCounter .WithLabelValues (cs .shard .Name , "check_directory" ).Inc ()
189
- } else if shouldDelete {
195
+
196
+ case shouldDelete :
190
197
if err := cs .deleteDirectory (ctx , dirName ); err != nil {
191
- cs .logger .Error ().With (log.Fields {
192
- "shard" : log .String (cs .shard .Name ),
193
- "directory" : log .String (dirName ),
194
- }).LogErrorf ("failed to delete directory: %v" , err )
198
+ logger .Error ().LogErrorf ("failed to delete directory: %v" , err )
199
+
195
200
errorCount ++
196
201
cleanupErrorsCounter .WithLabelValues (cs .shard .Name , "delete_directory" ).Inc ()
197
202
} else {
@@ -203,12 +208,13 @@ func (cs *CleanupService) runCleanup(ctx context.Context) {
203
208
204
209
duration := time .Since (startTime )
205
210
206
- cs .logger . Info () .With (log.Fields {
211
+ logger := cs .logger .With (log.Fields {
207
212
"shard" : log .String (cs .shard .Name ),
208
213
"deleted" : log .Int (deletedCount ),
209
214
"errors" : log .Int (errorCount ),
210
215
"duration" : log .String (duration .String ()),
211
- }).Log ("completed cleanup run" )
216
+ })
217
+ logger .Info ().Log ("completed cleanup run" )
212
218
213
219
span .SetAttributes (
214
220
attribute .Int ("achgateway.cleanup.deleted_count" , deletedCount ),
@@ -325,7 +331,9 @@ func (cs *CleanupService) GetStats(ctx context.Context) (*CleanupStats, error) {
325
331
326
332
entries , err := cs .storage .ReadDir ("." )
327
333
if err != nil {
328
- return nil , err
334
+ wd , _ := os .Getwd ()
335
+
336
+ return nil , fmt .Errorf ("reading %s failed: %w" , wd , err )
329
337
}
330
338
331
339
cutoffTime := time .Now ().Add (- cs .config .RetentionDuration )
@@ -337,15 +345,19 @@ func (cs *CleanupService) GetStats(ctx context.Context) (*CleanupStats, error) {
337
345
338
346
info , err := entry .Info ()
339
347
if err != nil {
340
- continue
348
+ return nil , fmt . Errorf ( "getting info on %s failed: %w" , entry . Name (), err )
341
349
}
342
350
343
351
stats .TotalDirectories ++
344
352
345
- if shouldDelete , err := cs .shouldDeleteDirectory (ctx , entry .Name (), cutoffTime ); err == nil && shouldDelete {
353
+ shouldDelete , err := cs .shouldDeleteDirectory (ctx , entry .Name (), cutoffTime )
354
+ if err == nil && shouldDelete {
346
355
stats .EligibleForDeletion ++
347
356
stats .TotalSize += info .Size ()
348
357
}
358
+ if err != nil {
359
+ return nil , fmt .Errorf ("checking %s to delete failed: %w" , entry .Name (), err )
360
+ }
349
361
}
350
362
351
363
return stats , nil
0 commit comments