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_hub_state.go 2.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package ticktock
  2. import (
  3. "fmt"
  4. "time"
  5. "gitlink.org.cn/cloudream/common/pkgs/logger"
  6. "gitlink.org.cn/cloudream/common/pkgs/mq"
  7. "gitlink.org.cn/cloudream/common/utils/reflect2"
  8. "gitlink.org.cn/cloudream/jcs-pub/common/consts"
  9. stgglb "gitlink.org.cn/cloudream/jcs-pub/common/globals"
  10. hubmq "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/mq/hub"
  11. cortypes "gitlink.org.cn/cloudream/jcs-pub/coordinator/types"
  12. )
  13. type CheckHubState struct {
  14. }
  15. func (j *CheckHubState) Name() string {
  16. return reflect2.TypeNameOf[CheckHubState]()
  17. }
  18. func (j *CheckHubState) Execute(t *TickTock) {
  19. log := logger.WithType[CheckHubState]("TickTock")
  20. log.Debugf("job start")
  21. startTime := time.Now()
  22. defer func() {
  23. log.Debugf("job end, time: %v", time.Since(startTime))
  24. }()
  25. hubs, err := t.db.Hub().GetAllHubs(t.db.DefCtx())
  26. if err != nil {
  27. log.Warnf("get all hubs: %s", err.Error())
  28. return
  29. }
  30. for _, hub := range hubs {
  31. err := j.checkOne(t, hub)
  32. if err != nil {
  33. log.Warnf("check one hub %v: %s", hub, err.Error())
  34. }
  35. }
  36. }
  37. func (j *CheckHubState) checkOne(t *TickTock, hub cortypes.Hub) error {
  38. log := logger.WithType[CheckHubState]("TickTock")
  39. agtCli, err := stgglb.HubMQPool.Acquire(hub.HubID)
  40. if err != nil {
  41. return fmt.Errorf("new hub mq client: %w", err)
  42. }
  43. defer stgglb.HubMQPool.Release(agtCli)
  44. _, err = agtCli.GetState(hubmq.NewGetState(), mq.RequestOption{Timeout: time.Second * 30})
  45. if err != nil {
  46. if hub.LastReportTime != nil {
  47. if time.Since(*hub.LastReportTime) > t.cfg.HubUnavailableTime {
  48. err := t.db.Hub().UpdateState(t.db.DefCtx(), hub.HubID, consts.HubStateUnavailable)
  49. if err != nil {
  50. log.Warnf("set hub %v state: %s", hub, err.Error())
  51. }
  52. }
  53. } else if hub.LastReportTime == nil {
  54. err := t.db.Hub().UpdateState(t.db.DefCtx(), hub.HubID, consts.HubStateUnavailable)
  55. if err != nil {
  56. log.Warnf("set hub %v state: %s", hub, err.Error())
  57. }
  58. }
  59. return fmt.Errorf("getting state: %w", err)
  60. }
  61. // TODO 如果以后还有其他的状态,要判断哪些状态下能设置Normal
  62. err = t.db.Hub().UpdateState(t.db.DefCtx(), hub.HubID, consts.HubStateNormal)
  63. if err != nil {
  64. log.Warnf("set hub %v state: %s", hub, err.Error())
  65. }
  66. return nil
  67. }

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