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.

cache.go 2.9 kB

2 years ago
2 years ago
2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package mq
  2. import (
  3. "time"
  4. "github.com/samber/lo"
  5. "gitlink.org.cn/cloudream/common/consts/errorcode"
  6. "gitlink.org.cn/cloudream/common/pkgs/logger"
  7. "gitlink.org.cn/cloudream/common/pkgs/mq"
  8. mytask "gitlink.org.cn/cloudream/storage/agent/internal/task"
  9. stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  10. agtmq "gitlink.org.cn/cloudream/storage/common/pkgs/mq/agent"
  11. )
  12. func (svc *Service) CheckCache(msg *agtmq.CheckCache) (*agtmq.CheckCacheResp, *mq.CodeMessage) {
  13. ipfsCli, err := stgglb.IPFSPool.Acquire()
  14. if err != nil {
  15. logger.Warnf("new ipfs client: %s", err.Error())
  16. return nil, mq.Failed(errorcode.OperationFailed, "new ipfs client failed")
  17. }
  18. defer ipfsCli.Close()
  19. files, err := ipfsCli.GetPinnedFiles()
  20. if err != nil {
  21. logger.Warnf("get pinned files from ipfs failed, err: %s", err.Error())
  22. return nil, mq.Failed(errorcode.OperationFailed, "get pinned files from ipfs failed")
  23. }
  24. return mq.ReplyOK(agtmq.NewCheckCacheResp(lo.Keys(files)))
  25. }
  26. func (svc *Service) CacheGC(msg *agtmq.CacheGC) (*agtmq.CacheGCResp, *mq.CodeMessage) {
  27. ipfsCli, err := stgglb.IPFSPool.Acquire()
  28. if err != nil {
  29. logger.Warnf("new ipfs client: %s", err.Error())
  30. return nil, mq.Failed(errorcode.OperationFailed, "new ipfs client failed")
  31. }
  32. defer ipfsCli.Close()
  33. files, err := ipfsCli.GetPinnedFiles()
  34. if err != nil {
  35. logger.Warnf("get pinned files from ipfs failed, err: %s", err.Error())
  36. return nil, mq.Failed(errorcode.OperationFailed, "get pinned files from ipfs failed")
  37. }
  38. // unpin所有没有没记录到元数据的文件
  39. shouldPinnedFiles := lo.SliceToMap(msg.PinnedFileHashes, func(hash string) (string, bool) { return hash, true })
  40. for hash := range files {
  41. if !shouldPinnedFiles[hash] {
  42. ipfsCli.Unpin(hash)
  43. logger.WithField("FileHash", hash).Debugf("unpinned by gc")
  44. }
  45. }
  46. return mq.ReplyOK(agtmq.RespCacheGC())
  47. }
  48. func (svc *Service) StartCacheMovePackage(msg *agtmq.StartCacheMovePackage) (*agtmq.StartCacheMovePackageResp, *mq.CodeMessage) {
  49. tsk := svc.taskManager.StartNew(mytask.NewCacheMovePackage(msg.UserID, msg.PackageID))
  50. return mq.ReplyOK(agtmq.NewStartCacheMovePackageResp(tsk.ID()))
  51. }
  52. func (svc *Service) WaitCacheMovePackage(msg *agtmq.WaitCacheMovePackage) (*agtmq.WaitCacheMovePackageResp, *mq.CodeMessage) {
  53. tsk := svc.taskManager.FindByID(msg.TaskID)
  54. if tsk == nil {
  55. return nil, mq.Failed(errorcode.TaskNotFound, "task not found")
  56. }
  57. if msg.WaitTimeoutMs == 0 {
  58. tsk.Wait()
  59. errMsg := ""
  60. if tsk.Error() != nil {
  61. errMsg = tsk.Error().Error()
  62. }
  63. return mq.ReplyOK(agtmq.NewWaitCacheMovePackageResp(true, errMsg))
  64. } else {
  65. if tsk.WaitTimeout(time.Duration(msg.WaitTimeoutMs) * time.Millisecond) {
  66. errMsg := ""
  67. if tsk.Error() != nil {
  68. errMsg = tsk.Error().Error()
  69. }
  70. return mq.ReplyOK(agtmq.NewWaitCacheMovePackageResp(true, errMsg))
  71. }
  72. return mq.ReplyOK(agtmq.NewWaitCacheMovePackageResp(false, ""))
  73. }
  74. }

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