github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/cluster/calcium/status.go (about) 1 package calcium 2 3 import ( 4 "context" 5 6 "github.com/projecteru2/core/log" 7 "github.com/projecteru2/core/types" 8 "github.com/projecteru2/core/utils" 9 ) 10 11 // GetNodeStatus set status of a node 12 // it's used to report whether a node is still alive 13 func (c *Calcium) GetNodeStatus(ctx context.Context, nodename string) (*types.NodeStatus, error) { 14 return c.store.GetNodeStatus(ctx, nodename) 15 } 16 17 // SetNodeStatus set status of a node 18 // it's used to report whether a node is still alive 19 func (c *Calcium) SetNodeStatus(ctx context.Context, nodename string, ttl int64) error { 20 logger := log.WithFunc("calcium.SetNodeStatus").WithField("node", nodename).WithField("ttl", ttl) 21 node, err := c.store.GetNode(ctx, nodename) 22 if err != nil { 23 logger.Error(ctx, err) 24 return err 25 } 26 err = c.store.SetNodeStatus(ctx, node, ttl) 27 logger.Error(ctx, err) 28 return err 29 } 30 31 // NodeStatusStream returns a stream of node status for subscribing 32 func (c *Calcium) NodeStatusStream(ctx context.Context) chan *types.NodeStatus { 33 return c.store.NodeStatusStream(ctx) 34 } 35 36 // GetWorkloadsStatus get workload status 37 func (c *Calcium) GetWorkloadsStatus(ctx context.Context, IDs []string) ([]*types.StatusMeta, error) { 38 r := []*types.StatusMeta{} 39 for _, ID := range IDs { 40 s, err := c.store.GetWorkloadStatus(ctx, ID) 41 if err != nil { 42 log.WithFunc("calcium.GetWorkloadStatus").WithField("IDs", IDs).Error(ctx, err) 43 return r, err 44 } 45 r = append(r, s) 46 } 47 return r, nil 48 } 49 50 // SetWorkloadsStatus set workloads status 51 func (c *Calcium) SetWorkloadsStatus(ctx context.Context, statusMetas []*types.StatusMeta, ttls map[string]int64) ([]*types.StatusMeta, error) { 52 logger := log.WithFunc("calcium.SetWorkloadsStatus").WithField("status", statusMetas[0]).WithField("ttls", ttls) 53 r := []*types.StatusMeta{} 54 for _, statusMeta := range statusMetas { 55 // In order to compat 56 if statusMeta.Appname == "" || statusMeta.Nodename == "" || statusMeta.Entrypoint == "" { 57 workload, err := c.store.GetWorkload(ctx, statusMeta.ID) 58 if err != nil { 59 logger.Error(ctx, err) 60 return nil, err 61 } 62 63 appname, entrypoint, _, err := utils.ParseWorkloadName(workload.Name) 64 if err != nil { 65 logger.Error(ctx, err) 66 return nil, err 67 } 68 69 statusMeta.Appname = appname 70 statusMeta.Nodename = workload.Nodename 71 statusMeta.Entrypoint = entrypoint 72 } 73 74 ttl, ok := ttls[statusMeta.ID] 75 if !ok { 76 ttl = 0 77 } 78 79 if err := c.store.SetWorkloadStatus(ctx, statusMeta, ttl); err != nil { 80 logger.Error(ctx, err) 81 return nil, err 82 } 83 r = append(r, statusMeta) 84 } 85 return r, nil 86 } 87 88 // WorkloadStatusStream stream workload status 89 func (c *Calcium) WorkloadStatusStream(ctx context.Context, appname, entrypoint, nodename string, labels map[string]string) chan *types.WorkloadStatus { 90 return c.store.WorkloadStatusStream(ctx, appname, entrypoint, nodename, labels) 91 }