awesome-patterns/concurrency/runner/main.go
2017-12-08 22:04:30 +10:00

35 lines
603 B
Go

package main
import (
"errors"
"os"
"os/signal"
"time"
)
type Runner struct {
interrupt chan os.Signal
complete chan error
timeout <-chan time.Time
tasks []func(int)
}
var ErrTimeout = errors.New("Received Timeout")
var ErrInterrupt = errors.New("Received Interrupt")
func New(d time.Duration) *Runner {
return &Runner{
interrupt: make(chan os.Signal, 1),
complete: make(chan error),
timeout: time.After(d),
}
}
func (r *Runner) Add(tasks ...func(int)) {
r.tasks = append(r.tasks, tasks...)
}
func (r *Runner) Start() error {
signal.Notify(r.interrupt, os.Interrupt)
}