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.

agent_check_state.go 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package event
  2. import (
  3. "database/sql"
  4. "time"
  5. "github.com/samber/lo"
  6. "gitlink.org.cn/cloudream/common/consts"
  7. "gitlink.org.cn/cloudream/common/pkg/logger"
  8. "gitlink.org.cn/cloudream/db/model"
  9. mysql "gitlink.org.cn/cloudream/db/sql"
  10. agtcli "gitlink.org.cn/cloudream/rabbitmq/client/agent"
  11. agtevt "gitlink.org.cn/cloudream/rabbitmq/message/agent/event"
  12. scevt "gitlink.org.cn/cloudream/rabbitmq/message/scanner/event"
  13. "gitlink.org.cn/cloudream/scanner/internal/config"
  14. )
  15. type AgentCheckState struct {
  16. NodeID int
  17. }
  18. func NewAgentCheckState(nodeID int) *AgentCheckState {
  19. return &AgentCheckState{
  20. NodeID: nodeID,
  21. }
  22. }
  23. func (t *AgentCheckState) TryMerge(other Event) bool {
  24. event, ok := other.(*AgentCheckState)
  25. if !ok {
  26. return false
  27. }
  28. return t.NodeID == event.NodeID
  29. }
  30. func (t *AgentCheckState) Execute(execCtx ExecuteContext) {
  31. log := logger.WithType[AgentCheckState]("Event")
  32. log.Debugf("begin with %v", logger.FormatStruct(t))
  33. node, err := mysql.Node.GetByID(execCtx.Args.DB.SQLCtx(), t.NodeID)
  34. if err == sql.ErrNoRows {
  35. return
  36. }
  37. if err != nil {
  38. log.WithField("NodeID", t.NodeID).Warnf("get node by id failed, err: %s", err.Error())
  39. return
  40. }
  41. if node.State != consts.NODE_STATE_NORMAL {
  42. return
  43. }
  44. // 检查上次上报时间,超时的设置为不可用
  45. // TODO 没有上报过是否要特殊处理?
  46. if node.LastReportTime == nil && time.Since(*node.LastReportTime) > time.Duration(config.Cfg().NodeUnavailableSeconds)*time.Second {
  47. err := mysql.Node.ChangeState(execCtx.Args.DB.SQLCtx(), t.NodeID, consts.NODE_STATE_UNAVAILABLE)
  48. if err != nil {
  49. log.WithField("NodeID", t.NodeID).Warnf("set node state failed, err: %s", err.Error())
  50. return
  51. }
  52. caches, err := execCtx.Args.DB.Cache().GetNodeCaches(execCtx.Args.DB.SQLCtx(), t.NodeID)
  53. if err != nil {
  54. log.WithField("NodeID", t.NodeID).Warnf("get node caches failed, err: %s", err.Error())
  55. return
  56. }
  57. // 补充备份数
  58. execCtx.Executor.Post(NewCheckRepCount(lo.Map(caches, func(ch model.Cache, index int) string { return ch.FileHash })))
  59. return
  60. }
  61. agentClient, err := agtcli.NewAgentClient(t.NodeID, &config.Cfg().RabbitMQ)
  62. if err != nil {
  63. log.WithField("NodeID", t.NodeID).Warnf("create agent client failed, err: %s", err.Error())
  64. return
  65. }
  66. defer agentClient.Close()
  67. // 紧急任务
  68. err = agentClient.PostEvent(agtevt.NewCheckState(), true, true)
  69. if err != nil {
  70. log.WithField("NodeID", t.NodeID).Warnf("request to agent failed, err: %s", err.Error())
  71. }
  72. }
  73. func init() {
  74. RegisterMessageConvertor(func(msg scevt.AgentCheckState) Event { return NewAgentCheckState(msg.NodeID) })
  75. }

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