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 /example/policies.go | |
parent | 01eeeaf5e136928abe75f95d58f3f9cce11c6fe6 (diff) |
Refactored
New clean API
Timeouts and restart limits
Diffstat (limited to 'example/policies.go')
-rw-r--r-- | example/policies.go | 51 |
1 files changed, 17 insertions, 34 deletions
diff --git a/example/policies.go b/example/policies.go index 6addea7..8bb4643 100644 --- a/example/policies.go +++ b/example/policies.go @@ -13,58 +13,41 @@ import ( func main() { // New instance with builtin context - r := rutina.New() - - r = r.With(rutina.WithErrChan(), rutina.WithStdLogger()) + r := rutina.New(rutina.Opt.SetLogger(log.Printf).SetListenOsSignals(true)) r.Go(func(ctx context.Context) error { <-time.After(1 * time.Second) log.Println("Do something 1 second without errors and restart") return nil - }, rutina.RestartIfDone, rutina.ShutdownIfError) + }, nil) r.Go(func(ctx context.Context) error { <-time.After(2 * time.Second) log.Println("Do something 2 seconds without errors and do nothing") return nil - }, rutina.DoNothingIfDone, rutina.ShutdownIfError) - - r.Go(func(ctx context.Context) error { - <-time.After(3 * time.Second) - log.Println("Do something 3 seconds with error and restart") - return errors.New("Error #1!") - }, rutina.RestartIfError) + }, nil) r.Go(func(ctx context.Context) error { - <-time.After(4 * time.Second) - log.Println("Do something 4 seconds with error and do nothing") - return errors.New("Error #2!") - }, rutina.DoNothingIfError) - - r.Go(func(ctx context.Context) error { - <-time.After(10 * time.Second) - log.Println("Do something 10 seconds with error and close context") - return errors.New("Successfully shutdown at proper place") - }, rutina.ShutdownIfError) + select { + case <-time.After(time.Second): + return errors.New("max 10 times") + case <-ctx.Done(): + return nil + } + }, rutina.RunOpt.SetOnError(rutina.Restart).SetMaxCount(10)) r.Go(func(ctx context.Context) error { - for { - select { - case <-ctx.Done(): - log.Println("Shutdown chan listener") - return nil - case err := <-r.Errors(): - log.Printf("Error in chan: %v", err) - } + select { + case <-time.After(time.Second): + return errors.New("max 10 seconds") + case <-ctx.Done(): + return nil } - }) - - // OS signals subscriber - r.ListenOsSignals() + }, rutina.RunOpt.SetOnError(rutina.Restart).SetTimeout(10*time.Second)) if err := r.Wait(); err != nil { log.Fatal(err) } else { - log.Println("Routines stopped but not correct") + log.Println("Routines stopped") } } |