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.

io.go 2.7 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package mq
  2. import (
  3. "time"
  4. "gitlink.org.cn/cloudream/common/consts/errorcode"
  5. "gitlink.org.cn/cloudream/common/pkgs/logger"
  6. "gitlink.org.cn/cloudream/common/pkgs/mq"
  7. mytask "gitlink.org.cn/cloudream/storage/agent/internal/task"
  8. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch"
  9. agtmq "gitlink.org.cn/cloudream/storage/common/pkgs/mq/agent"
  10. )
  11. // SetupIOPlan 设置I/O计划。
  12. // msg: 包含I/O计划信息的消息体。
  13. // 返回值: 成功时返回响应消息和成功标志,失败时返回错误代码和消息。
  14. func (svc *Service) SetupIOPlan(msg *agtmq.SetupIOPlan) (*agtmq.SetupIOPlanResp, *mq.CodeMessage) {
  15. err := svc.sw.SetupPlan(msg.Plan)
  16. if err != nil {
  17. logger.WithField("PlanID", msg.Plan.ID).Warnf("adding plan: %s", err.Error())
  18. return nil, mq.Failed(errorcode.OperationFailed, "adding plan failed")
  19. }
  20. return mq.ReplyOK(agtmq.NewSetupIOPlanResp())
  21. }
  22. // StartIOPlan 启动I/O计划。
  23. // msg: 包含I/O计划ID的消息体。
  24. // 返回值: 成功时返回任务ID和成功标志,失败时返回错误代码和消息。
  25. func (svc *Service) StartIOPlan(msg *agtmq.StartIOPlan) (*agtmq.StartIOPlanResp, *mq.CodeMessage) {
  26. tsk := svc.taskManager.StartNew(mytask.NewExecuteIOPlan(msg.PlanID))
  27. return mq.ReplyOK(agtmq.NewStartIOPlanResp(tsk.ID()))
  28. }
  29. // WaitIOPlan 等待I/O计划完成。
  30. // msg: 包含任务ID和等待超时时间的消息体。
  31. // 返回值: 成功时返回任务完成状态、错误消息和结果,失败时返回错误代码和消息。
  32. func (svc *Service) WaitIOPlan(msg *agtmq.WaitIOPlan) (*agtmq.WaitIOPlanResp, *mq.CodeMessage) {
  33. tsk := svc.taskManager.FindByID(msg.TaskID)
  34. if tsk == nil {
  35. return nil, mq.Failed(errorcode.TaskNotFound, "task not found")
  36. }
  37. if msg.WaitTimeoutMs == 0 {
  38. tsk.Wait()
  39. errMsg := ""
  40. if tsk.Error() != nil {
  41. errMsg = tsk.Error().Error()
  42. }
  43. planTsk := tsk.Body().(*mytask.ExecuteIOPlan)
  44. return mq.ReplyOK(agtmq.NewWaitIOPlanResp(true, errMsg, planTsk.Result))
  45. } else {
  46. if tsk.WaitTimeout(time.Duration(msg.WaitTimeoutMs) * time.Millisecond) {
  47. errMsg := ""
  48. if tsk.Error() != nil {
  49. errMsg = tsk.Error().Error()
  50. }
  51. planTsk := tsk.Body().(*mytask.ExecuteIOPlan)
  52. return mq.ReplyOK(agtmq.NewWaitIOPlanResp(true, errMsg, planTsk.Result))
  53. }
  54. return mq.ReplyOK(agtmq.NewWaitIOPlanResp(false, "", ioswitch.PlanResult{}))
  55. }
  56. }
  57. // CancelIOPlan 取消I/O计划。
  58. // msg: 包含要取消的I/O计划ID的消息体。
  59. // 返回值: 成功时返回响应消息和成功标志,失败时返回错误代码和消息。
  60. func (svc *Service) CancelIOPlan(msg *agtmq.CancelIOPlan) (*agtmq.CancelIOPlanResp, *mq.CodeMessage) {
  61. svc.sw.CancelPlan(msg.PlanID)
  62. return mq.ReplyOK(agtmq.NewCancelIOPlanResp())
  63. }

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