scheduler

package module
v0.0.0-...-fbcbf73 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2018 License: MIT Imports: 5 Imported by: 4

README

scheduler

Warning: Don't use Schedule in a Task. It often leads to deadlock. For an example:

type RetryTask struct {
    task Task
    ctx *context.Context
}

func (rt *RetryTask) Do(ctx *context.Context) error {
    if err := rt.task.Do(rt.ctx); err == nil {
        return nil
    }

    // get times and pool from context
    if times > 0 {
        times--
        pool.Schedule(NewRetryContext(times, pool), NewRetryTask(info, rt.task))
        return nil
    }

    return err
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrScheduleTimeout happens when task schedule failed during the specific interval.
	ErrScheduleTimeout = errors.New("schedule not available currently")
)

Functions

func StartWorker

func StartWorker(pool *Pool)

StartWorker create a new worker.

Types

type Pool

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

Pool caches tasks and schedule tasks to work.

func New

func New(qsize, wsize int) *Pool

New a goroutine pool.

func (*Pool) Schedule

func (p *Pool) Schedule(ctx context.Context, task Task) error

Schedule push a task on queue.

func (*Pool) ScheduleWithTimeout

func (p *Pool) ScheduleWithTimeout(ctx context.Context, timeout time.Duration, task Task) error

ScheduleWithTimeout try to push a task on queue, if timeout, return false.

func (*Pool) Stop

func (p *Pool) Stop()

func (*Pool) Wait

func (p *Pool) Wait()

type Task

type Task interface {
	Do(context.Context) error
}

Task represents a generic task.

type TaskFunc

type TaskFunc func(context.Context) error

TaskFunc is a wrapper for task function.

func (TaskFunc) Do

func (t TaskFunc) Do(ctx context.Context) error

Do is the Task interface implementation for type TaskFunc.

type Worker

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

Worker represents a working goroutine.

Jump to

Keyboard shortcuts

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