runner包简介
- 用途
- 使用场景
- 调度后台处理任务的程序(该任务可能作为cron作业执行,or在基于定时任务的云环境里执行)
源码解析
// runner 包管理处理任务的运行与生命周期
package runner
import (
"errors"
"os"
"os/signal"
"time"
)
// 声明Runner结构,Runner的任务有
//1. 在给定的超时时间内执行一组任务
//2. 在操作系统发送中断信号时结束这些任务
type Runner struct {
// interrupt channel 接收从操作系统发来的信号
interruput chan os.Signal
// complete channel 报告处理任务已完成
complete chan error
// timeout chanel 报告处理任务已超时
timeout <-chan time.Time
// tasks 持有一组以索引顺序执行的任务函数
tasks []func(int)
}
// ErrTimeout 是在任务超时时返回的错误
var ErrTimeout = errors.New("received timeout")
// ErrInterrupt 是在接收到操作系统信号时返回的错误
var ErrInterrupt = errors.New("received interrupt")
// 工厂函数, 返回一个Runner类型的指针
func New(d time.Duration) *Runner {
// 分别初始化channel