gotask

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2019 License: MIT Imports: 4 Imported by: 4

README

gotask

Simple routine group library for Golang. Main purpose is to gracefully shutdown complex multitask apps.

go get github.com/void616/gotask

Examples

Group

Make a group, add tasks, run it and wait for completion.

t1, _ := gotask.NewTask("MyTask #1", func() { log.Println("Routine 1") })
t2, _ := gotask.NewTask("MyTask #2", func() { log.Println("Routine 2") })

g := gotask.NewGroup("MyGroup")
g.Add(t1)
g.Add(t2)

g.Run()
g.Wait()

Output:

Routine 2
Routine 1

Task lifetime

Pass token into the routine to control it's lifetime.

result := 0
routine := func(token *gotask.Token) {
  for {
    result++
    // sleep with alertness
    if token.Sleep(time.Minute * 60) {
      break
    }
  }
}

t, _ := gotask.NewTask("MyTask", routine)
token, waiter, _ := t.Run()

token.Stop()
waiter.Wait()

log.Println("Result", result)

Output:

Result 1

Panic

Stop group of tasks on panic.

g := gotask.NewGroup("MyGroup")

routine1 := func(token *gotask.Token) {
  log.Println("Routine 1 begin")
  defer log.Println("Routine 1 end")
  token.Sleep(time.Minute * 60)
}

routine2 := func(token *gotask.Token) {
  log.Println("Routine 2 begin")
  defer log.Println("Routine 2 end")
  
  panic("panic!")
}

t1, _ := gotask.NewTask("MyTask #1", routine1)
t2, _ := gotask.NewTask("MyTask #2", routine2)

t2.Recover(func(v interface{}) {
  g.Stop()
})

g.Add(t1)
g.Add(t2)

g.Run()
g.Wait()

Output:

Routine 1 begin
Routine 2 begin
Routine 2 end
Routine 1 end

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidRoutine means routine has invalid type
	ErrInvalidRoutine = errors.New("invalid routine")
	// ErrInvalidState means a task/group state is invalid for current operation (broken flow)
	ErrInvalidState = errors.New("invalid state")
)

Functions

This section is empty.

Types

type Group

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

Group is a group of Tasks.

func NewGroup

func NewGroup(tag string) *Group

NewGroup instance.

func (*Group) Add

func (g *Group) Add(t *Task) error

Add adds a Task. If the Group is running, `ErrInvalidState` error will be returned.

func (*Group) Log added in v1.0.2

func (g *Group) Log(l Logger) error

Log sets a Logger. If the Group is running, `ErrInvalidState` error will be returned.

func (*Group) Run

func (g *Group) Run() error

Run starts Group's Tasks. If the Group is running, `ErrInvalidState` error will be returned. Run will ignore any Task that is already running (i.e. Task.Run() return error).

func (*Group) Stop

func (g *Group) Stop()

Stop requires Group's Tasks to stop. Does nothing if the Group is not running.

func (*Group) Tag

func (g *Group) Tag() string

Tag gets Group's tag.

func (*Group) TokenOf added in v1.0.2

func (g *Group) TokenOf(t *Task) (*Token, error)

TokenOf returns specific Task Token. If the Task wasn't started by the Group or Group isn't running, `ErrInvalidState` will be returned.

func (*Group) Wait

func (g *Group) Wait()

Wait awaits Group's Tasks stop. Does nothing if the Group is not running.

func (*Group) WaiterOf added in v1.0.2

func (g *Group) WaiterOf(t *Task) (*Waiter, error)

WaiterOf returns specific Task Waiter. If the Task wasn't started by the Group or Group isn't running, `ErrInvalidState` will be returned.

type Logger added in v1.0.2

type Logger interface {
	Log(...interface{})
}

Logger logs events for debug

type Recover

type Recover func(interface{})

Recover is a callback function template that will be called on routine panic.

type Task

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

Task is a named routine that does something in parallel.

func NewTask

func NewTask(tag string, routine interface{}, args ...interface{}) (*Task, error)

NewTask instance. Arg `routine` should be an entity of type: func(), func(*Token), func(...interface{}), func(*Token, ...interface{}). Otherwise `ErrInvalidRoutine` will be returned.

func (*Task) Log added in v1.0.2

func (t *Task) Log(l Logger) *Task

Log sets a Logger. If the Task is running, `ErrInvalidState` error will be returned.

func (*Task) Recover

func (t *Task) Recover(r Recover) *Task

Recover specifies a callback function that will be called in case of routine's panic.

func (*Task) Run

func (t *Task) Run() (*Token, *Waiter, error)

Run starts routine in a goroutine and returns `*Token` to control lifetime and `*Waiter` to wait routine stop. If the Task is already running, `ErrInvalidState` error will be returned.

func (*Task) Tag

func (t *Task) Tag() string

Tag gets Task's tag.

type Token

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

Token controls a Task's lifetime.

func (*Token) Sleep

func (t *Token) Sleep(d time.Duration) bool

Sleep sleeps specified amount of time or until related Task is requested to stop. Returns `true` if a stop-event received while sleeping. In general case this method is used from within a routine.

func (*Token) Stop

func (t *Token) Stop()

Stop requires related Task to stop.

func (*Token) Stopped

func (t *Token) Stopped() bool

Stopped returns `true` if related Task should be stopped. In general case this method is used from within a routine.

func (*Token) Tag

func (t *Token) Tag() string

Tag gets related Task's tag.

type Waiter

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

Waiter allows to await a Task.

func (*Waiter) Tag

func (w *Waiter) Tag() string

Tag gets related Task's tag.

func (*Waiter) Wait

func (w *Waiter) Wait()

Wait awaits Task stop.

func (*Waiter) WaitTimeout

func (w *Waiter) WaitTimeout(d time.Duration) bool

WaitTimeout awaits Task stop specified amount of time. Returns `true` if the Task is stopped.

Jump to

Keyboard shortcuts

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