flow_resource.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package service
  2. import (
  3. "context"
  4. "github.com/jinzhu/gorm"
  5. "go-common/app/admin/main/aegis/model/net"
  6. "go-common/library/log"
  7. )
  8. func (s *Service) addFlowResources(tx *gorm.DB, netID int64, rids []int64, flowID int64, state int8) (err error) {
  9. fr := &net.FlowResource{
  10. FlowID: flowID,
  11. State: state,
  12. NetID: netID,
  13. }
  14. for _, rid := range rids {
  15. fr.ID = 0
  16. fr.RID = rid
  17. if err = s.gorm.AddItem(context.TODO(), tx, fr); err != nil {
  18. log.Error("addFlowResources s.gorm.AddItem error(%v) fr(%+v)", err, fr)
  19. return
  20. }
  21. }
  22. return
  23. }
  24. /**
  25. * updateFlowResources 正常流转到新节点
  26. * 指定网 & 指定资源 & 新节点
  27. * 不需要指定现状flowid,不同资源可能不在同一现状节点上
  28. * 已被取消运行的资源现状,不能被更新
  29. */
  30. func (s *Service) updateFlowResources(c context.Context, tx *gorm.DB, netID int64, rid int64, newFlowID int64) (err error) {
  31. var (
  32. frs []*net.FlowResource
  33. )
  34. //资源的运行现状
  35. if frs, err = s.gorm.FRByNetRID(c, []int64{netID}, []int64{rid}, false); err != nil {
  36. log.Error("updateFlowResources s.gorm.FRByNetRID error(%v)", err)
  37. return
  38. }
  39. if len(frs) == 0 {
  40. if err = s.addFlowResources(tx, netID, []int64{rid}, newFlowID, net.FRStateRunning); err != nil {
  41. log.Error("updateFlowResources s.addFlowResources error(%v)", err)
  42. }
  43. return
  44. }
  45. //确定state & 记录数目:注意单线->并发和并发->单线的转折点
  46. //todo--只有新节点,如何确定要不要并发拆分或并发合并?--并发要怎么存储(在配置时候确认并发拆分点、并发分支、并发结合点)?
  47. //todo--需要上游transitionid确认上游dir.order吗?能出现transition->flow的多对多的情况吗(不能)?
  48. //todo--单独通过flow去确定是在单线上还是并发线上?
  49. for _, item := range frs {
  50. if err = s.gorm.UpdateFields(context.TODO(), tx, net.TableFlowResource, item.ID,
  51. map[string]interface{}{"flow_id": newFlowID, "state": net.FRStateRunning}); err != nil {
  52. log.Error("updateFlowResources s.gorm.UpdateFields error(%v)", err)
  53. return
  54. }
  55. }
  56. return
  57. }