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.

registerparticipantlogic.go 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright (c) [2023] [pcm]
  3. [pcm-coordinator] is licensed under Mulan PSL v2.
  4. You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. You may obtain a copy of Mulan PSL v2 at:
  6. http://license.coscl.org.cn/MulanPSL2
  7. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. See the Mulan PSL v2 for more details.
  11. */
  12. package participantservicelogic
  13. import (
  14. "context"
  15. "github.com/pkg/errors"
  16. models2 "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/models"
  17. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils"
  18. "gorm.io/gorm"
  19. "time"
  20. "gitlink.org.cn/jcce-pcm/pcm-coordinator/rpc/internal/svc"
  21. "gitlink.org.cn/jcce-pcm/pcm-coordinator/rpc/pcmCore"
  22. "github.com/zeromicro/go-zero/core/logx"
  23. )
  24. type RegisterParticipantLogic struct {
  25. ctx context.Context
  26. svcCtx *svc.ServiceContext
  27. logx.Logger
  28. }
  29. func NewRegisterParticipantLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterParticipantLogic {
  30. return &RegisterParticipantLogic{
  31. ctx: ctx,
  32. svcCtx: svcCtx,
  33. Logger: logx.WithContext(ctx),
  34. }
  35. }
  36. // RegisterParticipant Participant注册接口
  37. func (l *RegisterParticipantLogic) RegisterParticipant(in *pcmCore.ParticipantPhyReq) (*pcmCore.ParticipantPhyResp, error) {
  38. //判断ParticipantId是否存在
  39. db := l.svcCtx.DbEngin.Begin()
  40. // 执行回滚或者提交操作
  41. defer func() {
  42. if p := recover(); p != nil {
  43. db.Rollback()
  44. logx.Error(p)
  45. } else if db.Error != nil {
  46. logx.Info("rollback")
  47. db.Rollback()
  48. } else {
  49. db = db.Commit()
  50. logx.Info("commit success")
  51. }
  52. }()
  53. participantInfo := &models2.ScParticipantPhyInfo{}
  54. utils.Convert(in, participantInfo)
  55. if in.ParticipantId == 0 {
  56. participantInfo.Id = utils.GenSnowflakeID()
  57. } else {
  58. participantInfo.Id = in.ParticipantId
  59. }
  60. //保存participant静态信息
  61. result := db.Save(&participantInfo)
  62. //保存节点信息
  63. nodeList := make([]*models2.ScNodePhyInfo, 0)
  64. for _, info := range in.NodeInfo {
  65. nodeInfo := &models2.ScNodePhyInfo{}
  66. utils.Convert(info, nodeInfo)
  67. nodeInfo.CreatedTime = time.Now()
  68. nodeInfo.ParticipantId = participantInfo.Id
  69. //查询节点name与ParticipantId是否存在
  70. nodeErr := db.Where(&models2.ScNodePhyInfo{NodeName: nodeInfo.NodeName, ParticipantId: in.ParticipantId}).Take(nodeInfo)
  71. if errors.Is(nodeErr.Error, gorm.ErrRecordNotFound) {
  72. nodeInfo.Id = utils.GenSnowflakeID()
  73. }
  74. nodeList = append(nodeList, nodeInfo)
  75. }
  76. result = db.Save(&nodeList)
  77. //保存队列信息
  78. queueList := make([]*models2.ScQueuePhyInfo, 0)
  79. for _, info := range in.QueueInfo {
  80. queueInfo := &models2.ScQueuePhyInfo{}
  81. utils.Convert(info, queueInfo)
  82. queueInfo.ParticipantId = participantInfo.Id
  83. //查询队列name与ParticipantId是否存在
  84. queueErr := db.Where(&models2.ScQueuePhyInfo{QueueName: queueInfo.QueueName, ParticipantId: in.ParticipantId}).Take(queueInfo)
  85. if errors.Is(queueErr.Error, gorm.ErrRecordNotFound) {
  86. queueInfo.Id = utils.GenSnowflakeID()
  87. }
  88. queueList = append(queueList, queueInfo)
  89. }
  90. result = db.Save(&queueList)
  91. //保存标签信息
  92. labelList := make([]*models2.ScParticipantLabelInfo, 0)
  93. for _, label := range in.LabelInfo {
  94. labelInfo := &models2.ScParticipantLabelInfo{}
  95. utils.Convert(label, labelInfo)
  96. labelInfo.CreatedTime = time.Now()
  97. labelInfo.ParticipantId = participantInfo.Id
  98. //查询标签key value与ParticipantId是否存在
  99. labelErr := db.Where(&models2.ScParticipantLabelInfo{Key: labelInfo.Key, Value: labelInfo.Value, ParticipantId: in.ParticipantId}).Take(labelInfo)
  100. if errors.Is(labelErr.Error, gorm.ErrRecordNotFound) {
  101. labelInfo.Id = utils.GenSnowflakeID()
  102. }
  103. labelList = append(labelList, labelInfo)
  104. }
  105. result = db.Save(&labelList)
  106. if result.Error != nil {
  107. logx.Errorf("orm err:", result.Error)
  108. return &pcmCore.ParticipantPhyResp{}, nil
  109. }
  110. return &pcmCore.ParticipantPhyResp{
  111. Code: 200,
  112. Msg: "ok",
  113. ParticipantId: participantInfo.Id,
  114. }, nil
  115. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.