@@ -560,54 +560,52 @@ func doLoadFromExternalStorage(ctx context.Context, input *iwfidl.EncodedObject)
560560
561561func CleanupBlobStore (
562562 ctx context.Context , backendType service.BackendType , storeId string ,
563- ) error {
563+ ) ( int , error ) {
564564 store := env .GetBlobStore ()
565565 provider := interfaces .GetActivityProviderByType (backendType )
566566 logger := provider .GetLogger (ctx )
567567 logger .Info ("CleanupBlobStore started" )
568- cfg := env .GetSharedConfig ()
569- minAgeForCleanupCheckInDays := cfg .ExternalStorage .MinAgeForCleanupCheckInDays
570- stopChecingkUnixSeconds := time .Now ().Unix () - int64 (minAgeForCleanupCheckInDays )* 24 * 3600
571- var continueToken * string
572568
573569 client := env .GetUnifiedClient ()
570+
571+ var continueToken * string
572+ var totalDeleted int
573+
574574 for {
575575 listOutput , err := store .ListWorkflowPaths (ctx , blobstore.ListObjectPathsInput {
576576 StoreId : storeId ,
577577 ContinuationToken : continueToken ,
578578 })
579579 if err != nil {
580- return err
580+ return totalDeleted , err
581581 }
582582 continueToken = listOutput .ContinuationToken
583583 for _ , workflowPath := range listOutput .WorkflowPaths {
584- currentTimestamp , valid := blobstore .ExtractYyyymmddToUnixSeconds (workflowPath )
584+ _ , valid := blobstore .ExtractYyyymmddToUnixSeconds (workflowPath )
585585 if ! valid {
586586 logger .Info ("CleanupBlobStore skipped workflow path" , "path" , workflowPath )
587587 continue
588588 }
589- if currentTimestamp < stopChecingkUnixSeconds {
590- logger .Info ("CleanupBlobStore stopped checking at" , "currentTimestamp" , currentTimestamp , "stopChecingkUnixSeconds" , stopChecingkUnixSeconds )
591- break
592- }
593589
590+ // Check if workflow still exists in Temporal
591+ // We must always check because workflows can run indefinitely,
592+ // and the retention period only applies after workflow closure
594593 workflowId := blobstore .MustExtractWorkflowId (workflowPath )
595594 _ , err := client .DescribeWorkflowExecution (ctx , workflowId , "" , nil )
596595 if client .IsNotFoundError (err ) {
597- // this means workflow has been deleted from the history
596+ // Workflow has been removed from Temporal, safe to delete S3 objects
598597 err = store .DeleteWorkflowObjects (ctx , storeId , workflowPath )
599598 if err != nil {
600599 logger .Error ("CleanupBlobStore failed to delete workflow objects" , "workflowPath" , workflowPath , "error" , err )
601- return err
602- } else {
603- logger .Info ("CleanupBlobStore deleted workflow objects" , "workflowPath" , workflowPath )
604- }
605- } else {
606- if err != nil {
607- logger .Error ("CleanupBlobStore failed to delete workflow objects" , "workflowPath" , workflowPath , "error" , err )
608- return err
600+ return totalDeleted , err
609601 }
602+ totalDeleted ++
603+ logger .Info ("CleanupBlobStore deleted workflow objects" , "workflowPath" , workflowPath )
604+ } else if err != nil {
605+ logger .Error ("CleanupBlobStore failed to describe workflow" , "workflowPath" , workflowPath , "error" , err )
606+ return totalDeleted , err
610607 }
608+ // If no error, workflow still exists (open or within retention), don't delete
611609
612610 // this is a long running activity
613611 // using record heartbeat so that it won't timeout at startToClose timeout
@@ -617,5 +615,6 @@ func CleanupBlobStore(
617615 break
618616 }
619617 }
620- return nil
618+ logger .Info ("CleanupBlobStore completed" , "totalDeleted" , totalDeleted )
619+ return totalDeleted , nil
621620}
0 commit comments