You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

check_storage.go 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package event
  2. import (
  3. "io/fs"
  4. "io/ioutil"
  5. "path/filepath"
  6. "github.com/samber/lo"
  7. "gitlink.org.cn/cloudream/agent/internal/config"
  8. "gitlink.org.cn/cloudream/common/consts"
  9. evcst "gitlink.org.cn/cloudream/common/consts/event"
  10. "gitlink.org.cn/cloudream/common/pkg/logger"
  11. "gitlink.org.cn/cloudream/common/utils"
  12. "gitlink.org.cn/cloudream/db/model"
  13. agtevt "gitlink.org.cn/cloudream/rabbitmq/message/agent/event"
  14. scevt "gitlink.org.cn/cloudream/rabbitmq/message/scanner/event"
  15. )
  16. type CheckStorage struct {
  17. agtevt.CheckStorage
  18. }
  19. func NewCheckStorage(storageID int, dir string, isComplete bool, objects []model.StorageObject) *CheckStorage {
  20. return &CheckStorage{
  21. CheckStorage: agtevt.NewCheckStorage(storageID, dir, isComplete, objects),
  22. }
  23. }
  24. func (t *CheckStorage) TryMerge(other Event) bool {
  25. event, ok := other.(*CheckStorage)
  26. if !ok {
  27. return false
  28. }
  29. if event.StorageID != t.StorageID {
  30. return false
  31. }
  32. if event.IsComplete {
  33. t.IsComplete = true
  34. t.Objects = event.Objects
  35. return true
  36. }
  37. if !t.IsComplete {
  38. t.Objects = append(t.Objects, event.Objects...)
  39. t.Objects = lo.UniqBy(t.Objects, func(obj model.StorageObject) int { return obj.ObjectID })
  40. return true
  41. }
  42. return false
  43. }
  44. func (t *CheckStorage) Execute(execCtx ExecuteContext) {
  45. log := logger.WithType[CheckStorage]("Event")
  46. log.Debugf("begin with %v", logger.FormatStruct(t))
  47. dirFullPath := filepath.Join(config.Cfg().StorageBaseDir, t.Directory)
  48. infos, err := ioutil.ReadDir(dirFullPath)
  49. if err != nil {
  50. log.Warnf("list storage directory failed, err: %s", err.Error())
  51. err := execCtx.Args.Scanner.PostEvent(scevt.NewUpdateStorage(t.StorageID, err.Error(), nil),
  52. execCtx.Option.IsEmergency,
  53. execCtx.Option.DontMerge,
  54. )
  55. if err != nil {
  56. log.Warnf("post event to scanner failed, err: %s", err.Error())
  57. }
  58. return
  59. }
  60. fileInfos := lo.Filter(infos, func(info fs.FileInfo, index int) bool { return !info.IsDir() })
  61. if t.IsComplete {
  62. t.checkComplete(fileInfos, execCtx)
  63. } else {
  64. t.checkIncrement(fileInfos, execCtx)
  65. }
  66. }
  67. func (t *CheckStorage) checkIncrement(fileInfos []fs.FileInfo, execCtx ExecuteContext) {
  68. log := logger.WithType[CheckStorage]("Event")
  69. infosMap := make(map[string]fs.FileInfo)
  70. for _, info := range fileInfos {
  71. infosMap[info.Name()] = info
  72. }
  73. var updateStorageOps []scevt.UpdateStorageEntry
  74. for _, obj := range t.Objects {
  75. fileName := utils.MakeMoveOperationFileName(obj.ObjectID, obj.UserID)
  76. _, ok := infosMap[fileName]
  77. if ok {
  78. // 不需要做处理
  79. // 删除map中的记录,表示此记录已被检查过
  80. delete(infosMap, fileName)
  81. } else {
  82. // 只要文件不存在,就删除StorageObject表中的记录
  83. updateStorageOps = append(updateStorageOps, scevt.NewUpdateStorageEntry(obj.ObjectID, obj.UserID, evcst.UPDATE_STORAGE_DELETE))
  84. }
  85. }
  86. // 增量情况下,不需要对infosMap中没检查的记录进行处理
  87. err := execCtx.Args.Scanner.PostEvent(
  88. scevt.NewUpdateStorage(t.StorageID, consts.STORAGE_DIRECTORY_STATUS_OK, updateStorageOps),
  89. execCtx.Option.IsEmergency,
  90. execCtx.Option.DontMerge,
  91. )
  92. if err != nil {
  93. log.Warnf("post event to scanner failed, err: %s", err.Error())
  94. }
  95. }
  96. func (t *CheckStorage) checkComplete(fileInfos []fs.FileInfo, execCtx ExecuteContext) {
  97. log := logger.WithType[CheckStorage]("Event")
  98. infosMap := make(map[string]fs.FileInfo)
  99. for _, info := range fileInfos {
  100. infosMap[info.Name()] = info
  101. }
  102. var updateStorageOps []scevt.UpdateStorageEntry
  103. for _, obj := range t.Objects {
  104. fileName := utils.MakeMoveOperationFileName(obj.ObjectID, obj.UserID)
  105. _, ok := infosMap[fileName]
  106. if ok {
  107. // 不需要做处理
  108. // 删除map中的记录,表示此记录已被检查过
  109. delete(infosMap, fileName)
  110. } else {
  111. // 只要文件不存在,就删除StorageObject表中的记录
  112. updateStorageOps = append(updateStorageOps, scevt.NewUpdateStorageEntry(obj.ObjectID, obj.UserID, evcst.UPDATE_STORAGE_DELETE))
  113. }
  114. }
  115. // Storage中多出来的文件不做处理
  116. err := execCtx.Args.Scanner.PostEvent(
  117. scevt.NewUpdateStorage(t.StorageID, consts.STORAGE_DIRECTORY_STATUS_OK, updateStorageOps),
  118. execCtx.Option.IsEmergency,
  119. execCtx.Option.DontMerge,
  120. )
  121. if err != nil {
  122. log.Warnf("post event to scanner failed, err: %s", err.Error())
  123. }
  124. }
  125. func init() {
  126. Register(func(val agtevt.CheckStorage) Event {
  127. return NewCheckStorage(val.StorageID, val.Directory, val.IsComplete, val.Objects)
  128. })
  129. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。