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.

update_storage.go 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package event
  2. import (
  3. tskcst "gitlink.org.cn/cloudream/common/consts/event"
  4. "gitlink.org.cn/cloudream/common/utils/logger"
  5. mysql "gitlink.org.cn/cloudream/db/sql"
  6. scevt "gitlink.org.cn/cloudream/rabbitmq/message/scanner/event"
  7. )
  8. type UpdateStorageEntry = scevt.UpdateStorageEntry
  9. type UpdateStorage struct {
  10. scevt.UpdateStorage
  11. }
  12. func NewUpdateStorage(storageID int, dirState string, entries []UpdateStorageEntry) *UpdateStorage {
  13. return &UpdateStorage{
  14. UpdateStorage: scevt.NewUpdateStorage(storageID, dirState, entries),
  15. }
  16. }
  17. func (t *UpdateStorage) TryMerge(other Event) bool {
  18. event, ok := other.(*UpdateStorage)
  19. if !ok {
  20. return false
  21. }
  22. if event.StorageID != t.StorageID {
  23. return false
  24. }
  25. // 后投递的任务的状态更新一些
  26. t.DirectoryState = event.DirectoryState
  27. // TODO 可以考虑合并同FileHash和NodeID的记录
  28. t.Entries = append(t.Entries, event.Entries...)
  29. return true
  30. }
  31. func (t *UpdateStorage) Execute(execCtx ExecuteContext) {
  32. logger.Debugf("begin update storage")
  33. err := mysql.Storage.ChangeState(execCtx.Args.DB.SQLCtx(), t.StorageID, t.DirectoryState)
  34. if err != nil {
  35. logger.WithField("StorageID", t.StorageID).Warnf("change storage state failed, err: %s", err.Error())
  36. }
  37. var chkObjIDs []int
  38. for _, entry := range t.Entries {
  39. switch entry.Operation {
  40. case tskcst.UPDATE_STORAGE_DELETE:
  41. err := mysql.StorageObject.Delete(execCtx.Args.DB.SQLCtx(), t.StorageID, entry.ObjectID, entry.UserID)
  42. if err != nil {
  43. logger.WithField("StorageID", t.StorageID).
  44. WithField("ObjectID", entry.ObjectID).
  45. Warnf("delete storage object failed, err: %s", err.Error())
  46. }
  47. chkObjIDs = append(chkObjIDs, entry.ObjectID)
  48. logger.WithField("StorageID", t.StorageID).
  49. WithField("ObjectID", entry.ObjectID).
  50. WithField("UserID", entry.UserID).
  51. Debugf("delete storage object")
  52. case tskcst.UPDATE_STORAGE_SET_NORMAL:
  53. err := mysql.StorageObject.SetStateNormal(execCtx.Args.DB.SQLCtx(), t.StorageID, entry.ObjectID, entry.UserID)
  54. if err != nil {
  55. logger.WithField("StorageID", t.StorageID).
  56. WithField("ObjectID", entry.ObjectID).
  57. Warnf("change storage object state failed, err: %s", err.Error())
  58. }
  59. logger.WithField("StorageID", t.StorageID).
  60. WithField("ObjectID", entry.ObjectID).
  61. WithField("UserID", entry.UserID).
  62. Debugf("set storage object normal")
  63. }
  64. }
  65. if len(chkObjIDs) > 0 {
  66. execCtx.Executor.Post(NewCheckObject(chkObjIDs))
  67. }
  68. }
  69. func init() {
  70. RegisterMessageConvertor(func(msg scevt.UpdateStorage) Event {
  71. return NewUpdateStorage(msg.StorageID, msg.DirectoryState, msg.Entries)
  72. })
  73. }

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