update task
This commit is contained in:
		| @ -22,5 +22,5 @@ type ITask interface { | |||||||
| 	// Callback 任务执行成功的回调 | 	// Callback 任务执行成功的回调 | ||||||
| 	Callback(result *Result) error | 	Callback(result *Result) error | ||||||
| 	// Execute 执行任务 | 	// Execute 执行任务 | ||||||
| 	Execute(ctx context.Context, cfg *Config) (map[string]interface{}, error) | 	Execute(ctx context.Context, cfg *Config) *Result | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										11
									
								
								core.go
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								core.go
									
									
									
									
									
								
							| @ -133,7 +133,7 @@ func (tw *TimeWheel) checkAndRunTask() { | |||||||
|  |  | ||||||
| 						} | 						} | ||||||
| 					}() | 					}() | ||||||
| 					_, _ = task.Job.Execute(nil, nil) | 					_ = task.Job.Execute(nil, nil) | ||||||
| 				}() | 				}() | ||||||
| 			} else if tw.job != nil { | 			} else if tw.job != nil { | ||||||
| 				go func() { | 				go func() { | ||||||
| @ -142,7 +142,7 @@ func (tw *TimeWheel) checkAndRunTask() { | |||||||
|  |  | ||||||
| 						} | 						} | ||||||
| 					}() | 					}() | ||||||
| 					_, _ = tw.job.Execute(nil, nil) | 					_ = tw.job.Execute(nil, nil) | ||||||
| 				}() | 				}() | ||||||
| 			} else { | 			} else { | ||||||
| 				fmt.Println(fmt.Sprintf("The task %v don't have job to run", task.Key)) | 				fmt.Println(fmt.Sprintf("The task %v don't have job to run", task.Key)) | ||||||
| @ -182,9 +182,12 @@ func (tw *TimeWheel) checkAndRunTask() { | |||||||
| // | // | ||||||
| // Date : 13:43 2023/8/4 | // Date : 13:43 2023/8/4 | ||||||
| func (tw *TimeWheel) AddTask(task *Task, byInterval bool) { | func (tw *TimeWheel) AddTask(task *Task, byInterval bool) { | ||||||
|  | 	if nil == task { | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
| 	// 生成 run_id | 	// 生成 run_id | ||||||
| 	task.RunID = wrapper.StringFromRandom(128, "").Md5().Value | 	task.RunID = wrapper.StringFromRandom(128, "").Md5().Value | ||||||
| 	if nil != task && nil != task.Job { | 	if nil != task.Job { | ||||||
| 		task.Key = task.Job.GetFlag() + "_" + task.RunID | 		task.Key = task.Job.GetFlag() + "_" + task.RunID | ||||||
| 	} | 	} | ||||||
| 	var pos, circle int | 	var pos, circle int | ||||||
| @ -239,7 +242,7 @@ func (tw *TimeWheel) getPosAndCircleByInterval(d time.Duration) (int, int) { | |||||||
| // Author : go_developer@163.com<白茶清欢> | // Author : go_developer@163.com<白茶清欢> | ||||||
| // | // | ||||||
| // Date : 13:47 2023/8/4 | // Date : 13:47 2023/8/4 | ||||||
| func (tw *TimeWheel) getPosAndCircleByCreatedTime(createdTime time.Time, d time.Duration, key interface{}) (int, int) { | func (tw *TimeWheel) getPosAndCircleByCreatedTime(createdTime time.Time, d time.Duration, key any) (int, int) { | ||||||
|  |  | ||||||
| 	passedTime := time.Since(createdTime) | 	passedTime := time.Since(createdTime) | ||||||
| 	passedSeconds := int(passedTime.Seconds()) | 	passedSeconds := int(passedTime.Seconds()) | ||||||
|  | |||||||
| @ -61,7 +61,7 @@ func (t testTask) Callback(result *Result) error { | |||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func (t testTask) Execute(ctx context.Context, cfg *Config) (map[string]interface{}, error) { | func (t testTask) Execute(ctx context.Context, cfg *Config) (map[string]any, error) { | ||||||
| 	fmt.Println(wrapper.OwnTimeFromNow().ToString()) | 	fmt.Println(wrapper.OwnTimeFromNow().ToString()) | ||||||
| 	return nil, nil | 	return nil, nil | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										18
									
								
								define.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								define.go
									
									
									
									
									
								
							| @ -18,7 +18,7 @@ type Config struct { | |||||||
| 	// ForbiddenCallback 禁用执行结果回调 | 	// ForbiddenCallback 禁用执行结果回调 | ||||||
| 	ForbiddenCallback bool | 	ForbiddenCallback bool | ||||||
| 	// Param 任务执行参数 | 	// Param 任务执行参数 | ||||||
| 	Param map[string]interface{} | 	Param map[string]any | ||||||
| 	// TaskFlag 任务标识 | 	// TaskFlag 任务标识 | ||||||
| 	TaskFlag string | 	TaskFlag string | ||||||
| 	// TaskName 任务名称 | 	// TaskName 任务名称 | ||||||
| @ -33,12 +33,12 @@ type Config struct { | |||||||
| // | // | ||||||
| // Date : 14:43 2022/6/23 | // Date : 14:43 2022/6/23 | ||||||
| type Result struct { | type Result struct { | ||||||
| 	StartTime       int64                  // 开始时间, 纳秒 | 	StartTime       int64          // 开始时间, 纳秒 | ||||||
| 	FinishTime      int64                  // 结束时间, 纳秒 | 	FinishTime      int64          // 结束时间, 纳秒 | ||||||
| 	Used            int64                  // 耗时, 纳秒 | 	Used            int64          // 耗时, 纳秒 | ||||||
| 	TaskRunID       string                 // 任务运行ID | 	TaskRunID       string         // 任务运行ID | ||||||
| 	TaskDescription string                 // 任务描述 | 	TaskDescription string         // 任务描述 | ||||||
| 	TaskConfig      *Config                // 任务配置 | 	TaskConfig      *Config        // 任务配置 | ||||||
| 	Data            map[string]interface{} // 任务结果数据 | 	Data            map[string]any // 任务结果数据 | ||||||
| 	Err             error                  // 异常信息, err == nil , 代表执行成功 | 	Err             error          // 异常信息, err == nil , 代表执行成功 | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										11
									
								
								task.go
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								task.go
									
									
									
									
									
								
							| @ -10,6 +10,7 @@ package task | |||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"git.zhangdeman.cn/zhangdeman/wrapper" | ||||||
| 	"runtime" | 	"runtime" | ||||||
| 	"sync" | 	"sync" | ||||||
| 	"time" | 	"time" | ||||||
| @ -51,10 +52,10 @@ func (d *dispatch) Register(taskInstanceList ...ITask) error { | |||||||
| 		if nil == taskInstance { | 		if nil == taskInstance { | ||||||
| 			continue | 			continue | ||||||
| 		} | 		} | ||||||
| 		if _, exist := d.taskTable[taskInstance.Name()]; exist { | 		if _, exist := d.taskTable[taskInstance.GetFlag()]; exist { | ||||||
| 			return fmt.Errorf("%s 任务重复注册! ", taskInstance.Name()) | 			return fmt.Errorf("%s 任务重复注册! ", taskInstance.GetFlag()) | ||||||
| 		} | 		} | ||||||
| 		d.taskTable[taskInstance.Name()] = taskInstance | 		d.taskTable[taskInstance.GetFlag()] = taskInstance | ||||||
| 	} | 	} | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| @ -104,7 +105,7 @@ func (d *dispatch) Run(ctx context.Context, cfg *Config) *Result { | |||||||
| 		StartTime:       time.Now().UnixNano(), | 		StartTime:       time.Now().UnixNano(), | ||||||
| 		FinishTime:      0, | 		FinishTime:      0, | ||||||
| 		Used:            0, | 		Used:            0, | ||||||
| 		TaskRunID:       "", | 		TaskRunID:       wrapper.StringFromRandom(64, "").Md5().Value, | ||||||
| 		TaskDescription: "", | 		TaskDescription: "", | ||||||
| 		TaskConfig:      cfg, | 		TaskConfig:      cfg, | ||||||
| 		Data:            nil, | 		Data:            nil, | ||||||
| @ -131,13 +132,11 @@ func (d *dispatch) Run(ctx context.Context, cfg *Config) *Result { | |||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 			result = taskInstance.Execute(ctx, cfg) | 			result = taskInstance.Execute(ctx, cfg) | ||||||
| 			result.TaskRunID = taskInstance.GetRunID() |  | ||||||
| 			result.TaskDescription = taskInstance.Description() | 			result.TaskDescription = taskInstance.Description() | ||||||
| 			_ = taskInstance.Callback(result) | 			_ = taskInstance.Callback(result) | ||||||
| 		}() | 		}() | ||||||
| 	} else { | 	} else { | ||||||
| 		result = taskInstance.Execute(ctx, cfg) | 		result = taskInstance.Execute(ctx, cfg) | ||||||
| 		result.TaskRunID = taskInstance.GetRunID() |  | ||||||
| 		result.TaskDescription = taskInstance.Description() | 		result.TaskDescription = taskInstance.Description() | ||||||
| 		_ = taskInstance.Callback(result) | 		_ = taskInstance.Callback(result) | ||||||
| 	} | 	} | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user