Skip to content

Commit fa96647

Browse files
committed
add cron task to delete need-delete artifacts
1 parent 72ac7a0 commit fa96647

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

models/actions/artifact.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ func ListNeedExpiredArtifacts(ctx context.Context) ([]*ActionArtifact, error) {
164164
Where("expired_unix < ? AND status = ?", timeutil.TimeStamp(time.Now().Unix()), ArtifactStatusUploadConfirmed).Find(&arts)
165165
}
166166

167+
// ListNeedDeleteArtifacts returns all need delete artifacts but not deleted
168+
func ListNeedDeleteArtifacts(ctx context.Context) ([]*ActionArtifact, error) {
169+
arts := make([]*ActionArtifact, 0, 10)
170+
return arts, db.GetEngine(ctx).
171+
Where("status = ?", ArtifactStatusNeedDelete).Find(&arts)
172+
}
173+
167174
// SetArtifactExpired sets an artifact to expired
168175
func SetArtifactExpired(ctx context.Context, artifactID int64) error {
169176
_, err := db.GetEngine(ctx).Where("id=? AND status = ?", artifactID, ArtifactStatusUploadConfirmed).Cols("status").Update(&ActionArtifact{Status: int64(ArtifactStatusExpired)})
@@ -175,3 +182,9 @@ func SetArtifactNeedDelete(ctx context.Context, runID int64, name string) error
175182
_, err := db.GetEngine(ctx).Where("run_id=? AND artifact_name=? AND status = ?", runID, name, ArtifactStatusUploadConfirmed).Cols("status").Update(&ActionArtifact{Status: int64(ArtifactStatusNeedDelete)})
176183
return err
177184
}
185+
186+
// SetArtifactDeleted sets an artifact to deleted
187+
func SetArtifactDeleted(ctx context.Context, artifactID int64) error {
188+
_, err := db.GetEngine(ctx).Where("id=?", artifactID).Cols("status").Update(&ActionArtifact{Status: int64(ArtifactStatusDeleted)})
189+
return err
190+
}

services/actions/cleanup.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,18 @@ func Cleanup(taskCtx context.Context, olderThan time.Duration) error {
2020
return CleanupArtifacts(taskCtx)
2121
}
2222

23-
// CleanupArtifacts removes expired artifacts and set records expired status
23+
// CleanupArtifacts removes expired add need-deleted artifacts and set records expired status
2424
func CleanupArtifacts(taskCtx context.Context) error {
25+
if err := cleanExpiredArtifacts(taskCtx); err != nil {
26+
return err
27+
}
28+
if err := cleanNeedDeleteArtifacts(taskCtx); err != nil {
29+
return err
30+
}
31+
return nil
32+
}
33+
34+
func cleanExpiredArtifacts(taskCtx context.Context) error {
2535
artifacts, err := actions.ListNeedExpiredArtifacts(taskCtx)
2636
if err != nil {
2737
return err
@@ -40,3 +50,23 @@ func CleanupArtifacts(taskCtx context.Context) error {
4050
}
4151
return nil
4252
}
53+
54+
func cleanNeedDeleteArtifacts(taskCtx context.Context) error {
55+
artifacts, err := actions.ListNeedDeleteArtifacts(taskCtx)
56+
if err != nil {
57+
return err
58+
}
59+
log.Info("Found %d need-deleted artifacts", len(artifacts))
60+
for _, artifact := range artifacts {
61+
if err := storage.ActionsArtifacts.Delete(artifact.StoragePath); err != nil {
62+
log.Error("Cannot delete artifact %d: %v", artifact.ID, err)
63+
continue
64+
}
65+
if err := actions.SetArtifactDeleted(taskCtx, artifact.ID); err != nil {
66+
log.Error("Cannot set artifact %d deleted: %v", artifact.ID, err)
67+
continue
68+
}
69+
log.Info("Artifact %d set deleted", artifact.ID)
70+
}
71+
return nil
72+
}

0 commit comments

Comments
 (0)