# Functions
NewScheduler 创建一个任务调度器
@param ctx context.Context 根Context @param lg logger.Iface 可选的日志句柄 @return scheduler 任务调度器
# Usage // 首先创建一个根 Context pCtx, _ := context.WithTimeout(context.Background(), 50*time.Second) scheduler := NewScheduler(pCtx, logger)
// 定义任务, 需实现 CronJob 接口 type Clock struct { Job // 默认 Job 未实现 Do 方法 lastTime time.Time }
func (c *Clock) Interval() time.Duration { return 1 * time.Second } func (c *Clock) String() string { return "报时器" }
func (c *Clock) Do(ctx context.Context) error { diff := time.Now().Sub(c.lastTime) c.lastTime = time.Now() fmt.Println("time interval:", diff.String())
return nil }
// 注册任务 scheduler.Add(&Clock{}) // 运行调度器 scheduler.Run() // 检测调度器是否退出 <-scheduler.Done().
# Interfaces
No description provided by the author