From 14fcf184ae40729e270821115e2ba39c475badb9 Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Thu, 18 Mar 2021 17:58:00 +0300 Subject: v3 --- example/http_server.go | 9 +++++---- example/policies.go | 13 +++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) (limited to 'example') diff --git a/example/http_server.go b/example/http_server.go index 6bb7157..8b723b9 100755 --- a/example/http_server.go +++ b/example/http_server.go @@ -7,13 +7,14 @@ import ( "io" "log" "net/http" + "os" - "github.com/neonxp/rutina" + "github.com/neonxp/rutina/v3" ) func main() { // New instance with builtin context - r := rutina.New(rutina.Opt.SetListenOsSignals(true)) + r := rutina.New(rutina.ListenOsSignals(os.Interrupt, os.Kill)) srv := &http.Server{Addr: ":8080"} http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { @@ -27,14 +28,14 @@ func main() { } log.Println("Server stopped") return nil - }, rutina.RunOpt.SetOnDone(rutina.Shutdown)) + }) // Gracefully stopping server when context canceled r.Go(func(ctx context.Context) error { <-ctx.Done() log.Println("Stopping server...") return srv.Shutdown(ctx) - }, nil) + }) if err := r.Wait(); err != nil { log.Fatal(err) diff --git a/example/policies.go b/example/policies.go index 8bb4643..87081cb 100644 --- a/example/policies.go +++ b/example/policies.go @@ -6,26 +6,27 @@ import ( "context" "errors" "log" + "os" "time" - "github.com/neonxp/rutina" + "github.com/neonxp/rutina/v3" ) func main() { // New instance with builtin context - r := rutina.New(rutina.Opt.SetLogger(log.Printf).SetListenOsSignals(true)) + r := rutina.New(rutina.Logger(log.Printf), rutina.ListenOsSignals(os.Interrupt, os.Kill)) r.Go(func(ctx context.Context) error { <-time.After(1 * time.Second) log.Println("Do something 1 second without errors and restart") return nil - }, 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 - }, nil) + }) r.Go(func(ctx context.Context) error { select { @@ -34,7 +35,7 @@ func main() { case <-ctx.Done(): return nil } - }, rutina.RunOpt.SetOnError(rutina.Restart).SetMaxCount(10)) + }, rutina.OnError(rutina.Restart), rutina.MaxCount(10)) r.Go(func(ctx context.Context) error { select { @@ -43,7 +44,7 @@ func main() { case <-ctx.Done(): return nil } - }, rutina.RunOpt.SetOnError(rutina.Restart).SetTimeout(10*time.Second)) + }, rutina.OnError(rutina.Restart), rutina.SetTimeout(10*time.Second)) if err := r.Wait(); err != nil { log.Fatal(err) -- cgit v1.2.3