task

package
v0.0.0-...-6bcf8dd Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Pending job is wait to running
	Pending = JobState(0)
	// Running job is running
	Running = JobState(1)
	// Cancelling job is cancelling
	Cancelling = JobState(2)
	// Cancelled job is cancelled
	Cancelled = JobState(3)
	// Finished job is complete
	Finished = JobState(4)
	// Failed job is failed when execute
	Failed = JobState(5)
)

Variables

View Source
var (
	// ErrJobCancelled error job cancelled
	ErrJobCancelled = errors.New("Job cancelled")
)

Functions

This section is empty.

Types

type Job

type Job struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Job is do for something with state

func (*Job) Cancel

func (job *Job) Cancel()

Cancel cancel the job

func (*Job) GetResult

func (job *Job) GetResult() interface{}

GetResult returns job result

func (*Job) IsCancelled

func (job *Job) IsCancelled() bool

IsCancelled returns true if job state is Cancelled

func (*Job) IsCancelling

func (job *Job) IsCancelling() bool

IsCancelling returns true if job state is Cancelling

func (*Job) IsComplete

func (job *Job) IsComplete() bool

IsComplete return true means the job is complete.

func (*Job) IsFailed

func (job *Job) IsFailed() bool

IsFailed returns true if job state is Failed

func (*Job) IsFinished

func (job *Job) IsFinished() bool

IsFinished returns true if job state is Finished

func (*Job) IsNotComplete

func (job *Job) IsNotComplete() bool

IsNotComplete return true means the job is not complete.

func (*Job) IsPending

func (job *Job) IsPending() bool

IsPending returns true if job state is Pending

func (*Job) IsRunning

func (job *Job) IsRunning() bool

IsRunning returns true if job state is Running

func (*Job) SetResult

func (job *Job) SetResult(result interface{})

SetResult set result

type JobState

type JobState int

JobState is the job state

type Runner

type Runner struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Runner TODO

func NewRunner

func NewRunner() *Runner

NewRunner returns a task runner

func (*Runner) AddNamedWorker

func (s *Runner) AddNamedWorker(name string) (uint64, error)

AddNamedWorker add a named worker, the named worker has uniq queue, so jobs are linear execution

func (*Runner) IsNamedWorkerBusy

func (s *Runner) IsNamedWorkerBusy(worker string) bool

IsNamedWorkerBusy returns true if named queue is not empty

func (*Runner) RunCancelableTask

func (s *Runner) RunCancelableTask(task func(context.Context)) (uint64, error)

RunCancelableTask run a task that can be cancelled Example:

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

func (*Runner) RunJob

func (s *Runner) RunJob(desc string, task func() error) error

RunJob run a job

func (*Runner) RunJobWithNamedWorker

func (s *Runner) RunJobWithNamedWorker(desc, worker string, task func() error) error

RunJobWithNamedWorker run a job in a named worker

func (*Runner) RunJobWithNamedWorkerWithCB

func (s *Runner) RunJobWithNamedWorkerWithCB(desc, worker string, task func() error, cb func(*Job)) error

RunJobWithNamedWorkerWithCB run a job in a named worker

func (*Runner) RunTask

func (s *Runner) RunTask(task func()) error

RunTask runs a task in new goroutine

func (*Runner) Stop

func (s *Runner) Stop() error

Stop stop all task RunTask will failure with an error Wait complete for the tasks that already in execute Cancel the tasks that is not start

func (*Runner) StopCancelableTask

func (s *Runner) StopCancelableTask(id uint64) error

StopCancelableTask stop cancelable spec task

Jump to

Keyboard shortcuts

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