github.com/wfusion/gofusion@v1.1.14/async/types.go (about) 1 package async 2 3 import ( 4 "context" 5 "reflect" 6 "sync" 7 "time" 8 9 "github.com/wfusion/gofusion/common/utils" 10 "github.com/wfusion/gofusion/log" 11 ) 12 13 const ( 14 ErrDuplicatedHandlerName utils.Error = "duplicated async handler name" 15 ErrDuplicatedInstanceName utils.Error = "duplicated async instance name" 16 ErrDuplicatedQueueName utils.Error = "duplicated async queue name" 17 ErrConsumerDisabled utils.Error = "async consumer is disabled" 18 ErrUnsupportedSchedulerType utils.Error = "unsupported async type" 19 ) 20 21 var ( 22 // callbackMap taskName:callback function 23 funcNameToTaskName = map[string]map[string]string{} 24 callbackMap = map[string]map[string]any{} 25 callbackMapLock = sync.RWMutex{} 26 27 customLoggerType = reflect.TypeOf((*customLogger)(nil)).Elem() 28 ) 29 30 type Producable interface { 31 Go(fn any, opts ...utils.OptionExtender) error 32 Goc(ctx context.Context, fn any, opts ...utils.OptionExtender) error 33 Send(ctx context.Context, taskName string, data any, opts ...utils.OptionExtender) (err error) 34 } 35 36 type Consumable interface { 37 Use(mws ...routerMiddleware) 38 Handle(pattern string, fn any, opts ...utils.OptionExtender) 39 HandleFunc(fn any, opts ...utils.OptionExtender) 40 Serve() error 41 Start() error 42 shutdown() error 43 } 44 45 type Task interface { 46 ID() string 47 Name() string 48 Payload() []byte 49 RawMessage() any 50 } 51 52 // Conf async conf 53 //nolint: revive // struct tag too long issue 54 type Conf struct { 55 Type asyncType `yaml:"type" json:"type" toml:"type"` 56 Instance string `yaml:"instance" json:"instance" toml:"instance"` 57 InstanceType instanceType `yaml:"instance_type" json:"instance_type" toml:"instance_type"` 58 Producer bool `yaml:"producer" json:"producer" toml:"producer" default:"true"` 59 Consumer bool `yaml:"consumer" json:"consumer" toml:"consumer" default:"false"` 60 ConsumerConcurrency int `yaml:"consumer_concurrency" json:"consumer_concurrency" toml:"consumer_concurrency"` 61 MessageSerializeType string `yaml:"message_serialize_type" json:"message_serialize_type" toml:"message_serialize_type" default:"gob"` 62 MessageCompressType string `yaml:"message_compress_type" json:"message_compress_type" toml:"message_compress_type"` 63 Queues []*queueConf `yaml:"queues" json:"queues" toml:"queues"` 64 StrictPriority bool `yaml:"strict_priority" json:"strict_priority" toml:"strict_priority"` 65 66 EnableLogger bool `yaml:"enable_logger" json:"enable_logger" toml:"enable_logger" default:"false"` 67 LogLevel string `yaml:"log_level" json:"log_level" toml:"log_level" default:"info"` 68 Logger string `yaml:"logger" json:"logger" toml:"logger" default:"github.com/wfusion/gofusion/log/customlogger.asyncLogger"` 69 LogInstance string `yaml:"log_instance" json:"log_instance" toml:"log_instance" default:"default"` 70 } 71 72 type queueConf struct { 73 Name string `yaml:"name" json:"name" toml:"name"` 74 Level int `yaml:"level" json:"level" toml:"level"` 75 } 76 77 type instanceType string 78 79 const ( 80 instanceTypeRedis instanceType = "redis" 81 instanceTypeDB instanceType = "db" 82 ) 83 84 type task struct { 85 id, name string 86 payload []byte 87 rawMessage any 88 } 89 90 func (t *task) ID() string { 91 return t.id 92 } 93 94 func (t *task) Name() string { 95 return t.name 96 } 97 98 func (t *task) Payload() []byte { 99 return t.payload 100 } 101 102 func (t *task) RawMessage() any { 103 return t.rawMessage 104 } 105 106 type produceOption struct { 107 id string 108 args []any 109 queue string 110 maxRetry int 111 deadline time.Time 112 timeout time.Duration 113 delayDuration time.Duration 114 delayTime time.Time 115 retentionDuration time.Duration 116 } 117 118 func TaskID(id string) utils.OptionFunc[produceOption] { 119 return func(o *produceOption) { o.id = id } 120 } 121 func Args(args ...any) utils.OptionFunc[produceOption] { 122 return func(o *produceOption) { o.args = append(o.args, args...) } 123 } 124 func Queue(queue string) utils.OptionFunc[produceOption] { 125 return func(o *produceOption) { o.queue = queue } 126 } 127 func MaxRetry(n int) utils.OptionFunc[produceOption] { 128 return func(o *produceOption) { o.maxRetry = n } 129 } 130 func Deadline(t time.Time) utils.OptionFunc[produceOption] { 131 return func(o *produceOption) { o.deadline = t } 132 } 133 func Timeout(d time.Duration) utils.OptionFunc[produceOption] { 134 return func(o *produceOption) { o.timeout = d } 135 } 136 func Delay(d time.Duration) utils.OptionFunc[produceOption] { 137 return func(o *produceOption) { o.delayDuration = d } 138 } 139 func DelayAt(t time.Time) utils.OptionFunc[produceOption] { 140 return func(o *produceOption) { o.delayTime = t } 141 } 142 func Retention(d time.Duration) utils.OptionFunc[produceOption] { 143 return func(o *produceOption) { o.retentionDuration = d } 144 } 145 146 type asyncType string 147 148 const ( 149 asyncTypeAsynq asyncType = "asynq" 150 asyncTypeMysql asyncType = "mysql" 151 ) 152 153 type routerMiddlewareFunc func(ctx context.Context, task Task) (err error) 154 155 type routerMiddleware func(next routerMiddlewareFunc) routerMiddlewareFunc 156 157 type customLogger interface { 158 Init(log log.Loggable, appName, name string) 159 }