Skip to content

Commit 2238b0d

Browse files
committed
pipeline: log more errors inside cleanup steps
1 parent ac04696 commit 2238b0d

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

internal/pipeline/cleanup.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package pipeline
2020
import (
2121
"context"
2222
"fmt"
23+
"os"
2324
"path/filepath"
2425
"regexp"
2526
"time"
@@ -178,20 +179,24 @@ func (cs *CleanupService) runCleanup(ctx context.Context) {
178179
continue
179180
}
180181

182+
logger := cs.logger.Warn().With(log.Fields{
183+
"shard": log.String(cs.shard.Name),
184+
"directory": log.String(dirName),
185+
})
186+
181187
// 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+
187193
errorCount++
188194
cleanupErrorsCounter.WithLabelValues(cs.shard.Name, "check_directory").Inc()
189-
} else if shouldDelete {
195+
196+
case shouldDelete:
190197
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+
195200
errorCount++
196201
cleanupErrorsCounter.WithLabelValues(cs.shard.Name, "delete_directory").Inc()
197202
} else {
@@ -203,12 +208,13 @@ func (cs *CleanupService) runCleanup(ctx context.Context) {
203208

204209
duration := time.Since(startTime)
205210

206-
cs.logger.Info().With(log.Fields{
211+
logger := cs.logger.With(log.Fields{
207212
"shard": log.String(cs.shard.Name),
208213
"deleted": log.Int(deletedCount),
209214
"errors": log.Int(errorCount),
210215
"duration": log.String(duration.String()),
211-
}).Log("completed cleanup run")
216+
})
217+
logger.Info().Log("completed cleanup run")
212218

213219
span.SetAttributes(
214220
attribute.Int("achgateway.cleanup.deleted_count", deletedCount),
@@ -325,7 +331,9 @@ func (cs *CleanupService) GetStats(ctx context.Context) (*CleanupStats, error) {
325331

326332
entries, err := cs.storage.ReadDir(".")
327333
if err != nil {
328-
return nil, err
334+
wd, _ := os.Getwd()
335+
336+
return nil, fmt.Errorf("reading %s failed: %w", wd, err)
329337
}
330338

331339
cutoffTime := time.Now().Add(-cs.config.RetentionDuration)
@@ -337,15 +345,19 @@ func (cs *CleanupService) GetStats(ctx context.Context) (*CleanupStats, error) {
337345

338346
info, err := entry.Info()
339347
if err != nil {
340-
continue
348+
return nil, fmt.Errorf("getting info on %s failed: %w", entry.Name(), err)
341349
}
342350

343351
stats.TotalDirectories++
344352

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 {
346355
stats.EligibleForDeletion++
347356
stats.TotalSize += info.Size()
348357
}
358+
if err != nil {
359+
return nil, fmt.Errorf("checking %s to delete failed: %w", entry.Name(), err)
360+
}
349361
}
350362

351363
return stats, nil

0 commit comments

Comments
 (0)