From d45d913c9e0d088f339c007f14d2fcaa7b9c8c74 Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Wed, 27 Mar 2019 02:44:38 +0300 Subject: Flexible run policies Options refactoring --- options.go | 57 +++++++++------------------------------------------------ 1 file changed, 9 insertions(+), 48 deletions(-) mode change 100755 => 100644 options.go (limited to 'options.go') diff --git a/options.go b/options.go old mode 100755 new mode 100644 index 8144dde..d1030ba --- a/options.go +++ b/options.go @@ -1,51 +1,12 @@ package rutina -import ( - "context" - "log" - "os" +type Options int + +const ( + ShutdownIfFail Options = iota // Shutdown all routines if fail + RestartIfFail // Restart this routine if fail + DoNothingIfFail // 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 ) - -type Option interface { - apply(*Rutina) -} - -type OptionContext struct { - Context context.Context -} - -func WithContext(context context.Context) *OptionContext { - return &OptionContext{Context: context} -} - -func (o OptionContext) apply(r *Rutina) { - ctx, cancel := context.WithCancel(o.Context) - r.ctx = ctx - r.Cancel = cancel -} - -type OptionLogger struct { - Logger *log.Logger -} - -func WithLogger(logger *log.Logger) *OptionLogger { - return &OptionLogger{Logger: logger} -} - -func WithStdLogger() *OptionLogger { - return &OptionLogger{Logger: log.New(os.Stdout, "rutina", log.LstdFlags)} -} - -func (o OptionLogger) apply(r *Rutina) { - r.logger = o.Logger -} - -type OptionCancelByError struct{} - -func WithCancelByError() *OptionCancelByError { - return &OptionCancelByError{} -} - -func (OptionCancelByError) apply(r *Rutina) { - r.cancelByError = true -} -- cgit v1.2.3