diff options
Diffstat (limited to 'example')
-rwxr-xr-x | example/http_server.go | 11 | ||||
-rw-r--r-- | example/policies.go | 51 |
2 files changed, 21 insertions, 41 deletions
diff --git a/example/http_server.go b/example/http_server.go index fe1d456..6bb7157 100755 --- a/example/http_server.go +++ b/example/http_server.go @@ -13,7 +13,7 @@ import ( func main() { // New instance with builtin context - r := rutina.New(rutina.WithStdLogger()) + r := rutina.New(rutina.Opt.SetListenOsSignals(true)) srv := &http.Server{Addr: ":8080"} http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { @@ -27,17 +27,14 @@ func main() { } log.Println("Server stopped") return nil - }) + }, rutina.RunOpt.SetOnDone(rutina.Shutdown)) - // Gracefully stoping server when context canceled + // Gracefully stopping server when context canceled r.Go(func(ctx context.Context) error { <-ctx.Done() log.Println("Stopping server...") return srv.Shutdown(ctx) - }) - - // OS signals subscriber - r.ListenOsSignals() + }, nil) if err := r.Wait(); err != nil { log.Fatal(err) 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") } } |