Skip to content

Commit 5677b89

Browse files
authored
Clean up files in processingDir (temporary staging dir) (#629)
* Clean up files in processingDir (temporary staging dir) * Clean up activity take several paths as input
1 parent 521ff28 commit 5677b89

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

internal/workflow/activities/cleanup.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ func NewCleanUpActivity() *CleanUpActivity {
1414
}
1515

1616
type CleanUpActivityParams struct {
17-
FullPath string
17+
Paths []string
1818
}
1919

2020
func (a *CleanUpActivity) Execute(ctx context.Context, params *CleanUpActivityParams) error {
21-
if params == nil || params.FullPath == "" {
22-
return fmt.Errorf("error processing parameters: missing or empty")
21+
if params == nil {
22+
return fmt.Errorf("error processing parameters: missing")
2323
}
2424

25-
if err := os.RemoveAll(params.FullPath); err != nil {
26-
return fmt.Errorf("error removing transfer directory: %v", err)
25+
for _, p := range params.Paths {
26+
if err := os.RemoveAll(p); err != nil {
27+
return fmt.Errorf("error removing path: %v", err)
28+
}
2729
}
2830

2931
return nil

internal/workflow/processing.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,8 @@ func (w *ProcessingWorkflow) SessionHandler(sessCtx temporalsdk_workflow.Context
457457
if tinfo.WatcherName != "" && !tinfo.IsDir {
458458
// TODO: even if TempFile is defined, we should confirm that the file is
459459
// locally available in disk, just in case we're in the context of a
460-
// session retry where a different working is doing the work. In that
461-
// case, the activity whould be executed again.
460+
// session retry where a different worker is doing the work. In that
461+
// case, the activity would be executed again.
462462
if tinfo.TempFile == "" {
463463
activityOpts := withActivityOptsForLongLivedRequest(sessCtx)
464464
err := temporalsdk_workflow.ExecuteActivity(
@@ -475,6 +475,9 @@ func (w *ProcessingWorkflow) SessionHandler(sessCtx temporalsdk_workflow.Context
475475
}
476476
}
477477

478+
// Both of these values relate to temporary files on Enduro's processing Dir that never get cleaned-up.
479+
var tempBlob, tempExtracted string
480+
tempBlob = tinfo.TempFile
478481
// Extract downloaded archive file contents.
479482
{
480483
if tinfo.WatcherName != "" && !tinfo.IsDir {
@@ -497,6 +500,7 @@ func (w *ProcessingWorkflow) SessionHandler(sessCtx temporalsdk_workflow.Context
497500
tinfo.TempFile = result.ExtractPath
498501
tinfo.StripTopLevelDir = false
499502
tinfo.IsDir = true
503+
tempExtracted = result.ExtractPath
500504
}
501505
}
502506
}
@@ -523,11 +527,25 @@ func (w *ProcessingWorkflow) SessionHandler(sessCtx temporalsdk_workflow.Context
523527

524528
// Delete local temporary files.
525529
defer func() {
530+
// We need disconnected context here because when session gets released the cleanup
531+
// activities get scheduled and then immediately canceled.
532+
var filesToRemove []string
526533
if tinfo.Bundle.FullPathBeforeStrip != "" {
527-
activityOpts := withActivityOptsForLocalAction(sessCtx)
528-
_ = temporalsdk_workflow.ExecuteActivity(activityOpts, activities.CleanUpActivityName, &activities.CleanUpActivityParams{
529-
FullPath: tinfo.Bundle.FullPathBeforeStrip,
530-
}).Get(activityOpts, nil)
534+
filesToRemove = append(filesToRemove, tinfo.Bundle.FullPathBeforeStrip)
535+
}
536+
if tempBlob != "" {
537+
filesToRemove = append(filesToRemove, tempBlob)
538+
}
539+
if tempExtracted != "" {
540+
filesToRemove = append(filesToRemove, tempExtracted)
541+
}
542+
cleanUpCtx, cancel := temporalsdk_workflow.NewDisconnectedContext(sessCtx)
543+
defer cancel()
544+
activityOpts := withActivityOptsForLocalAction(cleanUpCtx)
545+
if err := temporalsdk_workflow.ExecuteActivity(activityOpts, activities.CleanUpActivityName, &activities.CleanUpActivityParams{
546+
Paths: filesToRemove,
547+
}).Get(activityOpts, nil); err != nil {
548+
w.logger.Error(err, "failed to clean up temporary files", "path", tempExtracted)
531549
}
532550
}()
533551

0 commit comments

Comments
 (0)