task

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2021 License: MIT Imports: 7 Imported by: 0

README

GoDoc Go Report Card

task

This simple package provides a concurrency and configued tasks handle queue.

Example

// new a tasker with options
tasker := NewTasker(1)
tasker.Init(
    WithContextDoneCb(func() {
        fmt.Println("tasker context done...")
    }),

    WithTimeout(time.Second*5),

    WithSleep(time.Millisecond*100),

    WithUpdateCb(func() {
        fmt.Println("tasker update...")
    }),
)

// add a simple task
tasker.Add(
    context.Background(),
    func(context.Context, ...interface{}) error {
        fmt.Println("task handled")
    },
)

// add task with parameters
tasker.Add(
    context.Background(),
    func(ctx context.Context, p ...interface{}) error {
        p1 := p[0].(int)
        p2 := p[1].(string)
        fmt.Println("task handled with parameters:", p1, p2)
    },
    1,
    "name",
)

// begin run
go tasker.Run(context.Background())

// stop
tasker.Stop()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TaskDefaultExecuteTimeout = time.Second * 5          // execute timeout
	TaskDefaultTimeout        = time.Hour * 24 * 30 * 12 // default timeout
	TaskDefaultSleep          = time.Millisecond * 500   // sleep time 500ms
	ErrTimeout                = errors.New("time out")
)

Functions

This section is empty.

Types

type StartFn added in v1.0.2

type StartFn func()

type StopFn added in v1.0.9

type StopFn func()

type Task

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

type TaskHandler

type TaskHandler func(context.Context, ...interface{}) error

type Tasker

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

func NewTasker

func NewTasker(max int32) *Tasker

func (*Tasker) Add

func (t *Tasker) Add(ctx context.Context, f TaskHandler, p ...interface{})

func (*Tasker) AddWait added in v1.0.6

func (t *Tasker) AddWait(ctx context.Context, f TaskHandler, p ...interface{}) error

func (*Tasker) Init

func (t *Tasker) Init(opts ...TaskerOption)

func (*Tasker) IsRunning added in v1.0.5

func (t *Tasker) IsRunning() bool

func (*Tasker) ResetTimer

func (t *Tasker) ResetTimer()

func (*Tasker) Run

func (t *Tasker) Run(ctx context.Context) error

func (*Tasker) Stop

func (t *Tasker) Stop()

type TaskerOption

type TaskerOption func(*TaskerOptions)

func WithSleep

func WithSleep(d time.Duration) TaskerOption

func WithStartFns added in v1.0.3

func WithStartFns(f ...StartFn) TaskerOption

func WithStopFn added in v1.0.9

func WithStopFn(f StopFn) TaskerOption

func WithTimeout

func WithTimeout(d time.Duration) TaskerOption

func WithUpdateFn added in v1.0.2

func WithUpdateFn(f UpdateFn) TaskerOption

type TaskerOptions

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

type UpdateFn added in v1.0.2

type UpdateFn func()

Jump to

Keyboard shortcuts

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