taskRunner

package
v0.0.0-...-11d5444 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IPQItem

type IPQItem interface {
	GetIndex() int
	SetIndex(idx int)
	GetPriority() int
}

type ITask

type ITask interface {
	SetTaskDoneCh(ch chan struct{})
}

type Priority

type Priority int64
const (
	Low Priority = iota + 1
	Normal
	Important
	Critical
)

Priorities

type PriorityQueue

type PriorityQueue[T IPQItem] struct {
	// contains filtered or unexported fields
}

func NewPriorityQueue

func NewPriorityQueue[T IPQItem]() *PriorityQueue[T]

func (*PriorityQueue[T]) Items

func (p *PriorityQueue[T]) Items() []T

func (*PriorityQueue[T]) Len

func (p *PriorityQueue[T]) Len() int

func (*PriorityQueue[T]) Pop

func (p *PriorityQueue[T]) Pop() T

func (*PriorityQueue[T]) Push

func (p *PriorityQueue[T]) Push(item T)

type TaskRunner

type TaskRunner[T ITask] struct {
	// contains filtered or unexported fields
}

TaskRunner ...

Whenever we call "WithPriority(...)" a new task will be pushed in the "pushCh" channel and then the code will block until the task can actually be executed.

The task runner starts 2 threads.

  • One that receive new tasks from the "WithPriority" function and put them in the priority queue. It then notifies the "popCh" that a new task has been added.
  • The second thread pop tasks from the PQ. Then notify the task that it can be processed. This will unblock the code at "WithPriority", then it waits until that task is done being processed. When the task is completed, it will wait for another one from the "popCh".

This way we can ensure that we ever only have 1 task being executed at the time, but we can queue as many as we want with different priorities.

func NewTaskRunner

func NewTaskRunner[T ITask](ctx context.Context, factory func() T) *TaskRunner[T]

func (*TaskRunner[T]) GetTasks

func (r *TaskRunner[T]) GetTasks() (out TasksOverview)

func (*TaskRunner[T]) WithPriority

func (r *TaskRunner[T]) WithPriority(priority Priority) T

type TasksOverview

type TasksOverview struct {
	Low       Priority
	Normal    Priority
	Important Priority
	Critical  Priority
	Total     int64
}

TasksOverview overview of tasks in heap

Jump to

Keyboard shortcuts

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