Skip to content

Commit

Permalink
Add more examples for all public methods (#30)
Browse files Browse the repository at this point in the history
* Added new Example for NewWithSignals

* Added new Example for SetLogger
  • Loading branch information
skovtunenko authored Jul 3, 2022
1 parent e8bb584 commit bb677e6
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,46 @@ import (
"time"
)

func ExampleNewWithSignals() {
// create new Stopper instance:
stopper, appCtx := graterm.NewWithSignals(context.Background(), syscall.SIGINT, syscall.SIGTERM)

// register hooks...

// Wire other components ...

// Wait for os.Signal to occur, then terminate application with maximum timeout of 40 seconds:
if err := stopper.Wait(appCtx, 40*time.Second); err != nil {
log.Printf("graceful termination period is timed out: %+v", err)
}
}

func ExampleStopper_SetLogger() {
// create new Stopper instance:
stopper, _ := graterm.NewWithSignals(context.Background(), syscall.SIGINT, syscall.SIGTERM)

// Set custom logger implementation instead of default NOOP one:
stopper.SetLogger(log.Default())
}

func ExampleStopper_Wait() {
// create new Stopper instance:
stopper, appCtx := graterm.NewWithSignals(context.Background(), syscall.SIGINT, syscall.SIGTERM)

// register hooks...

// Wire other components ...

// Wait for os.Signal to occur, then terminate application with maximum timeout of 40 seconds:
if err := stopper.Wait(appCtx, 40*time.Second); err != nil {
log.Printf("graceful termination period is timed out: %+v", err)
}
}

func ExampleStopper_Register() {
// create new Stopper instance:
stopper, appCtx := graterm.NewWithSignals(context.Background(), syscall.SIGINT, syscall.SIGTERM)

// Register some hooks:
{
stopper.Register(1, "HOOK#1", 1*time.Second, func(ctx context.Context) {
Expand All @@ -48,6 +74,7 @@ func ExampleStopper_Register() {
}
})

// Wait for os.Signal to occur, then terminate application with maximum timeout of 40 seconds:
if err := stopper.Wait(appCtx, 20*time.Second); err != nil {
log.Printf("graceful termination period is timed out: %+v", err)
}
Expand Down

0 comments on commit bb677e6

Please sign in to comment.