task

package module
v1.3.8 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: MIT Imports: 13 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(
    WithStartFns(
			func() {
				fmt.Println("tasker start fn1...")
			},
		),

    WithTimeout(time.Second*5),

    WithSleep(time.Millisecond*100),

    WithUpdateInterval(time.Millisecond*200),

    WithUpdateFn(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

Overview

Package mpsc provides an efficient implementation of a multi-producer, single-consumer lock-free queue.

The Push function is safe to call from multiple goroutines. The Pop and Empty APIs must only be called from a single, consumer goroutine.

Index

Constants

This section is empty.

Variables

View Source
var (
	TaskDefaultExecuteTimeout = time.Second * 5      // execute timeout
	TaskDefaultTimeout        = time.Hour * 24 * 356 // default timeout
	TaskDefaultUpdateInterval = time.Second          // update interval
	ErrTimeout                = errors.New("time out")
	ErrTaskPanic              = errors.New("task panic")
	ErrTaskFailed             = errors.New("task failed")
	ErrTaskFulled             = errors.New("task fulled")
	ErrTaskNotRunning         = errors.New("task not running")
)

Functions

This section is empty.

Types

type Queue added in v1.3.5

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

func NewQueue added in v1.3.5

func NewQueue() *Queue

func (*Queue) Empty added in v1.3.5

func (q *Queue) Empty() bool

Empty returns true if the queue is empty

Empty must be called from a single, consumer goroutine

func (*Queue) Pop added in v1.3.5

func (q *Queue) Pop() interface{}

Pop removes the item from the front of the queue or nil if the queue is empty

Pop must be called from a single, consumer goroutine

func (*Queue) Push added in v1.3.5

func (q *Queue) Push(x interface{})

Push adds x to the back of the queue.

Push can be safely called from multiple goroutines

func (*Queue) Size added in v1.3.5

func (q *Queue) Size() int32

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() *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) GetTaskNum added in v1.2.2

func (t *Tasker) GetTaskNum() int

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) (reterr error)

func (*Tasker) Stop

func (t *Tasker) Stop()

type TaskerOption

type TaskerOption func(*TaskerOptions)

func WithExecuteTimeout added in v1.1.8

func WithExecuteTimeout(d time.Duration) TaskerOption

func WithOutput added in v1.2.8

func WithOutput(output io.Writer) TaskerOption

func WithStartFns added in v1.0.3

func WithStartFns(f ...StartFn) TaskerOption

func WithStopFns added in v1.1.1

func WithStopFns(f ...StopFn) TaskerOption

func WithTimeout

func WithTimeout(d time.Duration) TaskerOption

func WithUniqueId added in v1.2.2

func WithUniqueId(id int32) TaskerOption

func WithUpdateFn added in v1.0.2

func WithUpdateFn(f UpdateFn) TaskerOption

func WithUpdateInterval added in v1.1.7

func WithUpdateInterval(d time.Duration) TaskerOption

type TaskerOptions

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

type UpdateFn added in v1.0.2

type UpdateFn func() error

Jump to

Keyboard shortcuts

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