awesome-patterns/concurrency/runner/main.go

35 lines
603 B
Go
Raw Normal View History

2017-12-08 15:04:30 +03:00
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)
}