diff options
Diffstat (limited to 'example')
-rwxr-xr-x | example/http_server.go | 2 | ||||
-rw-r--r-- | example/policies.go | 55 |
2 files changed, 56 insertions, 1 deletions
diff --git a/example/http_server.go b/example/http_server.go index 86255e6..fe1d456 100755 --- a/example/http_server.go +++ b/example/http_server.go @@ -12,7 +12,7 @@ import ( ) func main() { - // New instance with builtin context. Alternative: r, ctx := rutina.OptionContext(ctx) + // New instance with builtin context r := rutina.New(rutina.WithStdLogger()) srv := &http.Server{Addr: ":8080"} 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") + } +} |