gitee.com/h79/goutils@v1.22.10/common/scheduler/option.go (about)

     1  package scheduler
     2  
     3  import (
     4  	"gitee.com/h79/goutils/common/option"
     5  	"time"
     6  )
     7  
     8  const (
     9  	DurationOpt = iota + 1
    10  	GroupNumOpt
    11  	CapacityOpt
    12  	LoggEnableOpt
    13  	StopOpt
    14  	QuitedOpt
    15  	TimeLocationOpt
    16  	UniqueOpt           //唯一检测添加,存在就不添加了
    17  	GenerateOrInitFnOpt //更新或初始化函数,在更新时,有 NotExistNewOpt 时生成函数
    18  	NotExistNewOpt      //更新时,不存在就生成
    19  	MinDurationOpt
    20  )
    21  
    22  type GenerateOrInitFunc func(group int, tk Task) (Task, Trigger, bool)
    23  
    24  func WithUnique() option.Option {
    25  	return option.WithOpt(true, "unique", UniqueOpt)
    26  }
    27  func WithGenerateOrInitFunc(fn GenerateOrInitFunc) option.Option {
    28  	return option.WithOpt[GenerateOrInitFunc](fn, "generate func", GenerateOrInitFnOpt)
    29  }
    30  
    31  func WithNotExistNew() option.Option {
    32  	return option.WithOpt(true, "not exist to new", NotExistNewOpt)
    33  }
    34  func WithTaskMinDuration(t time.Duration) option.Option {
    35  	return option.WithOpt(t, "task min duration", MinDurationOpt)
    36  }
    37  func WithTimeLocation(lc *time.Location) option.Option {
    38  	return option.WithOpt(lc, "time location", TimeLocationOpt)
    39  }
    40  func WithTaskDuration(t time.Duration) option.Option {
    41  	return option.WithOpt(t, "task duration", DurationOpt)
    42  }
    43  func WithGroupNum(num int) option.Option {
    44  	return option.WithOpt(num, "group number", GroupNumOpt)
    45  }
    46  func WithCapacity(capacity int) option.Option {
    47  	return option.WithOpt(capacity, "logger enable", CapacityOpt)
    48  }
    49  func WithLoggerEnabled() option.Option {
    50  	return option.WithOpt(true, "", LoggEnableOpt)
    51  }
    52  func WithStop() option.Option {
    53  	return option.WithOpt(true, "", StopOpt)
    54  }
    55  func IsStop(opts ...option.Option) bool {
    56  	return option.Select(StopOpt, false, opts...)
    57  }
    58  func WithQuited() option.Option {
    59  	return option.WithOpt(true, "", QuitedOpt)
    60  }
    61  func IsQuited(opts ...option.Option) bool {
    62  	return option.Select(QuitedOpt, false, opts...)
    63  }
    64  func With(s int) []option.Option {
    65  	var opts []option.Option
    66  	if s == 1 {
    67  		opts = append(opts, WithStop())
    68  	} else if s == 2 {
    69  		opts = append(opts, WithQuited())
    70  	}
    71  	return opts
    72  }