github.com/wfusion/gofusion@v1.1.14/cron/types.go (about)

     1  package cron
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"time"
     7  
     8  	"github.com/wfusion/gofusion/common/utils"
     9  	"github.com/wfusion/gofusion/log"
    10  )
    11  
    12  const (
    13  	ErrLockInstanceNotFound     utils.Error = "cron lock instance not found"
    14  	ErrUnsupportedSchedulerType utils.Error = "unsupported cron scheduler type"
    15  
    16  	errDiscardMessage     utils.Error = "discard message"
    17  	tolerantOfTimeNotSync             = 5 * time.Second
    18  )
    19  
    20  var (
    21  	customLoggerType = reflect.TypeOf((*customLogger)(nil)).Elem()
    22  )
    23  
    24  type IRouter interface {
    25  	Use(mws ...routerMiddleware)
    26  	Handle(pattern string, fn any, opts ...utils.OptionExtender)
    27  	Serve() error
    28  	Start() error
    29  	shutdown() error
    30  }
    31  
    32  type Task interface {
    33  	ID() string
    34  	Name() string
    35  	Payload() []byte
    36  	RawMessage() any
    37  }
    38  
    39  // Conf conf
    40  //nolint: revive // struct tag too long issue
    41  type Conf struct {
    42  	Type                 schedulerType        `yaml:"type" json:"type" toml:"type" default:"asynq"`
    43  	Instance             string               `yaml:"instance" json:"instance" toml:"instance"`
    44  	InstanceType         instanceType         `yaml:"instance_type" json:"instance_type" toml:"instance_type"`
    45  	LockInstance         string               `yaml:"lock_instance" json:"lock_instance" toml:"lock_instance"`
    46  	Queue                string               `yaml:"queue" json:"queue" toml:"queue"`
    47  	Server               bool                 `yaml:"server" json:"server" toml:"server" default:"true"`
    48  	Trigger              bool                 `yaml:"trigger" json:"trigger" toml:"trigger" default:"false"`
    49  	ServerConcurrency    int                  `yaml:"server_concurrency" json:"server_concurrency" toml:"server_concurrency"`
    50  	Timezone             string               `yaml:"timezone" json:"timezone" toml:"timezone" default:"Asia/Shanghai"`
    51  	Tasks                map[string]*taskConf `yaml:"tasks" json:"tasks" toml:"tasks"`
    52  	TaskLoader           string               `yaml:"task_loader" json:"task_loader" toml:"task_loader"`
    53  	RefreshTasksInterval string               `yaml:"refresh_tasks_interval" json:"refresh_tasks_interval" toml:"refresh_tasks_interval" default:"3m"`
    54  
    55  	EnableLogger bool   `yaml:"enable_logger" json:"enable_logger" toml:"enable_logger" default:"false"`
    56  	LogLevel     string `yaml:"log_level" json:"log_level" toml:"log_level" default:"info"`
    57  	Logger       string `yaml:"logger" json:"logger" toml:"logger" default:"github.com/wfusion/gofusion/log/customlogger.cronLogger"`
    58  	LogInstance  string `yaml:"log_instance" json:"log_instance" toml:"log_instance" default:"default"`
    59  }
    60  
    61  type schedulerType string
    62  
    63  const (
    64  	schedulerTypeAsynq schedulerType = "asynq"
    65  )
    66  
    67  type instanceType string
    68  
    69  const (
    70  	instanceTypeRedis instanceType = "redis"
    71  	instanceTypeMysql instanceType = "mysql"
    72  )
    73  
    74  type routerHandleFunc func(ctx context.Context, task Task) (err error)
    75  
    76  type routerMiddleware func(next routerHandleFunc) routerHandleFunc
    77  
    78  type taskConf struct {
    79  	Crontab  string `yaml:"crontab" json:"crontab" toml:"crontab"`
    80  	Callback string `yaml:"callback" json:"callback" toml:"callback"`
    81  	Payload  string `yaml:"payload" json:"payload" toml:"payload"`
    82  	Retry    int    `yaml:"retry" json:"retry" toml:"retry"`
    83  	Timeout  string `yaml:"timeout" json:"timeout" toml:"timeout"`
    84  	Deadline string `yaml:"deadline" json:"deadline" toml:"deadline"`
    85  }
    86  
    87  type task struct {
    88  	id, name   string
    89  	payload    []byte
    90  	rawMessage any
    91  }
    92  
    93  func (t *task) ID() string {
    94  	return t.id
    95  }
    96  
    97  func (t *task) Name() string {
    98  	return t.name
    99  }
   100  
   101  func (t *task) Payload() []byte {
   102  	return t.payload
   103  }
   104  
   105  func (t *task) RawMessage() any {
   106  	return t.rawMessage
   107  }
   108  
   109  type customLogger interface {
   110  	Init(log log.Loggable, appName, name string)
   111  }