diff options
author | Alexander Kiryukhin <alexander@kiryukhin.su> | 2019-04-03 23:55:11 +0300 |
---|---|---|
committer | Alexander Kiryukhin <alexander@kiryukhin.su> | 2019-04-03 23:55:11 +0300 |
commit | 05212e50c9761181b1a2c0c9e8b43ae31fb24017 (patch) | |
tree | c25c9b189046d99a42d0b8bcc29422dc8d74d9cc /example | |
parent | 1772990500c97a79adce15c73919339d6e1618d7 (diff) |
Added:
- Mixin with errors channel
Changed:
- Default run policy now `ShutdownIfDone` && `ShutdownIfFail`
Fixed:
- Fixed OS signals listener
- Removed some dead code
Diffstat (limited to 'example')
-rw-r--r-- | example/policies.go | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/example/policies.go b/example/policies.go index 5f09b8a..50a4e07 100644 --- a/example/policies.go +++ b/example/policies.go @@ -14,6 +14,10 @@ func main() { // New instance with builtin context r := rutina.New() + errsChan := make(chan error, 1) + + r = r.With(rutina.WithErrChan(errsChan)) + r.Go(func(ctx context.Context) error { <-time.After(1 * time.Second) log.Println("Do something 1 second without errors and restart") @@ -24,18 +28,18 @@ func main() { <-time.After(2 * time.Second) log.Println("Do something 2 seconds without errors and do nothing") return nil - }, rutina.ShutdownIfFail) + }, rutina.DoNothingIfDone, rutina.ShutdownIfFail) 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!") + return errors.New("Error #1!") }, rutina.RestartIfFail) 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!") + return errors.New("Error #2!") }, rutina.DoNothingIfFail) r.Go(func(ctx context.Context) error { @@ -44,6 +48,18 @@ func main() { return errors.New("Successfully shutdown at proper place") }, rutina.ShutdownIfFail) + r.Go(func(ctx context.Context) error { + for { + select { + case <-ctx.Done(): + log.Println("Shutdown chan listener") + return nil + case err := <-errsChan: + log.Printf("Error in chan: %v", err) + } + } + }) + // OS signals subscriber r.ListenOsSignals() |