Documentation
¶
Overview ¶
Example ¶
// prepare service
service := &Service{}
// set reporter
var errs []error
service.Report(func(err error) {
errs = append(errs, err)
})
// run task
i := 0
service.Run(1, func() error {
i++
if i == 5 {
return ErrDone
}
if i%2 == 0 {
return errors.New("foo")
}
return nil
}, func() {
fmt.Println("finalize")
})
// wait for exit
<-service.Done()
// print output
fmt.Println(i)
fmt.Println(errs)
Output: finalize 5 [foo foo]
Index ¶
- Variables
- func Close(stop, kill time.Duration, closers ...Closer) bool
- func Repeat(task func() error, reporter func(error))
- func Run(n int, task func(), finalizer func())
- type Closer
- type Manager
- type Reporter
- type Service
- type Terminator
- func (t *Terminator) IsKilled() bool
- func (t *Terminator) IsStopping() bool
- func (t *Terminator) Kill()
- func (t *Terminator) Killed() <-chan struct{}
- func (t *Terminator) Notify(fn func())
- func (t *Terminator) Status() error
- func (t *Terminator) Stop()
- func (t *Terminator) Stopping() <-chan struct{}
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrDone = errors.New("done")
ErrDone is returned to indicate that the task is done.
var ErrKilled = errors.New("killed")
ErrKilled indicates that a Terminator has been killed.
var ErrStopped = errors.New("stopped")
ErrStopped indicates that a Terminator has been stopped.
Functions ¶
Types ¶
type Closer ¶
type Closer interface {
Done() <-chan struct{}
Stop()
Kill()
}
Closer is a closable service or struct that embeds a service.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
A Manager manages multiple finite running tasks.
func (*Manager) Done ¶
func (m *Manager) Done() <-chan struct{}
Done will return a channel that is closed once all until now started tasks have returned.
type Reporter ¶
type Reporter struct {
// contains filtered or unexported fields
}
A Reporter manages repeating tasks and their error reporting.
type Service ¶
type Service struct {
Manager
Terminator
Reporter
}
Service manages multiple long-running tasks.
type Terminator ¶
type Terminator struct {
// contains filtered or unexported fields
}
Terminator provides a stopping and killing mechanism.
func (*Terminator) IsKilled ¶
func (t *Terminator) IsKilled() bool
IsKilled returns whether Kill has been called.
func (*Terminator) IsStopping ¶
func (t *Terminator) IsStopping() bool
IsStopping returns whether Stop has been called.
func (*Terminator) Kill ¶
func (t *Terminator) Kill()
Kill will close the Stopping and Killed channel.
func (*Terminator) Killed ¶
func (t *Terminator) Killed() <-chan struct{}
Killed returns the channel closed by Kill.
func (*Terminator) Notify ¶ added in v0.3.0
func (t *Terminator) Notify(fn func())
Notify will store the specified callback and call it once the terminator has been stopped.
func (*Terminator) Status ¶
func (t *Terminator) Status() error
Status returns and error if Stop or Kill have been called.
func (*Terminator) Stopping ¶
func (t *Terminator) Stopping() <-chan struct{}
Stopping returns the channel closed by Stop.