stopper

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnavailable stopper is not running
	ErrUnavailable = moerr.NewInternalErrorNoCtx("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) GetTaskCount

func (s *Stopper) GetTaskCount() int64

GetTaskCount returns number of the running task

func (*Stopper) RunNamedTask

func (s *Stopper) RunNamedTask(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(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