diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2020-01-12 16:40:01 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2020-01-12 16:40:01 +0300 |
commit | 837cfe3223db11bf7f5edb6ba738d3e328b80ea2 (patch) | |
tree | 8207e53a6411ea5cd9f1da96f0de9d957c7301f2 /options.go | |
parent | 01eeeaf5e136928abe75f95d58f3f9cce11c6fe6 (diff) |
Refactored
New clean API
Timeouts and restart limits
Diffstat (limited to 'options.go')
-rw-r--r-- | options.go | 85 |
1 files changed, 77 insertions, 8 deletions
@@ -1,13 +1,82 @@ package rutina -// Options sets custom run policies -type Options int +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 ( - ShutdownIfError Options = iota // Shutdown all routines if fail - RestartIfError // Restart this routine if fail - DoNothingIfError // Do nothing on fail - ShutdownIfDone // Shutdown all routines if this routine done without errors - RestartIfDone // Restart if this routine done without errors - DoNothingIfDone // Do nothing if this routine done without errors + 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, +} |