cgscheduler

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2018 License: Apache-2.0 Imports: 5 Imported by: 0

README

cgscheduler Build Status Coverage Status

A concurrent scheduler for tasks with dependencies.

Installation

go get -u github.com/jackwakefield/cgscheduler

or

# go get -u github.com/golang/dep/cmd/dep
# dep init
dep ensure -add github.com/jackwakefield/cgscheduler

Documentation

GoDoc

Usage

Create the scheduler with cgscheduler.New()

scheduler := cgscheduler.New()

// or, limit the number of concurrent tasks executed at once,
// by default this is set to runtime.NumCPU()
scheduler := cgscheduler.New(cgscheduler.ConcurrentTasks(2))

Tasks can be created with functions matching the signature func(ctx context.Context) error

// add a task which outputs "World!"
taskWorld := scheduler.AddTask(func(ctx context.Context) error {
    fmt.Print("World!")
    return nil
})

// add a task which outputs "Hello"
taskHello := scheduler.AddTask(func(ctx context.Context) error {
    fmt.Print("Hello")
    return nil
})

// add a task which outputs a space (" ")
taskSeparator := scheduler.AddTask(func(ctx context.Context) error {
    fmt.Print(" ")
    return nil
})

Dependencies can be created between tasks with Task.DependsOn, which uses Scheduler.AddDependency internally.

// execute taskHello before taskSeparator
taskSeparator.DependsOn(taskHello)

// execute taskSeparator before taskWorld
taskWorld.DependsOn(taskSeparator)

Run the scheduler with Scheduler.Run, this accepts a context as a parameter and returns an error.

The scheduler returns when all tasks are complete, or a task has returned an error.

if err := scheduler.Run(context.Background()); err != nil {
    log.Fatalln(err)
}

// Outputs:
// Hello World!

Example

example/main.go

Internals

Internally the scheduler uses a Directed Acyclic Graph to represent the tasks as nodes and their dependencies as edges.

When the scheduler is ran, and when the graph state has changed since the scheduler was last ran, the tasks are topologically ordered into levels using the Coffman-Graham algorithm.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCircularDependency = errors.New("A task has a circular dependency")
	ErrOrderFailure       = errors.New("A problem occurred sorting the tasks in the correct order")
)

Scheduler related errors.

Functions

This section is empty.

Types

type Option

type Option func(*Options)

Option describes a function which mutates the scheduler's configuration.

func ConcurrentTasks

func ConcurrentTasks(maximum int) Option

ConcurrentTasks sets the maximum number of tasks to run at any given time.

type Options

type Options struct {
	ConcurrentTasks int
}

Options holds the scheduler's configuration.

type Scheduler

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

Scheduler is a concurrent task scheduler.

func New

func New(options ...Option) *Scheduler

New returns a concurrent task scheduler. Tasks may be dependent on each, being sorted into a layered topological order using the Coffman-Graham algorithm.

func (*Scheduler) AddDependency

func (s *Scheduler) AddDependency(task *Task, dependency *Task)

AddDependency creates a dependency between the specified task itself and the dependency task. When ran, the scheduler ensures the dependency task is executed first.

func (*Scheduler) AddTask

func (s *Scheduler) AddTask(function TaskFunc) *Task

AddTask registers the function with the scheduler and returns a Task.

func (*Scheduler) Dependencies

func (s *Scheduler) Dependencies(task *Task) []*Task

Dependencies lists the tasks the specified task depends on.

func (*Scheduler) DependencyCount

func (s *Scheduler) DependencyCount(task *Task) int

DependencyCount returns the number of tasks the specified task depends on.

func (*Scheduler) RemoveDependency

func (s *Scheduler) RemoveDependency(task *Task, dependency *Task)

RemoveDependency removes the dependency between the specified task itself and the dependency task.

func (*Scheduler) RemoveTask

func (s *Scheduler) RemoveTask(task *Task)

RemoveTask removes the specified task from the scheduler.

func (*Scheduler) RemoveTasks

func (s *Scheduler) RemoveTasks(tasks ...*Task)

RemoveTasks removes the specified tasks from the scheduler.

func (*Scheduler) Run

func (s *Scheduler) Run(ctx context.Context) error

Run executes the scheduler's tasks.

func (*Scheduler) TaskCount

func (s *Scheduler) TaskCount() int

TaskCount returns the number of tasks registered with the scheduler.

func (*Scheduler) Tasks

func (s *Scheduler) Tasks() []*Task

Tasks returns a list of the tasks registered with the scheduler.

type Task

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

Task wraps a task function.

func (*Task) Dependencies

func (t *Task) Dependencies() []*Task

Dependencies lists the tasks the this task depends on.

func (*Task) DependencyCount

func (t *Task) DependencyCount() int

DependencyCount returns the number of tasks this task depends on.

func (*Task) DependsOn

func (t *Task) DependsOn(dependency *Task)

DependsOn creates a dependency between this task and the dependency task. When ran, the scheduler ensures the dependency task is executed first.

func (*Task) RemoveDependency

func (t *Task) RemoveDependency(dependency *Task)

RemoveDependency removes the dependency between this task and the dependency task.

func (*Task) Run

func (t *Task) Run(ctx context.Context) error

Run executes the task.

type TaskFunc

type TaskFunc = func(ctx context.Context) error

TaskFunc describes the signature of a task function.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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