优化传参
This commit is contained in:
+7
-7
@@ -95,7 +95,7 @@ func InitCluster(ctx context.Context, red redis.UniversalClient, keyPrefix strin
|
|||||||
// @param callback 回调函数
|
// @param callback 回调函数
|
||||||
// @param extendData 扩展数据
|
// @param extendData 扩展数据
|
||||||
// @return error
|
// @return error
|
||||||
func (c *Cluster) EveryMonth(ctx context.Context, taskId string, day int, hour int, minute int, second int, callback callback, extendData interface{}) error {
|
func (c *Cluster) EveryMonth(ctx context.Context, taskId string, day int, hour int, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
||||||
nowTime := time.Now().In(c.location)
|
nowTime := time.Now().In(c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
@@ -117,7 +117,7 @@ func (c *Cluster) EveryMonth(ctx context.Context, taskId string, day int, hour i
|
|||||||
// @param hour int 小时
|
// @param hour int 小时
|
||||||
// @param minute int 分钟
|
// @param minute int 分钟
|
||||||
// @param second int 秒
|
// @param second int 秒
|
||||||
func (c *Cluster) EveryWeek(ctx context.Context, taskId string, week time.Weekday, hour int, minute int, second int, callback callback, extendData interface{}) error {
|
func (c *Cluster) EveryWeek(ctx context.Context, taskId string, week time.Weekday, hour int, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
||||||
nowTime := time.Now().In(c.location)
|
nowTime := time.Now().In(c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
@@ -133,7 +133,7 @@ func (c *Cluster) EveryWeek(ctx context.Context, taskId string, week time.Weekda
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 每天执行一次
|
// 每天执行一次
|
||||||
func (c *Cluster) EveryDay(ctx context.Context, taskId string, hour int, minute int, second int, callback callback, extendData interface{}) error {
|
func (c *Cluster) EveryDay(ctx context.Context, taskId string, hour int, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
||||||
nowTime := time.Now().In(c.location)
|
nowTime := time.Now().In(c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
@@ -148,7 +148,7 @@ func (c *Cluster) EveryDay(ctx context.Context, taskId string, hour int, minute
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 每小时执行一次
|
// 每小时执行一次
|
||||||
func (c *Cluster) EveryHour(ctx context.Context, taskId string, minute int, second int, callback callback, extendData interface{}) error {
|
func (c *Cluster) EveryHour(ctx context.Context, taskId string, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
||||||
nowTime := time.Now().In(c.location)
|
nowTime := time.Now().In(c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
@@ -162,7 +162,7 @@ func (c *Cluster) EveryHour(ctx context.Context, taskId string, minute int, seco
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 每分钟执行一次
|
// 每分钟执行一次
|
||||||
func (c *Cluster) EveryMinute(ctx context.Context, taskId string, second int, callback callback, extendData interface{}) error {
|
func (c *Cluster) EveryMinute(ctx context.Context, taskId string, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
||||||
nowTime := time.Now().In(c.location)
|
nowTime := time.Now().In(c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
@@ -175,7 +175,7 @@ func (c *Cluster) EveryMinute(ctx context.Context, taskId string, second int, ca
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 特定时间间隔
|
// 特定时间间隔
|
||||||
func (c *Cluster) EverySpace(ctx context.Context, taskId string, spaceTime time.Duration, callback callback, extendData interface{}) error {
|
func (c *Cluster) EverySpace(ctx context.Context, taskId string, spaceTime time.Duration, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
||||||
nowTime := time.Now().In(c.location)
|
nowTime := time.Now().In(c.location)
|
||||||
|
|
||||||
if spaceTime < 0 {
|
if spaceTime < 0 {
|
||||||
@@ -203,7 +203,7 @@ func (c *Cluster) EverySpace(ctx context.Context, taskId string, spaceTime time.
|
|||||||
// @param callback callback 回调函数
|
// @param callback callback 回调函数
|
||||||
// @param extendData interface{} 扩展数据
|
// @param extendData interface{} 扩展数据
|
||||||
// @return error
|
// @return error
|
||||||
func (c *Cluster) addJob(ctx context.Context, taskId string, jobData JobData, callback callback, extendData interface{}) error {
|
func (c *Cluster) addJob(ctx context.Context, taskId string, jobData JobData, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
||||||
_, ok := clusterWorkerList.Load(taskId)
|
_, ok := clusterWorkerList.Load(taskId)
|
||||||
if ok {
|
if ok {
|
||||||
c.logger.Errorf(ctx, "key已存在:%s", taskId)
|
c.logger.Errorf(ctx, "key已存在:%s", taskId)
|
||||||
|
|||||||
+6
-6
@@ -30,7 +30,7 @@ func TestCluster_AddEveryMonth(t *testing.T) {
|
|||||||
}
|
}
|
||||||
extendData := "testData"
|
extendData := "testData"
|
||||||
|
|
||||||
err := cluster.AddMonth(ctx, taskId, 1, hour, minute, second, callback, extendData)
|
err := cluster.EveryMonth(ctx, taskId, 1, hour, minute, second, callback, extendData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("AddEveryMonth failed, err: %v", err)
|
t.Errorf("AddEveryMonth failed, err: %v", err)
|
||||||
}
|
}
|
||||||
@@ -59,7 +59,7 @@ func TestCluster_AddEveryWeek(t *testing.T) {
|
|||||||
}
|
}
|
||||||
extendData := "testData"
|
extendData := "testData"
|
||||||
|
|
||||||
err := cluster.AddWeek(ctx, taskId, week, hour, minute, second, callback, extendData)
|
err := cluster.EveryWeek(ctx, taskId, week, hour, minute, second, callback, extendData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("AddEveryWeek failed, err: %v", err)
|
t.Errorf("AddEveryWeek failed, err: %v", err)
|
||||||
}
|
}
|
||||||
@@ -87,7 +87,7 @@ func TestCluster_AddEveryDay(t *testing.T) {
|
|||||||
}
|
}
|
||||||
extendData := "testData"
|
extendData := "testData"
|
||||||
|
|
||||||
err := cluster.AddDay(ctx, taskId, hour, minute, second, callback, extendData)
|
err := cluster.EveryDay(ctx, taskId, hour, minute, second, callback, extendData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("AddEveryDay failed, err: %v", err)
|
t.Errorf("AddEveryDay failed, err: %v", err)
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,7 @@ func TestCluster_AddEveryHour(t *testing.T) {
|
|||||||
}
|
}
|
||||||
extendData := "testData"
|
extendData := "testData"
|
||||||
|
|
||||||
err := cluster.AddHour(ctx, taskId, minute, second, callback, extendData)
|
err := cluster.EveryHour(ctx, taskId, minute, second, callback, extendData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("AddEveryHour failed, err: %v", err)
|
t.Errorf("AddEveryHour failed, err: %v", err)
|
||||||
}
|
}
|
||||||
@@ -140,7 +140,7 @@ func TestCluster_AddEveryMinute(t *testing.T) {
|
|||||||
}
|
}
|
||||||
extendData := "testData"
|
extendData := "testData"
|
||||||
|
|
||||||
err := cluster.AddMinute(ctx, taskId, second, callback, extendData)
|
err := cluster.EveryMinute(ctx, taskId, second, callback, extendData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("AddEveryMinute failed, err: %v", err)
|
t.Errorf("AddEveryMinute failed, err: %v", err)
|
||||||
}
|
}
|
||||||
@@ -170,7 +170,7 @@ func TestCluster_Add(t *testing.T) {
|
|||||||
}
|
}
|
||||||
extendData := "testData"
|
extendData := "testData"
|
||||||
|
|
||||||
err := cluster.AddSpace(ctx, taskId, dur, callback, extendData)
|
err := cluster.EverySpace(ctx, taskId, dur, callback, extendData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Add failed, err: %v", err)
|
t.Errorf("Add failed, err: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ func InitSingle(ctx context.Context, opts ...Option) *Single {
|
|||||||
// @param callback 回调函数
|
// @param callback 回调函数
|
||||||
// @param extendData 扩展数据
|
// @param extendData 扩展数据
|
||||||
// @return error
|
// @return error
|
||||||
func (c *Single) AddMonth(ctx context.Context, taskId string, day int, hour int, minute int, second int, callback callback, extendData interface{}) (int, error) {
|
func (c *Single) AddMonth(ctx context.Context, taskId string, day int, hour int, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int, error) {
|
||||||
nowTime := time.Now()
|
nowTime := time.Now()
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
@@ -103,7 +103,7 @@ func (c *Single) AddMonth(ctx context.Context, taskId string, day int, hour int,
|
|||||||
// @param hour int 小时
|
// @param hour int 小时
|
||||||
// @param minute int 分钟
|
// @param minute int 分钟
|
||||||
// @param second int 秒
|
// @param second int 秒
|
||||||
func (c *Single) AddWeek(ctx context.Context, taskId string, week time.Weekday, hour int, minute int, second int, callback callback, extendData interface{}) (int, error) {
|
func (c *Single) AddWeek(ctx context.Context, taskId string, week time.Weekday, hour int, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int, error) {
|
||||||
nowTime := time.Now()
|
nowTime := time.Now()
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
@@ -119,7 +119,7 @@ func (c *Single) AddWeek(ctx context.Context, taskId string, week time.Weekday,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 每天执行一次
|
// 每天执行一次
|
||||||
func (c *Single) AddDay(ctx context.Context, taskId string, hour int, minute int, second int, callback callback, extendData interface{}) (int, error) {
|
func (c *Single) AddDay(ctx context.Context, taskId string, hour int, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int, error) {
|
||||||
nowTime := time.Now()
|
nowTime := time.Now()
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
@@ -134,7 +134,7 @@ func (c *Single) AddDay(ctx context.Context, taskId string, hour int, minute int
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 每小时执行一次
|
// 每小时执行一次
|
||||||
func (c *Single) AddHour(ctx context.Context, taskId string, minute int, second int, callback callback, extendData interface{}) (int, error) {
|
func (c *Single) AddHour(ctx context.Context, taskId string, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int, error) {
|
||||||
nowTime := time.Now()
|
nowTime := time.Now()
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
@@ -148,7 +148,7 @@ func (c *Single) AddHour(ctx context.Context, taskId string, minute int, second
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 每分钟执行一次
|
// 每分钟执行一次
|
||||||
func (c *Single) AddMinute(ctx context.Context, taskId string, second int, callback callback, extendData interface{}) (int, error) {
|
func (c *Single) AddMinute(ctx context.Context, taskId string, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int, error) {
|
||||||
nowTime := time.Now()
|
nowTime := time.Now()
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
@@ -161,7 +161,7 @@ func (c *Single) AddMinute(ctx context.Context, taskId string, second int, callb
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 特定时间间隔
|
// 特定时间间隔
|
||||||
func (c *Single) AddSpace(ctx context.Context, taskId string, spaceTime time.Duration, callback callback, extendData interface{}) (int, error) {
|
func (c *Single) AddSpace(ctx context.Context, taskId string, spaceTime time.Duration, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int, error) {
|
||||||
nowTime := time.Now()
|
nowTime := time.Now()
|
||||||
|
|
||||||
if spaceTime < 0 {
|
if spaceTime < 0 {
|
||||||
@@ -184,7 +184,7 @@ func (c *Single) AddSpace(ctx context.Context, taskId string, spaceTime time.Dur
|
|||||||
// @param extend 附加参数
|
// @param extend 附加参数
|
||||||
// @return int 定时器索引
|
// @return int 定时器索引
|
||||||
// @return error 错误
|
// @return error 错误
|
||||||
func (l *Single) addJob(ctx context.Context, jobData JobData, call callback, extend interface{}) (int, error) {
|
func (l *Single) addJob(ctx context.Context, jobData JobData, call func(ctx context.Context, extendData interface{}) error, extend interface{}) (int, error) {
|
||||||
singleTimerIndex += 1
|
singleTimerIndex += 1
|
||||||
|
|
||||||
_, err := GetNextTime(time.Now().In(l.location), jobData)
|
_, err := GetNextTime(time.Now().In(l.location), jobData)
|
||||||
@@ -281,7 +281,7 @@ func (s *Single) iterator(ctx context.Context) {
|
|||||||
|
|
||||||
// 定时器操作类
|
// 定时器操作类
|
||||||
// 这里不应painc
|
// 这里不应painc
|
||||||
func (s *Single) doTask(ctx context.Context, call callback, extend interface{}) error {
|
func (s *Single) doTask(ctx context.Context, call func(ctx context.Context, extendData interface{}) error, extend interface{}) error {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
s.logger.Errorf(ctx, "timer:回调任务panic err:%+v stack:%s", err, string(debug.Stack()))
|
s.logger.Errorf(ctx, "timer:回调任务panic err:%+v stack:%s", err, string(debug.Stack()))
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type timerStr struct {
|
type timerStr struct {
|
||||||
Callback callback // 需要回调的方法
|
Callback func(ctx context.Context, extendData interface{}) error // 需要回调的方法
|
||||||
CanRunning chan (struct{}) // 是否允许执行(only single)
|
CanRunning chan (struct{}) // 是否允许执行(only single)
|
||||||
TaskId string // 任务ID 全局唯一键(only cluster)
|
TaskId string // 任务ID 全局唯一键(only cluster)
|
||||||
ExtendData interface{} // 附加参数
|
ExtendData interface{} // 附加参数
|
||||||
@@ -40,4 +40,4 @@ type JobData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 定义各个回调函数
|
// 定义各个回调函数
|
||||||
type callback func(ctx context.Context, extendData interface{}) error
|
// type callback func(ctx context.Context, extendData interface{}) error
|
||||||
|
|||||||
Reference in New Issue
Block a user