aboutsummaryrefslogtreecommitdiff
path: root/options.go
diff options
context:
space:
mode:
Diffstat (limited to 'options.go')
-rw-r--r--options.go85
1 files changed, 77 insertions, 8 deletions
diff --git a/options.go b/options.go
index be91068..70a9965 100644
--- a/options.go
+++ b/options.go
@@ -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,
+}