stop

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2022 License: Apache-2.0 Imports: 8 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnavailable stopper is not running
	ErrUnavailable = errors.New("runner is unavailable")
)

Functions

This section is empty.

Types

type Option

type Option func(*options)

Option stop option

func WithLogger

func WithLogger(logger *zap.Logger) Option

WithLogger set the logger

func WithStopTimeout

func WithStopTimeout(timeout time.Duration) Option

WithStopTimeout the stopper will print the names of tasks that are still running beyond this timeout.

func WithTimeoutTaskHandler

func WithTimeoutTaskHandler(handler func(tasks []string, timeAfterStop time.Duration)) Option

WithTimeoutTaskHandler set handler to handle timeout tasks

type Stopper

type Stopper struct {
	// contains filtered or unexported fields
}

Stopper a stopper used to to manage all tasks that are executed in a separate goroutine, and Stopper can manage these goroutines centrally to avoid leaks. When Stopper's Stop method is called, if some tasks do not exit within the specified time, the names of these tasks will be returned for analysis.

func NewStopper

func NewStopper(name string, opts ...Option) *Stopper

NewStopper create a stopper

func (*Stopper) RunNamedTask

func (s *Stopper) RunNamedTask(ctx context.Context, name string, task func(context.Context)) error

RunNamedTask run a task that can be cancelled. ErrUnavailable returned if stopped is not running Example:

err := s.RunNamedTask("named task", func(ctx context.Context) {
	select {
	case <-ctx.Done():
	// cancelled
	case <-time.After(time.Second):
		// do something
	}
})
if err != nil {
	// hanle error
	return
}

func (*Stopper) RunTask

func (s *Stopper) RunTask(ctx context.Context, task func(context.Context)) error

RunTask run a task that can be cancelled. ErrUnavailable returned if stopped is not running See also `RunNamedTask` Example:

err := s.RunTask(func(ctx context.Context) {
	select {
	case <-ctx.Done():
	// cancelled
	case <-time.After(time.Second):
		// do something
	}
})
if err != nil {
	// hanle error
	return
}

func (*Stopper) Stop

func (s *Stopper) Stop()

Stop stop all task, and wait to all tasks canceled. If some tasks do not exit within the specified time, the names of these tasks will be print to the given logger.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL