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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package http
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/gin-gonic/gin"
  6. "gitlink.org.cn/cloudream/common/consts/errorcode"
  7. "gitlink.org.cn/cloudream/common/pkgs/logger"
  8. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  9. )
  10. // CacheService 缓存服务结构体,依赖于Server
  11. type CacheService struct {
  12. *Server
  13. }
  14. // Cache 返回CacheService的实例
  15. func (s *Server) Cache() *CacheService {
  16. return &CacheService{
  17. Server: s,
  18. }
  19. }
  20. // CacheMovePackageReq 移动缓存包的请求参数
  21. type CacheMovePackageReq struct {
  22. UserID *cdssdk.UserID `json:"userID" binding:"required"`
  23. PackageID *cdssdk.PackageID `json:"packageID" binding:"required"`
  24. NodeID *cdssdk.NodeID `json:"nodeID" binding:"required"`
  25. }
  26. // CacheMovePackageResp 移动缓存包的响应参数
  27. type CacheMovePackageResp = cdssdk.CacheMovePackageResp
  28. // MovePackage 处理移动缓存包的请求
  29. func (s *CacheService) MovePackage(ctx *gin.Context) {
  30. // 初始化日志
  31. log := logger.WithField("HTTP", "Cache.LoadPackage")
  32. // 绑定请求JSON
  33. var req CacheMovePackageReq
  34. if err := ctx.ShouldBindJSON(&req); err != nil {
  35. log.Warnf("binding body: %s", err.Error())
  36. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  37. return
  38. }
  39. // 开始移动缓存包任务
  40. taskID, err := s.svc.CacheSvc().StartCacheMovePackage(*req.UserID, *req.PackageID, *req.NodeID)
  41. if err != nil {
  42. log.Warnf("start cache move package: %s", err.Error())
  43. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "cache move package failed"))
  44. return
  45. }
  46. // 循环等待缓存包移动完成
  47. for {
  48. // 检查移动是否完成
  49. complete, err := s.svc.CacheSvc().WaitCacheMovePackage(*req.NodeID, taskID, time.Second*10)
  50. if complete {
  51. // 移动完成后的处理
  52. if err != nil {
  53. log.Warnf("moving complete with: %s", err.Error())
  54. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "cache move package failed"))
  55. return
  56. }
  57. ctx.JSON(http.StatusOK, OK(CacheMovePackageResp{}))
  58. return
  59. }
  60. // 等待移动过程中的错误处理
  61. if err != nil {
  62. log.Warnf("wait moving: %s", err.Error())
  63. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "cache move package failed"))
  64. return
  65. }
  66. }
  67. }

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