dataDownloadCleanupService.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package services
  2. import (
  3. "context"
  4. "time"
  5. "fwh-toolbox-server/cache"
  6. "fwh-toolbox-server/domain"
  7. minioRepo "fwh-toolbox-server/repositories/minio"
  8. mongoRepo "fwh-toolbox-server/repositories/mongo"
  9. "go.mongodb.org/mongo-driver/bson"
  10. "go.uber.org/zap"
  11. )
  12. func CleanupDataDownloadPackages(ctx context.Context) error {
  13. now := time.Now().UTC()
  14. jobs, err := mongoRepo.FindExpiredDataDownloadPackageJobs(ctx, now)
  15. if err != nil {
  16. return err
  17. }
  18. if len(jobs) > 0 {
  19. zap.L().Info("[DataDownload] 清理过期 Package", zap.Int("count", len(jobs)))
  20. }
  21. for i := range jobs {
  22. lock, ok, lockErr := cache.TryLock(ctx, "data-download:package:"+jobs[i].ID.Hex(), 5*time.Minute)
  23. if lockErr != nil || !ok {
  24. continue
  25. }
  26. if jobs[i].ObjectKey != "" {
  27. if err := minioRepo.Delete(ctx, jobs[i].ObjectKey); err != nil {
  28. lock.SafeRelease(context.Background())
  29. continue
  30. }
  31. }
  32. purge := jobs[i].ExpiresAt.Add(30 * 24 * time.Hour)
  33. _ = mongoRepo.UpdateDataDownloadPackageJob(ctx, jobs[i].ID, bson.M{"status": domain.DataDownloadExpired, "objectKey": "", "purgeAt": purge})
  34. lock.SafeRelease(context.Background())
  35. }
  36. return mongoRepo.PurgeExpiredDataDownloadPackageJobs(ctx, now)
  37. }
  38. func CleanupDataDownloadArtifacts(ctx context.Context) error {
  39. artifacts, err := mongoRepo.FindExpiredDataDownloadArtifacts(ctx, time.Now().UTC())
  40. if err != nil {
  41. return err
  42. }
  43. if len(artifacts) > 0 {
  44. zap.L().Info("[DataDownload] 清理过期渲染 Artifact", zap.Int("count", len(artifacts)))
  45. }
  46. for i := range artifacts {
  47. lock, lockErr := cache.Lock(ctx, "data-download:content:"+artifacts[i].DocumentID, 10*time.Minute, 100*time.Millisecond, 10*time.Second)
  48. if lockErr != nil {
  49. continue
  50. }
  51. if err := mongoRepo.DetachDataDownloadArtifact(ctx, artifacts[i]); err != nil {
  52. lock.SafeRelease(context.Background())
  53. continue
  54. }
  55. if err := minioRepo.Delete(ctx, artifacts[i].ObjectKey); err != nil {
  56. lock.SafeRelease(context.Background())
  57. continue
  58. }
  59. _ = mongoRepo.DeleteDataDownloadArtifact(ctx, artifacts[i])
  60. lock.SafeRelease(context.Background())
  61. }
  62. return nil
  63. }