github.com/songzhibin97/gkit@v1.2.13/distributed/task/group.go (about)

     1  package task
     2  
     3  import (
     4  	"database/sql/driver"
     5  	"errors"
     6  	"strings"
     7  	"time"
     8  
     9  	"gorm.io/gorm"
    10  )
    11  
    12  type StringSlice []string
    13  
    14  func (s *StringSlice) Scan(src interface{}) error {
    15  	str, ok := src.([]byte)
    16  	if !ok {
    17  		return errors.New("failed to scan StringSlice field - source is not a string")
    18  	}
    19  	*s = strings.Split(string(str), ",")
    20  	return nil
    21  }
    22  
    23  func (s StringSlice) Value() (driver.Value, error) {
    24  	if s == nil || len(s) == 0 {
    25  		return nil, nil
    26  	}
    27  	return strings.Join(s, ","), nil
    28  }
    29  
    30  // GroupMeta 组详情
    31  type GroupMeta struct {
    32  	ID uint `json:"-" bson:"-" gorm:"column:_id;primarykey;comment:_id"`
    33  	// GroupID 组的唯一标识
    34  	GroupID string `json:"group_id" bson:"_id" gorm:"column:id;index;comment:id"`
    35  	// 组名称
    36  	Name string `json:"name" bson:"name" gorm:"column:name;comment:组名称"`
    37  	// TaskIDs 接管的任务id
    38  	TaskIDs StringSlice `json:"task_ids" bson:"task_ids" gorm:"column:task_ids;comment:接管的任务id;type:text"`
    39  	// TriggerCompleted 是否触发完成
    40  	TriggerCompleted bool `json:"trigger_chord" bson:"trigger_chord" gorm:"column:trigger_chord;comment:是否触发完成"`
    41  	// Lock 是否锁定
    42  	Lock bool `json:"lock" gorm:"column:lock;comment:锁"`
    43  	// TTL 有效时间
    44  	TTL int64 `json:"ttl,omitempty" bson:"ttl,omitempty" gorm:"column:ttl;comment:过期时间"`
    45  	// CreateAt 创建时间
    46  	CreateAt  time.Time      `json:"create_at" bson:"create_at" gorm:"column:create_at;comment:创建时间"`
    47  	DeletedAt gorm.DeletedAt `json:"-" bson:"-" gorm:"index"`
    48  }
    49  
    50  func InitGroupMeta(groupID string, name string, ttl int64, taskIDs ...string) *GroupMeta {
    51  	return &GroupMeta{
    52  		GroupID:  groupID,
    53  		Name:     name,
    54  		TaskIDs:  taskIDs,
    55  		CreateAt: time.Now().Local(),
    56  		TTL:      ttl,
    57  	}
    58  }