code.gitea.io/gitea@v1.22.3/services/actions/cleanup.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package actions 5 6 import ( 7 "context" 8 "time" 9 10 "code.gitea.io/gitea/models/actions" 11 "code.gitea.io/gitea/modules/log" 12 "code.gitea.io/gitea/modules/storage" 13 ) 14 15 // Cleanup removes expired actions logs, data and artifacts 16 func Cleanup(taskCtx context.Context, olderThan time.Duration) error { 17 // TODO: clean up expired actions logs 18 19 // clean up expired artifacts 20 return CleanupArtifacts(taskCtx) 21 } 22 23 // CleanupArtifacts removes expired add need-deleted artifacts and set records expired status 24 func CleanupArtifacts(taskCtx context.Context) error { 25 if err := cleanExpiredArtifacts(taskCtx); err != nil { 26 return err 27 } 28 return cleanNeedDeleteArtifacts(taskCtx) 29 } 30 31 func cleanExpiredArtifacts(taskCtx context.Context) error { 32 artifacts, err := actions.ListNeedExpiredArtifacts(taskCtx) 33 if err != nil { 34 return err 35 } 36 log.Info("Found %d expired artifacts", len(artifacts)) 37 for _, artifact := range artifacts { 38 if err := actions.SetArtifactExpired(taskCtx, artifact.ID); err != nil { 39 log.Error("Cannot set artifact %d expired: %v", artifact.ID, err) 40 continue 41 } 42 if err := storage.ActionsArtifacts.Delete(artifact.StoragePath); err != nil { 43 log.Error("Cannot delete artifact %d: %v", artifact.ID, err) 44 continue 45 } 46 log.Info("Artifact %d set expired", artifact.ID) 47 } 48 return nil 49 } 50 51 // deleteArtifactBatchSize is the batch size of deleting artifacts 52 const deleteArtifactBatchSize = 100 53 54 func cleanNeedDeleteArtifacts(taskCtx context.Context) error { 55 for { 56 artifacts, err := actions.ListPendingDeleteArtifacts(taskCtx, deleteArtifactBatchSize) 57 if err != nil { 58 return err 59 } 60 log.Info("Found %d artifacts pending deletion", len(artifacts)) 61 for _, artifact := range artifacts { 62 if err := actions.SetArtifactDeleted(taskCtx, artifact.ID); err != nil { 63 log.Error("Cannot set artifact %d deleted: %v", artifact.ID, err) 64 continue 65 } 66 if err := storage.ActionsArtifacts.Delete(artifact.StoragePath); err != nil { 67 log.Error("Cannot delete artifact %d: %v", artifact.ID, err) 68 continue 69 } 70 log.Info("Artifact %d set deleted", artifact.ID) 71 } 72 if len(artifacts) < deleteArtifactBatchSize { 73 log.Debug("No more artifacts pending deletion") 74 break 75 } 76 } 77 return nil 78 }