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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. scmsg "gitlink.org.cn/cloudream/rabbitmq/message/scanner"
  15. scevt "gitlink.org.cn/cloudream/rabbitmq/message/scanner/event"
  16. )
  17. type CheckStorage struct {
  18. agtevt.CheckStorage
  19. }
  20. func NewCheckStorage(storageID int, dir string, isComplete bool, objects []model.StorageObject) *CheckStorage {
  21. return &CheckStorage{
  22. CheckStorage: agtevt.NewCheckStorage(storageID, dir, isComplete, objects),
  23. }
  24. }
  25. func (t *CheckStorage) TryMerge(other Event) bool {
  26. event, ok := other.(*CheckStorage)
  27. if !ok {
  28. return false
  29. }
  30. if event.IsComplete {
  31. t.IsComplete = true
  32. t.Objects = event.Objects
  33. return true
  34. }
  35. if !t.IsComplete {
  36. t.Objects = append(t.Objects, event.Objects...)
  37. t.Objects = lo.UniqBy(t.Objects, func(obj model.StorageObject) int { return obj.ObjectID })
  38. return true
  39. }
  40. return false
  41. }
  42. func (t *CheckStorage) Execute(execCtx ExecuteContext) {
  43. logger.Debugf("begin check storage")
  44. dirFullPath := filepath.Join(config.Cfg().StorageBaseDir, t.Directory)
  45. infos, err := ioutil.ReadDir(dirFullPath)
  46. if err != nil {
  47. logger.Warnf("list storage directory failed, err: %s", err.Error())
  48. evtmsg, err := scmsg.NewPostEventBody(scevt.NewUpdateStorage(
  49. t.StorageID,
  50. err.Error(),
  51. nil,
  52. ), execCtx.Option.IsEmergency, execCtx.Option.DontMerge)
  53. if err == nil {
  54. execCtx.Args.Scanner.PostEvent(evtmsg)
  55. } else {
  56. logger.Warnf("new post event body 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. infosMap := make(map[string]fs.FileInfo)
  69. for _, info := range fileInfos {
  70. infosMap[info.Name()] = info
  71. }
  72. var updateStorageOps []scevt.UpdateStorageEntry
  73. for _, obj := range t.Objects {
  74. fileName := utils.MakeMoveOperationFileName(obj.ObjectID, obj.UserID)
  75. _, ok := infosMap[fileName]
  76. if ok {
  77. // 不需要做处理
  78. // 删除map中的记录,表示此记录已被检查过
  79. delete(infosMap, fileName)
  80. } else {
  81. // 只要文件不存在,就删除StorageObject表中的记录
  82. updateStorageOps = append(updateStorageOps, scevt.NewUpdateStorageEntry(obj.ObjectID, obj.UserID, evcst.UPDATE_STORAGE_DELETE))
  83. }
  84. }
  85. // 增量情况下,不需要对infosMap中没检查的记录进行处理
  86. evtmsg, err := scmsg.NewPostEventBody(
  87. scevt.NewUpdateStorage(t.StorageID, consts.STORAGE_DIRECTORY_STATUS_OK, updateStorageOps),
  88. execCtx.Option.IsEmergency,
  89. execCtx.Option.DontMerge,
  90. )
  91. if err == nil {
  92. execCtx.Args.Scanner.PostEvent(evtmsg)
  93. } else {
  94. logger.Warnf("new post event body failed, err: %s", err.Error())
  95. }
  96. }
  97. func (t *CheckStorage) checkComplete(fileInfos []fs.FileInfo, execCtx ExecuteContext) {
  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. evtmsg, err := scmsg.NewPostEventBody(
  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. execCtx.Args.Scanner.PostEvent(evtmsg)
  123. } else {
  124. logger.Warnf("new post event body failed, err: %s", err.Error())
  125. }
  126. }
  127. func init() {
  128. Register(func(val agtevt.CheckStorage) Event {
  129. return NewCheckStorage(val.StorageID, val.Directory, val.IsComplete, val.Objects)
  130. })
  131. }

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