github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/mq/task.go (about)

     1  package mq
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/google/uuid"
     7  )
     8  
     9  type Task interface {
    10  	Subject() string
    11  	ID() string
    12  	State() TaskState
    13  	SetState(TaskState)
    14  }
    15  
    16  type TaskUUID string
    17  
    18  func (tid *TaskUUID) ID() string {
    19  	if *tid == "" {
    20  		*tid = TaskUUID(uuid.New().String())
    21  	}
    22  	return string(*tid)
    23  }
    24  
    25  func (tid *TaskUUID) SetID(id string) { *tid = TaskUUID(id) }
    26  
    27  type WithArg interface {
    28  	Arg() interface{}
    29  }
    30  
    31  type SetArg interface {
    32  	SetArg(v interface{}) error
    33  }
    34  
    35  type TaskHeader struct {
    36  	TaskUUID
    37  	TaskState
    38  	subject string
    39  }
    40  
    41  var _ Task = (*TaskHeader)(nil)
    42  
    43  func (th *TaskHeader) Subject() string { return th.subject }
    44  
    45  func (th *TaskHeader) SetSubject(s string) { th.subject = s }
    46  
    47  func NewTaskBoard(tm TaskManager) *TaskBoard { return &TaskBoard{tm} }
    48  
    49  type TaskBoard struct {
    50  	tm TaskManager
    51  }
    52  
    53  func (b *TaskBoard) Dispatch(ch string, t Task) error {
    54  	if t == nil {
    55  		return nil
    56  	}
    57  	return b.tm.Push(ch, t)
    58  }
    59  
    60  type TaskOperator interface {
    61  	Output(ctx context.Context) (interface{}, error)
    62  	SetArg
    63  }