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

     1  package task
     2  
     3  import (
     4  	"bytes"
     5  	"database/sql/driver"
     6  	"errors"
     7  	"time"
     8  
     9  	json "github.com/json-iterator/go"
    10  
    11  	"gorm.io/gorm"
    12  )
    13  
    14  type State int
    15  
    16  const (
    17  	// StatePending 任务初始状态
    18  	StatePending State = iota
    19  	// StateReceived 收到任务
    20  	StateReceived
    21  	// StateStarted 开始执行任务
    22  	StateStarted
    23  	// StateRetry 准备重试
    24  	StateRetry
    25  	// StateSuccess 任务成功
    26  	StateSuccess
    27  	// StateFailure 任务失败
    28  	StateFailure
    29  )
    30  
    31  func (s State) String() string {
    32  	switch s {
    33  	case StatePending:
    34  		return "PENDING"
    35  	case StateReceived:
    36  		return "RECEIVED"
    37  	case StateStarted:
    38  		return "STARTED"
    39  	case StateRetry:
    40  		return "RETRY"
    41  	case StateSuccess:
    42  		return "SUCCESS"
    43  	case StateFailure:
    44  		return "FAILURE"
    45  	default:
    46  		return "UNKNOWN"
    47  	}
    48  }
    49  
    50  // NewPendingState 创建pending状态
    51  func NewPendingState(task *Signature) *Status {
    52  	return &Status{
    53  		TaskID:   task.ID,
    54  		Name:     task.Name,
    55  		Status:   StatePending,
    56  		GroupID:  task.GroupID,
    57  		CreateAt: time.Now().Local(),
    58  	}
    59  }
    60  
    61  // NewReceivedState 创建Received状态
    62  func NewReceivedState(task *Signature) *Status {
    63  	return &Status{
    64  		TaskID:  task.ID,
    65  		Name:    task.Name,
    66  		Status:  StateReceived,
    67  		GroupID: task.GroupID,
    68  	}
    69  }
    70  
    71  // NewStartedState 创建Started状态
    72  func NewStartedState(task *Signature) *Status {
    73  	return &Status{
    74  		TaskID:  task.ID,
    75  		Name:    task.Name,
    76  		Status:  StateStarted,
    77  		GroupID: task.GroupID,
    78  	}
    79  }
    80  
    81  // NewRetryState 创建Retry状态
    82  func NewRetryState(task *Signature) *Status {
    83  	return &Status{
    84  		TaskID:  task.ID,
    85  		Name:    task.Name,
    86  		Status:  StateRetry,
    87  		GroupID: task.GroupID,
    88  	}
    89  }
    90  
    91  // NewSuccessState 创建Success状态
    92  func NewSuccessState(task *Signature, results ...*Result) *Status {
    93  	return &Status{
    94  		TaskID:  task.ID,
    95  		Name:    task.Name,
    96  		Status:  StateSuccess,
    97  		Results: results,
    98  		GroupID: task.GroupID,
    99  	}
   100  }
   101  
   102  // NewFailureState 创建Failure状态
   103  func NewFailureState(task *Signature, err string) *Status {
   104  	return &Status{
   105  		TaskID:  task.ID,
   106  		Name:    task.Name,
   107  		Status:  StateFailure,
   108  		Error:   err,
   109  		GroupID: task.GroupID,
   110  	}
   111  }
   112  
   113  type Results []*Result
   114  
   115  func (s *Results) Scan(src interface{}) error {
   116  	str, ok := src.([]byte)
   117  	if !ok {
   118  		return errors.New("failed to scan Results field - source is not a string")
   119  	}
   120  	decoder := json.NewDecoder(bytes.NewReader(str))
   121  	decoder.UseNumber()
   122  	return decoder.Decode(s)
   123  }
   124  
   125  func (s Results) Value() (driver.Value, error) {
   126  	if s == nil || len(s) == 0 {
   127  		return nil, nil
   128  	}
   129  	return json.Marshal(s)
   130  }
   131  
   132  // Status 任务状态
   133  type Status struct {
   134  	ID        uint           `json:"-" bson:"-" gorm:"column:_id;primarykey;comment:_id"`
   135  	TaskID    string         `json:"task_id" bson:"_id" gorm:"column:id;index;comment:id"`
   136  	GroupID   string         `json:"group_id" bson:"group_id" gorm:"column:group_id;comment:组唯一标识"`
   137  	Name      string         `json:"name" bson:"name" gorm:"column:name;comment:组名称"`
   138  	Status    State          `json:"status" bson:"status" gorm:"column:status;comment:任务状态"`
   139  	TTL       int64          `json:"ttl" bson:"ttl" gorm:"column:ttl;comment:过期时间"`
   140  	Error     string         `json:"error" bson:"error" gorm:"column:error;comment:错误"`
   141  	Results   Results        `json:"results" bson:"results" gorm:"column:results;comment:结果;type:text"`
   142  	CreateAt  time.Time      `json:"create_at" bson:"create_at" gorm:"column:create_at;comment:创建时间"`
   143  	DeletedAt gorm.DeletedAt `json:"-" bson:"-" gorm:"index"`
   144  }
   145  
   146  func (t *Status) IsCompleted() bool {
   147  	return t.IsSuccess() || t.IsFailure()
   148  }
   149  
   150  func (t *Status) IsSuccess() bool {
   151  	return t.Status == StateSuccess
   152  }
   153  
   154  func (t *Status) IsFailure() bool {
   155  	return t.Status == StateFailure
   156  }