rutina/options.go
Alexander Kiryukhin 837cfe3223 Refactored
New clean API
Timeouts and restart limits
2020-01-12 16:40:01 +03:00

83 lines
1.4 KiB
Go

package rutina
import (
"context"
"time"
)
type Options struct {
ParentContext context.Context
ListenOsSignals bool
Logger func(format string, v ...interface{})
Errors chan error
}
func (o *Options) SetParentContext(ctx context.Context) *Options {
o.ParentContext = ctx
return o
}
func (o *Options) SetListenOsSignals(listenOsSignals bool) *Options {
o.ListenOsSignals = listenOsSignals
return o
}
func (o *Options) SetLogger(l logger) *Options {
o.Logger = l
return o
}
func (o *Options) SetErrors(errCh chan error) *Options {
o.Errors = errCh
return o
}
var Opt = &Options{
ParentContext: context.Background(),
ListenOsSignals: false,
Logger: nil,
Errors: nil,
}
type Policy int
const (
DoNothing Policy = iota
Shutdown
Restart
)
type RunOptions struct {
OnDone Policy
OnError Policy
Timeout *time.Duration
MaxCount *int
}
func (rp *RunOptions) SetOnDone(policy Policy) *RunOptions {
rp.OnDone = policy
return rp
}
func (rp *RunOptions) SetOnError(policy Policy) *RunOptions {
rp.OnError = policy
return rp
}
func (rp *RunOptions) SetTimeout(timeout time.Duration) *RunOptions {
rp.Timeout = &timeout
return rp
}
func (rp *RunOptions) SetMaxCount(maxCount int) *RunOptions {
rp.MaxCount = &maxCount
return rp
}
var RunOpt = &RunOptions{
OnDone: DoNothing,
OnError: Shutdown,
Timeout: nil,
MaxCount: nil,
}