task

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2021 License: MIT Imports: 3 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 ContextDoneCb

type ContextDoneCb 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{}) error

func (*Tasker) Init

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

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 WithContextDoneCb

func WithContextDoneCb(h ContextDoneCb) TaskerOption

func WithSleep

func WithSleep(d time.Duration) TaskerOption

func WithTimeout

func WithTimeout(d time.Duration) TaskerOption

func WithUpdateCb

func WithUpdateCb(du UpdateCb) TaskerOption

type TaskerOptions

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

type UpdateCb

type UpdateCb func()

Jump to

Keyboard shortcuts

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