diff options
author | Alexander Kiryukhin <alexander@kiryukhin.su> | 2019-03-27 02:44:38 +0300 |
---|---|---|
committer | Alexander Kiryukhin <alexander@kiryukhin.su> | 2019-03-27 02:44:38 +0300 |
commit | d45d913c9e0d088f339c007f14d2fcaa7b9c8c74 (patch) | |
tree | 486d43d7359b5842f6f34b814764389fa27640d2 /example/policies.go | |
parent | 11a32ca219905f7c42698b55b6af9920626b7b51 (diff) |
Flexible run policiesv0.3.0
Options refactoring
Diffstat (limited to 'example/policies.go')
-rw-r--r-- | example/policies.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/example/policies.go b/example/policies.go new file mode 100644 index 0000000..5f09b8a --- /dev/null +++ b/example/policies.go @@ -0,0 +1,55 @@ +// +build ignore + +package main + +import ( + "context" + "errors" + "github.com/neonxp/rutina" + "log" + "time" +) + +func main() { + // New instance with builtin context + r := rutina.New() + + 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.ShutdownIfFail) + + 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.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!") + }, 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!") + }, rutina.DoNothingIfFail) + + 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.ShutdownIfFail) + + // OS signals subscriber + r.ListenOsSignals() + + if err := r.Wait(); err != nil { + log.Fatal(err) + } else { + log.Println("Routines stopped but not correct") + } +} |