mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-12-01 09:26:04 +03:00
35 lines
603 B
Go
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)
|
|
}
|