|
- package scheduler
-
- import (
- "encoding/json"
- "github.com/pkg/errors"
- "github.com/zeromicro/go-zero/core/logx"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types"
- "gorm.io/gorm"
- )
-
- type scheduler struct {
- task *types.TaskInfo
- participantIds []int64
- scheduleService scheduleService
- }
-
- func NewScheduler(task *types.TaskInfo, val string) (*scheduler, error) {
- err := json.Unmarshal([]byte(val), &task)
- if err != nil {
- return nil, errors.New("create scheduler failed : " + err.Error())
- }
- return &scheduler{task: task}, nil
- }
-
- func (s scheduler) matchLabels(dbEngin *gorm.DB, task *types.TaskInfo) {
- var ids []int64
- count := 0
- for key := range task.MatchLabels {
- var participantId []int64
- dbEngin.Raw("select participant_id from sc_participant_label_info where `key` = ? and value = ?", key, task.MatchLabels[key]).Scan(&participantId)
- if count == 0 {
- ids = participantId
- }
- //if len(participantId) == 0 || len(ids) == 0 {
- // return nil, nil
- //}
- ids = intersect(ids, participantId)
- count++
- }
- s.participantIds = micsSlice(ids, 1)
- }
-
- func (s scheduler) saveToDb(dbEngin *gorm.DB) error {
- if len(s.participantIds) == 0 {
- return errors.New("participantIds 为空")
- }
- structForDb, err := s.scheduleService().getNewStructForDb(s.task, s.participantIds)
- if err != nil {
- return err
- }
- tx := dbEngin.Create(&structForDb)
- if tx.Error != nil {
- logx.Error(tx.Error)
- return tx.Error
- }
- return nil
- }
|