scheduler

package module
v0.0.0-...-9c20e1a Latest Latest
Warning

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

Go to latest
Published: May 24, 2021 License: MIT Imports: 7 Imported by: 0

README

scheduler

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareByDeadline

func CompareByDeadline(t1, t2 Task) bool

CompareByDeadline is the Less function used deadline

func CompareByPriority

func CompareByPriority(t1, t2 Task) bool

CompareByPriority is the Less function used priority

Types

type CompareFunc

type CompareFunc func(t1, t2 Task) bool

CompareFunc is the type for function used for sorting

type Queue

type Queue interface {
	Add(t Task)
	Get() Task
	Done(t Task)
	SetCompareFunc(CompareFunc)
	IsEmpty() bool
}

Queue is for storing tasks, supports sorting of tasks, and determines the order of execution of tasks

func NewChanQueue

func NewChanQueue(qsize int) Queue

func NewQueue

func NewQueue() Queue

NewQueue returns a new Queue

type Scheduler

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

Scheduler caches tasks and schedule tasks to work.

func New

func New() *Scheduler

New a goroutine Scheduler.

func (*Scheduler) Schedule

func (s *Scheduler) Schedule(t Task) error

Schedule push a task on queue.

func (*Scheduler) ScheduleWithCtx

func (s *Scheduler) ScheduleWithCtx(ctx context.Context, t Task) error

Schedule push a task on queue.

func (*Scheduler) SortByDeadline

func (s *Scheduler) SortByDeadline() error

SortByPriority uses deadline as the comparison factors

func (*Scheduler) SortByPriority

func (s *Scheduler) SortByPriority() error

SortByPriority uses priority as the comparison factors

func (*Scheduler) Start

func (s *Scheduler) Start(wsize int)

Starts the scheduling.

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop closes the schduler

func (*Scheduler) Wait

func (s *Scheduler) Wait()

Wait waits for all task finished

type Task

type Task interface {
	Do(context.Context) error
	WithRetry(times uint) Task
	WithTimeout(timeout time.Duration) Task
	WithCancelFunc(timeout time.Duration) (Task, context.CancelFunc)
	WithPriority(int) Task

	BindScheduler(s *Scheduler) Task
	SetContext(context context.Context) Task
}

Task represents a generic task.

func NewTask

func NewTask(f TaskFunc) Task

NewTask return a task

type TaskFunc

type TaskFunc func(context.Context) error

TaskFunc is a wrapper for task function.

func (TaskFunc) BindScheduler

func (t TaskFunc) BindScheduler(s *Scheduler) Task

BindScheduler bind the scheduler with this task, this shouldn't called by user

func (TaskFunc) Do

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

Do is the Task interface implementation for type TaskFunc.

func (TaskFunc) SetContext

func (t TaskFunc) SetContext(ctx context.Context) Task

SetContext set the context for this task, the context will used when call the internal function

func (TaskFunc) WithCancelFunc

func (t TaskFunc) WithCancelFunc(timeout time.Duration) (Task, context.CancelFunc)

WithCancelFunc returns the cancel function for this task

func (TaskFunc) WithPriority

func (t TaskFunc) WithPriority(priority int) Task

WithPriority set the priority for this task

func (TaskFunc) WithRetry

func (t TaskFunc) WithRetry(times uint) Task

WithRetry set the retry times for this task

func (TaskFunc) WithTimeout

func (t TaskFunc) WithTimeout(timeout time.Duration) Task

WithTimeout set the timeout for this task

type Type

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

Type is the real implementation for Queue, it supports sorting and avoid reentrant

func (*Type) Add

func (q *Type) Add(t Task)

Add add a new Task to Queue

func (*Type) Done

func (q *Type) Done(t Task)

Done means that the Task has finished

func (*Type) Get

func (q *Type) Get() Task

Get return a task

func (*Type) IsEmpty

func (q *Type) IsEmpty() bool

IsEmpty tells the user whether the queue is empty

func (*Type) Len

func (q *Type) Len() int

Len return the length for q.queue

func (*Type) Less

func (q *Type) Less(i, j int) bool

Less determines whether the element of index i is smaller than index j

func (*Type) Pop

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

Pop remove the last element in q.queue

func (*Type) Push

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

Push add a task to q.queue

func (*Type) SetCompareFunc

func (q *Type) SetCompareFunc(f CompareFunc)

SetCompareFunc set the func used for sorting

func (*Type) Swap

func (q *Type) Swap(i, j int)

Swap swaps the location for index i and j

type Worker

type Worker interface {
	Work()
}

func NewGoroutineWorker

func NewGoroutineWorker(s *Scheduler, stopCh chan struct{}) Worker

Jump to

Keyboard shortcuts

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