一、Runner
main.go
func makeFunc() func(int) {
return func(i int) {
time.Sleep(1 * time.Second)
fmt.Println("task completed: ", i)
}
}
func main() {
r := test.NewRunner(4 * time.Second)
r.AddTasts(makeFunc(), makeFunc(), makeFunc())
err := r.Start()
fmt.Println("err: ", err)
}
test.go
type runner struct {
interrrupt chan os.Signal
complete chan error
timeout <-chan time.Time
tasts []func(int)
}
func (r *runner) AddTasts(tasks ...func(int)) {
r.tasts = append(r.tasts, tasks...)
}
func (r *runner) Run() error {
for id, task := range r.tasts {
select {
case <-r.interrrupt:
signal.Stop(r.interrrupt)
return errors.New("os interrrupt")
default:
task(id)
}
}
return nil
}
func (r *runner) Start() error {
//recf os.sign
signal.Notify(r.interrrupt, os.Interrupt)
//run