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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package ticktock
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "gitlink.org.cn/cloudream/common/pkgs/logger"
  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. hubrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/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. addr, ok := hub.Address.(*cortypes.GRPCAddressInfo)
  40. if !ok {
  41. return fmt.Errorf("hub has no grpc address")
  42. }
  43. agtCli := stgglb.HubRPCPool.Get(stgglb.SelectGRPCAddress(hub, *addr))
  44. defer agtCli.Release()
  45. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  46. defer cancel()
  47. _, cerr := agtCli.GetState(ctx, &hubrpc.GetState{})
  48. if cerr != nil {
  49. if hub.LastReportTime != nil {
  50. if time.Since(*hub.LastReportTime) > t.cfg.HubUnavailableTime {
  51. err := t.db.Hub().UpdateState(t.db.DefCtx(), hub.HubID, consts.HubStateUnavailable)
  52. if err != nil {
  53. log.Warnf("set hub %v state: %s", hub, err.Error())
  54. }
  55. }
  56. } else if hub.LastReportTime == nil {
  57. err := t.db.Hub().UpdateState(t.db.DefCtx(), hub.HubID, consts.HubStateUnavailable)
  58. if err != nil {
  59. log.Warnf("set hub %v state: %s", hub, err.Error())
  60. }
  61. }
  62. return fmt.Errorf("getting state: %w", cerr.ToError())
  63. }
  64. // TODO 如果以后还有其他的状态,要判断哪些状态下能设置Normal
  65. err := t.db.Hub().UpdateState(t.db.DefCtx(), hub.HubID, consts.HubStateNormal)
  66. if err != nil {
  67. log.Warnf("set hub %v state: %s", hub, err.Error())
  68. }
  69. return nil
  70. }

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